Browse Source

logger now works and has some basic testing

master
Georg Hopp 14 years ago
parent
commit
d78d769bb2
  1. 6
      include/logger.h
  2. 17
      src/logger.c
  3. 4
      tests/Makefile.am
  4. 24
      tests/loggerTest.c
  5. 1
      tests/mock/class.h
  6. 6
      tests/runtest.c
  7. 4
      tests/runtest.h

6
include/logger.h

@ -1,5 +1,5 @@
#ifndef __MONITOR_H__
#define __MONITOR_H__
#ifndef __LOGGER_H__
#define __LOGGER_H__
#include <cclass.h>
@ -29,6 +29,6 @@ void logger_log(LOGGER this, int level, const char * msg, ...)
void logger_add(LOGGER this, logger_logfnct logfunc);
#endif /* __MONITOR_H__ */
#endif /* __LOGGER_H__ */
// vim: set ts=4 sw=4:

17
src/logger.c

@ -1,8 +1,8 @@
#define _ISOC99_SOURCE
#include <syslog.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
#include "logger.h"
@ -19,12 +19,14 @@ const int priority[] = {
INIT_CLASS(LOGGER);
static void logger_syslog(int level, const char * msg);
static void
logger_syslog(int level, const char * msg)
{
syslog(priority[level], "%s", msg);
}
__construct(LOGGER)
{
memset(this->logfncts, 0, sizeof(this->logfncts));
this->logfncts[0] = logger_syslog;
this->logfncts_count = 1;
}
@ -34,12 +36,6 @@ __jsonConst(LOGGER) {}
__toJson(LOGGER) {}
__clear(LOGGER) {}
static void
logger_syslog(int level, const char * msg)
{
syslog(level, "%s", msg);
}
void
logger_log(LOGGER this, int level, const char * message, ...) {
va_list args;
@ -58,6 +54,7 @@ logger_log(LOGGER this, int level, const char * message, ...) {
while (NULL != *logfnct) {
(*logfnct)(level, buffer);
logfnct++;
}
}

4
tests/Makefile.am

@ -6,10 +6,10 @@ check_PROGRAMS = cclassTest loggerTest
cclassTest_SOURCES = runtest.c cclassTest.c mock/class.c ../src/cclass.c
cclassTest_LDADD = $(LIBOBJS)
cclassTest_CFLAGS = -Wall -I ../include -I .. -I .
cclassTest_CFLAGS = -Wall -ggdb -O0 -finline-functions -I ../include -I .. -I .
loggerTest_SOURCES = runtest.c loggerTest.c ../src/cclass.c ../src/logger.c
loggerTest_LDADD = $(LIBOBJS)
loggerTest_CFLAGS = -Wall -I ../include -I .. -I .
loggerTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I .
EXTRA_DIST = runtest.h mock/class.h

24
tests/loggerTest.c

@ -24,15 +24,19 @@
#include "logger.h"
int level = -1;
const char * msg = NULL;
static void
logfnct_mock(int level, const char * msg)
logfnct_mock(int _level, const char * _msg)
{
printf("DEBUG: %d / %s\n", level, msg);
level = _level;
msg = malloc(strlen(_msg) + 1);
strcpy(msg, _msg);
}
const char testname[] = "loggerTest";
LOGGER logger = NULL;
int level = -1;
char * msg = NULL;
static
@ -53,6 +57,13 @@ static
int
__tearDown()
{
level = -1;
if (NULL != msg) {
free(msg);
msg = NULL;
}
if (NULL != logger) {
ASSERT_OBJECT(logger);
delete(&logger);
@ -66,7 +77,10 @@ static
int
testDummy()
{
logger_log(logger, LOGGER_DEBUG, "moo foo bar");
logger_log(logger, LOGGER_ERR, "foo %d %s", 123, "bar");
ASSERT_EQUAL(LOGGER_ERR, level);
ASSERT_STRING_EQUAL("foo 123 bar", msg);
return TEST_OK;
}

1
tests/mock/class.h

@ -35,7 +35,6 @@ _reset()
CLASS(MOCK_CLASS) {
const CCLASS const class;
int value;
};

6
tests/runtest.c

@ -39,15 +39,13 @@ const char results[3] = {
int
isObjectNull(void * _object)
{
const CCLASS * class = _object;
const CCLASS * class = _object - sizeof(CCLASS);
if (! isObject(_object)) {
return 0;
}
return isMemNull(
_object + CCLASS_PTR_SIZE,
(*class)->size - CCLASS_PTR_SIZE);
return isMemNull(_object, (*class)->size);
}
int

4
tests/runtest.h

@ -77,13 +77,13 @@ enum RESULT_TYPES {
__FILE__, __LINE__, #val, size); \
return TEST_FAILED; }
#define ASSERT_STRING_EQUAL(val1,val2) \
#define ASSERT_STRING_EQUAL(val1, val2) \
if(0 != strcmp((val1), (val2))) { \
printf("%s[%d]: Assertion failed that string %s EQUALS %s\n", \
__FILE__, __LINE__, val1, val2); \
return TEST_FAILED; }
#define ASSERT_STRING_NOT_EQUAL(val1,val2) \
#define ASSERT_STRING_NOT_EQUAL(val1, val2) \
if(0 == strcmp((val1), (val2))) { \
printf("%s[%d]: Assertion failed that string %s NOT EQUALS %s\n", \
__FILE__, __LINE__, val1, val2); \

Loading…
Cancel
Save