Commit d78d769bb2441b3524b138969dcc3b0d6775238a

Authored by Georg Hopp
1 parent 5b8db017

logger now works and has some basic testing

1 -#ifndef __MONITOR_H__  
2 -#define __MONITOR_H__ 1 +#ifndef __LOGGER_H__
  2 +#define __LOGGER_H__
3 3
4 #include <cclass.h> 4 #include <cclass.h>
5 5
@@ -29,6 +29,6 @@ void logger_log(LOGGER this, int level, const char * msg, ...) @@ -29,6 +29,6 @@ void logger_log(LOGGER this, int level, const char * msg, ...)
29 29
30 void logger_add(LOGGER this, logger_logfnct logfunc); 30 void logger_add(LOGGER this, logger_logfnct logfunc);
31 31
32 -#endif /* __MONITOR_H__ */ 32 +#endif /* __LOGGER_H__ */
33 33
34 // vim: set ts=4 sw=4: 34 // vim: set ts=4 sw=4:
1 #define _ISOC99_SOURCE 1 #define _ISOC99_SOURCE
2 2
3 #include <syslog.h> 3 #include <syslog.h>
4 -#include <string.h>  
5 #include <stdio.h> 4 #include <stdio.h>
  5 +#include <string.h>
6 6
7 #include "logger.h" 7 #include "logger.h"
8 8
@@ -19,12 +19,14 @@ const int priority[] = { @@ -19,12 +19,14 @@ const int priority[] = {
19 19
20 INIT_CLASS(LOGGER); 20 INIT_CLASS(LOGGER);
21 21
22 -static void logger_syslog(int level, const char * msg);  
23 - 22 +static void
  23 +logger_syslog(int level, const char * msg)
  24 +{
  25 + syslog(priority[level], "%s", msg);
  26 +}
24 27
25 __construct(LOGGER) 28 __construct(LOGGER)
26 { 29 {
27 - memset(this->logfncts, 0, sizeof(this->logfncts));  
28 this->logfncts[0] = logger_syslog; 30 this->logfncts[0] = logger_syslog;
29 this->logfncts_count = 1; 31 this->logfncts_count = 1;
30 } 32 }
@@ -34,12 +36,6 @@ __jsonConst(LOGGER) {} @@ -34,12 +36,6 @@ __jsonConst(LOGGER) {}
34 __toJson(LOGGER) {} 36 __toJson(LOGGER) {}
35 __clear(LOGGER) {} 37 __clear(LOGGER) {}
36 38
37 -static void  
38 -logger_syslog(int level, const char * msg)  
39 -{  
40 - syslog(level, "%s", msg);  
41 -}  
42 -  
43 void 39 void
44 logger_log(LOGGER this, int level, const char * message, ...) { 40 logger_log(LOGGER this, int level, const char * message, ...) {
45 va_list args; 41 va_list args;
@@ -58,6 +54,7 @@ logger_log(LOGGER this, int level, const char * message, ...) { @@ -58,6 +54,7 @@ logger_log(LOGGER this, int level, const char * message, ...) {
58 54
59 while (NULL != *logfnct) { 55 while (NULL != *logfnct) {
60 (*logfnct)(level, buffer); 56 (*logfnct)(level, buffer);
  57 + logfnct++;
61 } 58 }
62 } 59 }
63 60
@@ -6,10 +6,10 @@ check_PROGRAMS = cclassTest loggerTest @@ -6,10 +6,10 @@ check_PROGRAMS = cclassTest loggerTest
6 6
7 cclassTest_SOURCES = runtest.c cclassTest.c mock/class.c ../src/cclass.c 7 cclassTest_SOURCES = runtest.c cclassTest.c mock/class.c ../src/cclass.c
8 cclassTest_LDADD = $(LIBOBJS) 8 cclassTest_LDADD = $(LIBOBJS)
9 -cclassTest_CFLAGS = -Wall -I ../include -I .. -I . 9 +cclassTest_CFLAGS = -Wall -ggdb -O0 -finline-functions -I ../include -I .. -I .
10 10
11 loggerTest_SOURCES = runtest.c loggerTest.c ../src/cclass.c ../src/logger.c 11 loggerTest_SOURCES = runtest.c loggerTest.c ../src/cclass.c ../src/logger.c
12 loggerTest_LDADD = $(LIBOBJS) 12 loggerTest_LDADD = $(LIBOBJS)
13 -loggerTest_CFLAGS = -Wall -I ../include -I .. -I . 13 +loggerTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I .
14 14
15 EXTRA_DIST = runtest.h mock/class.h 15 EXTRA_DIST = runtest.h mock/class.h
@@ -24,15 +24,19 @@ @@ -24,15 +24,19 @@
24 #include "logger.h" 24 #include "logger.h"
25 25
26 26
  27 +int level = -1;
  28 +const char * msg = NULL;
  29 +
27 static void 30 static void
28 -logfnct_mock(int level, const char * msg) 31 +logfnct_mock(int _level, const char * _msg)
29 { 32 {
30 - printf("DEBUG: %d / %s\n", level, msg); 33 + level = _level;
  34 + msg = malloc(strlen(_msg) + 1);
  35 + strcpy(msg, _msg);
31 } 36 }
32 37
  38 +const char testname[] = "loggerTest";
33 LOGGER logger = NULL; 39 LOGGER logger = NULL;
34 -int level = -1;  
35 -char * msg = NULL;  
36 40
37 41
38 static 42 static
@@ -53,6 +57,13 @@ static @@ -53,6 +57,13 @@ static
53 int 57 int
54 __tearDown() 58 __tearDown()
55 { 59 {
  60 + level = -1;
  61 +
  62 + if (NULL != msg) {
  63 + free(msg);
  64 + msg = NULL;
  65 + }
  66 +
56 if (NULL != logger) { 67 if (NULL != logger) {
57 ASSERT_OBJECT(logger); 68 ASSERT_OBJECT(logger);
58 delete(&logger); 69 delete(&logger);
@@ -66,7 +77,10 @@ static @@ -66,7 +77,10 @@ static
66 int 77 int
67 testDummy() 78 testDummy()
68 { 79 {
69 - logger_log(logger, LOGGER_DEBUG, "moo foo bar"); 80 + logger_log(logger, LOGGER_ERR, "foo %d %s", 123, "bar");
  81 +
  82 + ASSERT_EQUAL(LOGGER_ERR, level);
  83 + ASSERT_STRING_EQUAL("foo 123 bar", msg);
70 84
71 return TEST_OK; 85 return TEST_OK;
72 } 86 }
@@ -35,7 +35,6 @@ _reset() @@ -35,7 +35,6 @@ _reset()
35 35
36 36
37 CLASS(MOCK_CLASS) { 37 CLASS(MOCK_CLASS) {
38 - const CCLASS const class;  
39 int value; 38 int value;
40 }; 39 };
41 40
@@ -39,15 +39,13 @@ const char results[3] = { @@ -39,15 +39,13 @@ const char results[3] = {
39 int 39 int
40 isObjectNull(void * _object) 40 isObjectNull(void * _object)
41 { 41 {
42 - const CCLASS * class = _object; 42 + const CCLASS * class = _object - sizeof(CCLASS);
43 43
44 if (! isObject(_object)) { 44 if (! isObject(_object)) {
45 return 0; 45 return 0;
46 } 46 }
47 47
48 - return isMemNull(  
49 - _object + CCLASS_PTR_SIZE,  
50 - (*class)->size - CCLASS_PTR_SIZE); 48 + return isMemNull(_object, (*class)->size);
51 } 49 }
52 50
53 int 51 int
@@ -77,13 +77,13 @@ enum RESULT_TYPES { @@ -77,13 +77,13 @@ enum RESULT_TYPES {
77 __FILE__, __LINE__, #val, size); \ 77 __FILE__, __LINE__, #val, size); \
78 return TEST_FAILED; } 78 return TEST_FAILED; }
79 79
80 -#define ASSERT_STRING_EQUAL(val1,val2) \ 80 +#define ASSERT_STRING_EQUAL(val1, val2) \
81 if(0 != strcmp((val1), (val2))) { \ 81 if(0 != strcmp((val1), (val2))) { \
82 printf("%s[%d]: Assertion failed that string %s EQUALS %s\n", \ 82 printf("%s[%d]: Assertion failed that string %s EQUALS %s\n", \
83 __FILE__, __LINE__, val1, val2); \ 83 __FILE__, __LINE__, val1, val2); \
84 return TEST_FAILED; } 84 return TEST_FAILED; }
85 85
86 -#define ASSERT_STRING_NOT_EQUAL(val1,val2) \ 86 +#define ASSERT_STRING_NOT_EQUAL(val1, val2) \
87 if(0 == strcmp((val1), (val2))) { \ 87 if(0 == strcmp((val1), (val2))) { \
88 printf("%s[%d]: Assertion failed that string %s NOT EQUALS %s\n", \ 88 printf("%s[%d]: Assertion failed that string %s NOT EQUALS %s\n", \
89 __FILE__, __LINE__, val1, val2); \ 89 __FILE__, __LINE__, val1, val2); \
Please register or login to post a comment