Commit d78d769bb2441b3524b138969dcc3b0d6775238a
1 parent
5b8db017
logger now works and has some basic testing
Showing
7 changed files
with
35 additions
and
27 deletions
1 | -#ifndef __MONITOR_H__ | |
2 | -#define __MONITOR_H__ | |
1 | +#ifndef __LOGGER_H__ | |
2 | +#define __LOGGER_H__ | |
3 | 3 | |
4 | 4 | #include <cclass.h> |
5 | 5 | |
... | ... | @@ -29,6 +29,6 @@ void logger_log(LOGGER this, int level, const char * msg, ...) |
29 | 29 | |
30 | 30 | void logger_add(LOGGER this, logger_logfnct logfunc); |
31 | 31 | |
32 | -#endif /* __MONITOR_H__ */ | |
32 | +#endif /* __LOGGER_H__ */ | |
33 | 33 | |
34 | 34 | // vim: set ts=4 sw=4: | ... | ... |
1 | 1 | #define _ISOC99_SOURCE |
2 | 2 | |
3 | 3 | #include <syslog.h> |
4 | -#include <string.h> | |
5 | 4 | #include <stdio.h> |
5 | +#include <string.h> | |
6 | 6 | |
7 | 7 | #include "logger.h" |
8 | 8 | |
... | ... | @@ -19,12 +19,14 @@ const int priority[] = { |
19 | 19 | |
20 | 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 | 28 | __construct(LOGGER) |
26 | 29 | { |
27 | - memset(this->logfncts, 0, sizeof(this->logfncts)); | |
28 | 30 | this->logfncts[0] = logger_syslog; |
29 | 31 | this->logfncts_count = 1; |
30 | 32 | } |
... | ... | @@ -34,12 +36,6 @@ __jsonConst(LOGGER) {} |
34 | 36 | __toJson(LOGGER) {} |
35 | 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 | 39 | void |
44 | 40 | logger_log(LOGGER this, int level, const char * message, ...) { |
45 | 41 | va_list args; |
... | ... | @@ -58,6 +54,7 @@ logger_log(LOGGER this, int level, const char * message, ...) { |
58 | 54 | |
59 | 55 | while (NULL != *logfnct) { |
60 | 56 | (*logfnct)(level, buffer); |
57 | + logfnct++; | |
61 | 58 | } |
62 | 59 | } |
63 | 60 | ... | ... |
... | ... | @@ -6,10 +6,10 @@ check_PROGRAMS = cclassTest loggerTest |
6 | 6 | |
7 | 7 | cclassTest_SOURCES = runtest.c cclassTest.c mock/class.c ../src/cclass.c |
8 | 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 | 11 | loggerTest_SOURCES = runtest.c loggerTest.c ../src/cclass.c ../src/logger.c |
12 | 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 | 15 | EXTRA_DIST = runtest.h mock/class.h | ... | ... |
... | ... | @@ -24,15 +24,19 @@ |
24 | 24 | #include "logger.h" |
25 | 25 | |
26 | 26 | |
27 | +int level = -1; | |
28 | +const char * msg = NULL; | |
29 | + | |
27 | 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 | 39 | LOGGER logger = NULL; |
34 | -int level = -1; | |
35 | -char * msg = NULL; | |
36 | 40 | |
37 | 41 | |
38 | 42 | static |
... | ... | @@ -53,6 +57,13 @@ static |
53 | 57 | int |
54 | 58 | __tearDown() |
55 | 59 | { |
60 | + level = -1; | |
61 | + | |
62 | + if (NULL != msg) { | |
63 | + free(msg); | |
64 | + msg = NULL; | |
65 | + } | |
66 | + | |
56 | 67 | if (NULL != logger) { |
57 | 68 | ASSERT_OBJECT(logger); |
58 | 69 | delete(&logger); |
... | ... | @@ -66,7 +77,10 @@ static |
66 | 77 | int |
67 | 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 | 85 | return TEST_OK; |
72 | 86 | } | ... | ... |
... | ... | @@ -39,15 +39,13 @@ const char results[3] = { |
39 | 39 | int |
40 | 40 | isObjectNull(void * _object) |
41 | 41 | { |
42 | - const CCLASS * class = _object; | |
42 | + const CCLASS * class = _object - sizeof(CCLASS); | |
43 | 43 | |
44 | 44 | if (! isObject(_object)) { |
45 | 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 | 51 | int | ... | ... |
... | ... | @@ -77,13 +77,13 @@ enum RESULT_TYPES { |
77 | 77 | __FILE__, __LINE__, #val, size); \ |
78 | 78 | return TEST_FAILED; } |
79 | 79 | |
80 | -#define ASSERT_STRING_EQUAL(val1,val2) \ | |
80 | +#define ASSERT_STRING_EQUAL(val1, val2) \ | |
81 | 81 | if(0 != strcmp((val1), (val2))) { \ |
82 | 82 | printf("%s[%d]: Assertion failed that string %s EQUALS %s\n", \ |
83 | 83 | __FILE__, __LINE__, val1, val2); \ |
84 | 84 | return TEST_FAILED; } |
85 | 85 | |
86 | -#define ASSERT_STRING_NOT_EQUAL(val1,val2) \ | |
86 | +#define ASSERT_STRING_NOT_EQUAL(val1, val2) \ | |
87 | 87 | if(0 == strcmp((val1), (val2))) { \ |
88 | 88 | printf("%s[%d]: Assertion failed that string %s NOT EQUALS %s\n", \ |
89 | 89 | __FILE__, __LINE__, val1, val2); \ | ... | ... |
Please
register
or
login
to post a comment