logger can now log timestamps

Fri, 08 Feb 2013 11:25:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 08 Feb 2013 11:25:04 +0100
changeset 82
6068d965328b
parent 81
86a23238d8a1
child 83
3b552d7a9610

logger can now log timestamps

ucx/logging.c file | annotate | diff | comparison | revisions
ucx/logging.h file | annotate | diff | comparison | revisions
--- a/ucx/logging.c	Fri Feb 08 10:37:24 2013 +0100
+++ b/ucx/logging.c	Fri Feb 08 11:25:04 2013 +0100
@@ -2,12 +2,14 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
+#include <time.h>
 
 UcxLogger *ucx_logger_new(FILE *stream, unsigned int level, unsigned int mask) {
     UcxLogger *logger = (UcxLogger*) malloc(sizeof(UcxLogger));
     if (logger != NULL) {
         logger->stream = stream;
-        logger->writer = fwrite;
+        logger->writer = (write_func)fwrite;
+        logger->dateformat = "%F %T %z ";
         logger->level = level;
         logger->mask = mask;
         logger->levels = ucx_map_new(8);
@@ -31,16 +33,6 @@
     free(logger);
 }
 
-void ucx_logger_setoutput(UcxLogger *logger, FILE *stream,
-        size_t(*writer)(const void*,size_t,size_t,FILE*)) {
-    if (stream) {
-        logger->stream = stream;
-    }
-    if (writer) {
-        logger->writer = writer;
-    }
-}
-
 void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file,
         const unsigned int line, const char *format, ...) {
     if (level <= logger->level) {
@@ -57,7 +49,10 @@
             k += n;
             msg[k++] = ' ';
         }
-        // TODO: timestamp
+        if ((logger->mask & UCX_LOGGER_TIMESTAMP) > 0) {
+            time_t now = time(NULL);
+            k += strftime(msg+k, 128, logger->dateformat, localtime(&now));
+        }
         if ((logger->mask & UCX_LOGGER_SOURCE) > 0) {
             n = strlen(file);
             memcpy(msg+k, file, n);
--- a/ucx/logging.h	Fri Feb 08 10:37:24 2013 +0100
+++ b/ucx/logging.h	Fri Feb 08 11:25:04 2013 +0100
@@ -22,7 +22,8 @@
 
 typedef struct {
     FILE *stream;
-    size_t(*writer)(const void*, size_t, size_t, FILE*);
+    write_func writer;
+    char *dateformat;
     unsigned int level;
     unsigned int mask;
     UcxMap* levels;
@@ -31,18 +32,6 @@
 UcxLogger *ucx_logger_new(FILE *stream, unsigned int level, unsigned int mask);
 void ucx_logger_free(UcxLogger* logger);
 
-/**
- * Sets the output stream and writer for this logger.
- * The parameters stream and writer may be NULL, if they shall remain unchanged
- * in the logger.
- * l
- * @param logger The logger
- * @param stream The stream the logger shall log to
- * @param writer A pointer to the write function
- */
-void ucx_logger_setoutput(UcxLogger *logger, FILE *stream,
-        size_t(*writer)(const void*,size_t,size_t,FILE*));
-
 void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file,
         const unsigned int line, const char* format, ...);
 #define ucx_logger_log(logger, level, format...) \

mercurial