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
     1.1 --- a/ucx/logging.c	Fri Feb 08 10:37:24 2013 +0100
     1.2 +++ b/ucx/logging.c	Fri Feb 08 11:25:04 2013 +0100
     1.3 @@ -2,12 +2,14 @@
     1.4  #include <stdlib.h>
     1.5  #include <string.h>
     1.6  #include <stdarg.h>
     1.7 +#include <time.h>
     1.8  
     1.9  UcxLogger *ucx_logger_new(FILE *stream, unsigned int level, unsigned int mask) {
    1.10      UcxLogger *logger = (UcxLogger*) malloc(sizeof(UcxLogger));
    1.11      if (logger != NULL) {
    1.12          logger->stream = stream;
    1.13 -        logger->writer = fwrite;
    1.14 +        logger->writer = (write_func)fwrite;
    1.15 +        logger->dateformat = "%F %T %z ";
    1.16          logger->level = level;
    1.17          logger->mask = mask;
    1.18          logger->levels = ucx_map_new(8);
    1.19 @@ -31,16 +33,6 @@
    1.20      free(logger);
    1.21  }
    1.22  
    1.23 -void ucx_logger_setoutput(UcxLogger *logger, FILE *stream,
    1.24 -        size_t(*writer)(const void*,size_t,size_t,FILE*)) {
    1.25 -    if (stream) {
    1.26 -        logger->stream = stream;
    1.27 -    }
    1.28 -    if (writer) {
    1.29 -        logger->writer = writer;
    1.30 -    }
    1.31 -}
    1.32 -
    1.33  void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file,
    1.34          const unsigned int line, const char *format, ...) {
    1.35      if (level <= logger->level) {
    1.36 @@ -57,7 +49,10 @@
    1.37              k += n;
    1.38              msg[k++] = ' ';
    1.39          }
    1.40 -        // TODO: timestamp
    1.41 +        if ((logger->mask & UCX_LOGGER_TIMESTAMP) > 0) {
    1.42 +            time_t now = time(NULL);
    1.43 +            k += strftime(msg+k, 128, logger->dateformat, localtime(&now));
    1.44 +        }
    1.45          if ((logger->mask & UCX_LOGGER_SOURCE) > 0) {
    1.46              n = strlen(file);
    1.47              memcpy(msg+k, file, n);
     2.1 --- a/ucx/logging.h	Fri Feb 08 10:37:24 2013 +0100
     2.2 +++ b/ucx/logging.h	Fri Feb 08 11:25:04 2013 +0100
     2.3 @@ -22,7 +22,8 @@
     2.4  
     2.5  typedef struct {
     2.6      FILE *stream;
     2.7 -    size_t(*writer)(const void*, size_t, size_t, FILE*);
     2.8 +    write_func writer;
     2.9 +    char *dateformat;
    2.10      unsigned int level;
    2.11      unsigned int mask;
    2.12      UcxMap* levels;
    2.13 @@ -31,18 +32,6 @@
    2.14  UcxLogger *ucx_logger_new(FILE *stream, unsigned int level, unsigned int mask);
    2.15  void ucx_logger_free(UcxLogger* logger);
    2.16  
    2.17 -/**
    2.18 - * Sets the output stream and writer for this logger.
    2.19 - * The parameters stream and writer may be NULL, if they shall remain unchanged
    2.20 - * in the logger.
    2.21 - * l
    2.22 - * @param logger The logger
    2.23 - * @param stream The stream the logger shall log to
    2.24 - * @param writer A pointer to the write function
    2.25 - */
    2.26 -void ucx_logger_setoutput(UcxLogger *logger, FILE *stream,
    2.27 -        size_t(*writer)(const void*,size_t,size_t,FILE*));
    2.28 -
    2.29  void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file,
    2.30          const unsigned int line, const char* format, ...);
    2.31  #define ucx_logger_log(logger, level, format...) \

mercurial