added logging API

Mon, 08 Oct 2012 14:04:52 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 08 Oct 2012 14:04:52 +0200
changeset 54
f634f790661a
parent 53
e533c170bfb8
child 55
180bc6b18fec

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
--- a/test/Makefile	Mon Oct 08 12:29:27 2012 +0200
+++ b/test/Makefile	Mon Oct 08 14:04:52 2012 +0200
@@ -28,7 +28,13 @@
 
 include ../$(CONF).mk
 
-SRC = main.c list_tests.c dlist_tests.c mpool_tests.c map_tests.c string_tests.c
+SRC  = main.c
+SRC += list_tests.c
+SRC += dlist_tests.c
+SRC += mpool_tests.c
+SRC += map_tests.c
+SRC += string_tests.c
+SRC += logging_tests.c
 
 OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT))
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/logging_tests.c	Mon Oct 08 14:04:52 2012 +0200
@@ -0,0 +1,38 @@
+/*
+ *
+ */
+
+#include "logging_tests.h"
+#ifndef _WIN32
+#include <unistd.h>
+#endif /* not _WIN32 */
+
+UCX_TEST_IMPLEMENT(test_ucx_logger_log) {
+    char buffer[100];
+#if defined _USE_GNU || defined _USE_XOPEN2K8
+    FILE *stream = fmemopen(buffer, 100, "w");
+#else
+    FILE *stream = fopen("test_ucx_logger", "w+");
+#endif /* _WIN32 */
+    UcxLogger *logger = ucx_logger_new(stream, UCX_LOGGER_INFO);
+    
+    UCX_TEST_BEGIN
+    ucx_logger_info(logger, ST("[INFO:] allright\n"));
+    ucx_logger_trace(logger, ST("[TRACE:] dont log this!\n"));
+    ucx_logger_error(logger, ST("[ERROR:] error!\n"));
+#if !(defined _USE_GNU || defined _USE_XOPEN2K8)
+    fseek(stream, 0, SEEK_SET);
+    fread(buffer, 1, 100, stream);
+#endif /* _WIN32 */
+
+    UCX_TEST_ASSERT(strncmp(buffer,
+            "[INFO:] allright\n[ERROR:] error!\n", 33) == 0, "incorrect logs");
+
+    UCX_TEST_END
+
+    free(logger);
+    fclose(stream);
+#if !(defined _USE_GNU || defined _USE_XOPEN2K8)
+    unlink("test_ucx_logger");
+#endif
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/logging_tests.h	Mon Oct 08 14:04:52 2012 +0200
@@ -0,0 +1,22 @@
+/* 
+ *
+ */
+
+#ifndef LOGGING_TESTS_H
+#define	LOGGING_TESTS_H
+
+#include "ucx/test.h"
+#include "ucx/logging.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+UCX_TEST_DECLARE(test_ucx_logger_log)
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* LOGGING_TESTS_H */
+
--- a/test/main.c	Mon Oct 08 12:29:27 2012 +0200
+++ b/test/main.c	Mon Oct 08 14:04:52 2012 +0200
@@ -33,6 +33,7 @@
 
 #include "main.h"
 
+#include "logging_tests.h"
 #include "list_tests.h"
 #include "dlist_tests.h"
 #include "string_tests.h"
@@ -106,6 +107,9 @@
 
         printf("\nLibrary function tests\n");
         suite = ucx_test_suite_new();
+        /* UcxLogger Tests */
+        ucx_test_register(suite, test_ucx_logger_log);
+
         /* UcxList Tests */
         ucx_test_register(suite, test_ucx_list_append);
         ucx_test_register(suite, test_ucx_list_prepend);
--- a/test/string_tests.h	Mon Oct 08 12:29:27 2012 +0200
+++ b/test/string_tests.h	Mon Oct 08 14:04:52 2012 +0200
@@ -20,5 +20,5 @@
 }
 #endif
 
-#endif	/* MPOOL_TESTS_H */
+#endif	/* STRING_TESTS_H */
 
--- a/ucx/Makefile	Mon Oct 08 12:29:27 2012 +0200
+++ b/ucx/Makefile	Mon Oct 08 14:04:52 2012 +0200
@@ -29,7 +29,14 @@
 include ../$(CONF).mk
 
 # list of source files
-SRC = list.c dlist.c map.c mempool.c string.c test.c allocator.c
+SRC  = list.c 
+SRC += dlist.c
+SRC += map.c
+SRC += mempool.c
+SRC += string.c
+SRC += test.c
+SRC += allocator.c
+SRC += logging.c
 
 OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT))
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ucx/logging.c	Mon Oct 08 14:04:52 2012 +0200
@@ -0,0 +1,19 @@
+#include "logging.h"
+#include <stdlib.h>
+
+UcxLogger *ucx_logger_new(FILE *stream, unsigned int level) {
+    UcxLogger *logger = (UcxLogger*) malloc(sizeof(UcxLogger));
+    if (logger != NULL) {
+        logger->stream = stream;
+        logger->level = level;
+    }
+
+    return logger;
+}
+
+void ucx_logger_log(UcxLogger *logger, unsigned int level, sstr_t message) {
+    if (level <= logger->level) {
+        fwrite(message.ptr, 1, message.length, logger->stream);
+        fflush(logger->stream);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ucx/logging.h	Mon Oct 08 14:04:52 2012 +0200
@@ -0,0 +1,35 @@
+#ifndef LOGGING_H
+#define LOGGING_H
+
+#include "string.h"
+#include <stdio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* leave enough space for custom log levels */
+#define UCX_LOGGER_ERROR    0x00
+#define UCX_LOGGER_WARN     0x10
+#define UCX_LOGGER_INFO     0x20
+#define UCX_LOGGER_TRACE    0x30
+
+typedef struct {
+    FILE *stream;
+    unsigned int level;
+} UcxLogger;
+
+UcxLogger *ucx_logger_new(FILE *stream, unsigned int level);
+/* neither provide a free function nor a parameter for an allocator */
+
+void ucx_logger_log(UcxLogger *logger, unsigned int level, sstr_t message);
+#define ucx_logger_error(l,m) ucx_logger_log(l, UCX_LOGGER_ERROR, m)
+#define ucx_logger_info(l,m) ucx_logger_log(l, UCX_LOGGER_INFO, m)
+#define ucx_logger_warn(l,m) ucx_logger_log(l, UCX_LOGGER_WARN, m)
+#define ucx_logger_trace(l,m) ucx_logger_log(l, UCX_LOGGER_TRACE, m)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* LOGGING_H */

mercurial