Mon, 08 Oct 2012 14:04:52 +0200
added logging API
test/Makefile | file | annotate | diff | comparison | revisions | |
test/logging_tests.c | file | annotate | diff | comparison | revisions | |
test/logging_tests.h | file | annotate | diff | comparison | revisions | |
test/main.c | file | annotate | diff | comparison | revisions | |
test/string_tests.h | file | annotate | diff | comparison | revisions | |
ucx/Makefile | file | annotate | diff | comparison | revisions | |
ucx/logging.c | file | annotate | diff | comparison | revisions | |
ucx/logging.h | file | annotate | diff | comparison | revisions |
1.1 --- a/test/Makefile Mon Oct 08 12:29:27 2012 +0200 1.2 +++ b/test/Makefile Mon Oct 08 14:04:52 2012 +0200 1.3 @@ -28,7 +28,13 @@ 1.4 1.5 include ../$(CONF).mk 1.6 1.7 -SRC = main.c list_tests.c dlist_tests.c mpool_tests.c map_tests.c string_tests.c 1.8 +SRC = main.c 1.9 +SRC += list_tests.c 1.10 +SRC += dlist_tests.c 1.11 +SRC += mpool_tests.c 1.12 +SRC += map_tests.c 1.13 +SRC += string_tests.c 1.14 +SRC += logging_tests.c 1.15 1.16 OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT)) 1.17
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/logging_tests.c Mon Oct 08 14:04:52 2012 +0200 2.3 @@ -0,0 +1,38 @@ 2.4 +/* 2.5 + * 2.6 + */ 2.7 + 2.8 +#include "logging_tests.h" 2.9 +#ifndef _WIN32 2.10 +#include <unistd.h> 2.11 +#endif /* not _WIN32 */ 2.12 + 2.13 +UCX_TEST_IMPLEMENT(test_ucx_logger_log) { 2.14 + char buffer[100]; 2.15 +#if defined _USE_GNU || defined _USE_XOPEN2K8 2.16 + FILE *stream = fmemopen(buffer, 100, "w"); 2.17 +#else 2.18 + FILE *stream = fopen("test_ucx_logger", "w+"); 2.19 +#endif /* _WIN32 */ 2.20 + UcxLogger *logger = ucx_logger_new(stream, UCX_LOGGER_INFO); 2.21 + 2.22 + UCX_TEST_BEGIN 2.23 + ucx_logger_info(logger, ST("[INFO:] allright\n")); 2.24 + ucx_logger_trace(logger, ST("[TRACE:] dont log this!\n")); 2.25 + ucx_logger_error(logger, ST("[ERROR:] error!\n")); 2.26 +#if !(defined _USE_GNU || defined _USE_XOPEN2K8) 2.27 + fseek(stream, 0, SEEK_SET); 2.28 + fread(buffer, 1, 100, stream); 2.29 +#endif /* _WIN32 */ 2.30 + 2.31 + UCX_TEST_ASSERT(strncmp(buffer, 2.32 + "[INFO:] allright\n[ERROR:] error!\n", 33) == 0, "incorrect logs"); 2.33 + 2.34 + UCX_TEST_END 2.35 + 2.36 + free(logger); 2.37 + fclose(stream); 2.38 +#if !(defined _USE_GNU || defined _USE_XOPEN2K8) 2.39 + unlink("test_ucx_logger"); 2.40 +#endif 2.41 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/logging_tests.h Mon Oct 08 14:04:52 2012 +0200 3.3 @@ -0,0 +1,22 @@ 3.4 +/* 3.5 + * 3.6 + */ 3.7 + 3.8 +#ifndef LOGGING_TESTS_H 3.9 +#define LOGGING_TESTS_H 3.10 + 3.11 +#include "ucx/test.h" 3.12 +#include "ucx/logging.h" 3.13 + 3.14 +#ifdef __cplusplus 3.15 +extern "C" { 3.16 +#endif 3.17 + 3.18 +UCX_TEST_DECLARE(test_ucx_logger_log) 3.19 + 3.20 +#ifdef __cplusplus 3.21 +} 3.22 +#endif 3.23 + 3.24 +#endif /* LOGGING_TESTS_H */ 3.25 +
4.1 --- a/test/main.c Mon Oct 08 12:29:27 2012 +0200 4.2 +++ b/test/main.c Mon Oct 08 14:04:52 2012 +0200 4.3 @@ -33,6 +33,7 @@ 4.4 4.5 #include "main.h" 4.6 4.7 +#include "logging_tests.h" 4.8 #include "list_tests.h" 4.9 #include "dlist_tests.h" 4.10 #include "string_tests.h" 4.11 @@ -106,6 +107,9 @@ 4.12 4.13 printf("\nLibrary function tests\n"); 4.14 suite = ucx_test_suite_new(); 4.15 + /* UcxLogger Tests */ 4.16 + ucx_test_register(suite, test_ucx_logger_log); 4.17 + 4.18 /* UcxList Tests */ 4.19 ucx_test_register(suite, test_ucx_list_append); 4.20 ucx_test_register(suite, test_ucx_list_prepend);
5.1 --- a/test/string_tests.h Mon Oct 08 12:29:27 2012 +0200 5.2 +++ b/test/string_tests.h Mon Oct 08 14:04:52 2012 +0200 5.3 @@ -20,5 +20,5 @@ 5.4 } 5.5 #endif 5.6 5.7 -#endif /* MPOOL_TESTS_H */ 5.8 +#endif /* STRING_TESTS_H */ 5.9
6.1 --- a/ucx/Makefile Mon Oct 08 12:29:27 2012 +0200 6.2 +++ b/ucx/Makefile Mon Oct 08 14:04:52 2012 +0200 6.3 @@ -29,7 +29,14 @@ 6.4 include ../$(CONF).mk 6.5 6.6 # list of source files 6.7 -SRC = list.c dlist.c map.c mempool.c string.c test.c allocator.c 6.8 +SRC = list.c 6.9 +SRC += dlist.c 6.10 +SRC += map.c 6.11 +SRC += mempool.c 6.12 +SRC += string.c 6.13 +SRC += test.c 6.14 +SRC += allocator.c 6.15 +SRC += logging.c 6.16 6.17 OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT)) 6.18
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/ucx/logging.c Mon Oct 08 14:04:52 2012 +0200 7.3 @@ -0,0 +1,19 @@ 7.4 +#include "logging.h" 7.5 +#include <stdlib.h> 7.6 + 7.7 +UcxLogger *ucx_logger_new(FILE *stream, unsigned int level) { 7.8 + UcxLogger *logger = (UcxLogger*) malloc(sizeof(UcxLogger)); 7.9 + if (logger != NULL) { 7.10 + logger->stream = stream; 7.11 + logger->level = level; 7.12 + } 7.13 + 7.14 + return logger; 7.15 +} 7.16 + 7.17 +void ucx_logger_log(UcxLogger *logger, unsigned int level, sstr_t message) { 7.18 + if (level <= logger->level) { 7.19 + fwrite(message.ptr, 1, message.length, logger->stream); 7.20 + fflush(logger->stream); 7.21 + } 7.22 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/ucx/logging.h Mon Oct 08 14:04:52 2012 +0200 8.3 @@ -0,0 +1,35 @@ 8.4 +#ifndef LOGGING_H 8.5 +#define LOGGING_H 8.6 + 8.7 +#include "string.h" 8.8 +#include <stdio.h> 8.9 + 8.10 +#ifdef __cplusplus 8.11 +extern "C" { 8.12 +#endif 8.13 + 8.14 +/* leave enough space for custom log levels */ 8.15 +#define UCX_LOGGER_ERROR 0x00 8.16 +#define UCX_LOGGER_WARN 0x10 8.17 +#define UCX_LOGGER_INFO 0x20 8.18 +#define UCX_LOGGER_TRACE 0x30 8.19 + 8.20 +typedef struct { 8.21 + FILE *stream; 8.22 + unsigned int level; 8.23 +} UcxLogger; 8.24 + 8.25 +UcxLogger *ucx_logger_new(FILE *stream, unsigned int level); 8.26 +/* neither provide a free function nor a parameter for an allocator */ 8.27 + 8.28 +void ucx_logger_log(UcxLogger *logger, unsigned int level, sstr_t message); 8.29 +#define ucx_logger_error(l,m) ucx_logger_log(l, UCX_LOGGER_ERROR, m) 8.30 +#define ucx_logger_info(l,m) ucx_logger_log(l, UCX_LOGGER_INFO, m) 8.31 +#define ucx_logger_warn(l,m) ucx_logger_log(l, UCX_LOGGER_WARN, m) 8.32 +#define ucx_logger_trace(l,m) ucx_logger_log(l, UCX_LOGGER_TRACE, m) 8.33 + 8.34 +#ifdef __cplusplus 8.35 +} 8.36 +#endif 8.37 + 8.38 +#endif /* LOGGING_H */