documents (and fixes!) the UcxLogger

Fri, 11 May 2018 19:48:19 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 11 May 2018 19:48:19 +0200
changeset 295
7fc65395188e
parent 294
bfa935ab7f85
child 296
e325716a442c

documents (and fixes!) the UcxLogger

docs/src/modules.md file | annotate | diff | comparison | revisions
src/logging.c file | annotate | diff | comparison | revisions
src/ucx/logging.h file | annotate | diff | comparison | revisions
     1.1 --- a/docs/src/modules.md	Fri May 11 18:46:31 2018 +0200
     1.2 +++ b/docs/src/modules.md	Fri May 11 19:48:19 2018 +0200
     1.3 @@ -246,7 +246,29 @@
     1.4  The logging module comes with some predefined log levels and allows some more
     1.5  customization. You may choose if you want to get timestamps or source file and
     1.6  line number logged automatically when outputting a message.
     1.7 -
     1.8 +The following function call initializes a debug logger with all of the above
     1.9 +information:
    1.10 +```C
    1.11 +    log = ucx_logger_new(stdout, UCX_LOGGER_DEBUG,
    1.12 +            UCX_LOGGER_LEVEL | UCX_LOGGER_TIMESTAMP | UCX_LOGGER_SOURCE);
    1.13 +```
    1.14 +Afterwards you can use this logger with the predefined macros
    1.15 +```C
    1.16 +    ucx_logger_trace(log, "Verbose output");
    1.17 +    ucx_logger_debug(log, "Debug message");
    1.18 +    ucx_logger_info(log, "Information");
    1.19 +    ucx_logger_warn(log, "Warning");
    1.20 +    ucx_logger_error(log, "Error message");
    1.21 +```
    1.22 +or you use
    1.23 +```C
    1.24 +    ucx_logger_log(log, CUSTOM_LEVEL, "Some message")
    1.25 +```
    1.26 +When you use your custom log level, don't forget to register it with
    1.27 +```C
    1.28 +    ucx_logger_register_level(log, CUSTOM_LEVEL, "CUSTOM")
    1.29 +```
    1.30 +where the last argument must be a string literal.
    1.31  
    1.32  ## Map
    1.33  
    1.34 @@ -308,6 +330,7 @@
    1.35  ucx_map_free(myprops);
    1.36  fclose(file);
    1.37  ```
    1.38 +
    1.39  ## Stack
    1.40  
    1.41  *Header file:* [stack.h](api/stack_8h.html)  
     2.1 --- a/src/logging.c	Fri May 11 18:46:31 2018 +0200
     2.2 +++ b/src/logging.c	Fri May 11 19:48:19 2018 +0200
     2.3 @@ -50,6 +50,8 @@
     2.4          ucx_map_int_put(logger->levels, l, (void*) "[WARNING]");
     2.5          l = UCX_LOGGER_INFO;
     2.6          ucx_map_int_put(logger->levels, l, (void*) "[INFO]");
     2.7 +        l = UCX_LOGGER_DEBUG;
     2.8 +        ucx_map_int_put(logger->levels, l, (void*) "[DEBUG]");
     2.9          l = UCX_LOGGER_TRACE;
    2.10          ucx_map_int_put(logger->levels, l, (void*) "[TRACE]");
    2.11      }
    2.12 @@ -75,6 +77,9 @@
    2.13          
    2.14          if ((logger->mask & UCX_LOGGER_LEVEL) > 0) {
    2.15              text = (char*) ucx_map_int_get(logger->levels, level);
    2.16 +            if (!text) {
    2.17 +                text = "[UNKNOWN]";
    2.18 +            }
    2.19              n = strlen(text);
    2.20              n = n > 256 ? 256 : n;
    2.21              memcpy(msg+k, text, n);
     3.1 --- a/src/ucx/logging.h	Fri May 11 18:46:31 2018 +0200
     3.2 +++ b/src/ucx/logging.h	Fri May 11 19:48:19 2018 +0200
     3.3 @@ -177,6 +177,18 @@
     3.4          const unsigned int line, const char* format, ...);
     3.5  
     3.6  /**
     3.7 + * Registers a custom log level.
     3.8 + * @param logger the logger
     3.9 + * @param level the log level as unsigned integer
    3.10 + * @param name a string literal describing the level
    3.11 + */
    3.12 +#define ucx_logger_register_level(logger, level, name) {\
    3.13 +        unsigned int l; \
    3.14 +            l = level; \
    3.15 +            ucx_map_int_put(logger->levels, l, (void*) "[" name "]"); \
    3.16 +        } while (0);
    3.17 +
    3.18 +/**
    3.19   * Logs a message at the specified level.
    3.20   * @param logger the logger to use
    3.21   * @param level the level to log the message on

mercurial