# HG changeset patch # User Mike Becker # Date 1526060899 -7200 # Node ID 7fc65395188e93cf4299c6d8ad23b10e1a5d72a4 # Parent bfa935ab7f85cc99fe84cfe7e4f257aaf9e2b27d documents (and fixes!) the UcxLogger diff -r bfa935ab7f85 -r 7fc65395188e docs/src/modules.md --- a/docs/src/modules.md Fri May 11 18:46:31 2018 +0200 +++ b/docs/src/modules.md Fri May 11 19:48:19 2018 +0200 @@ -246,7 +246,29 @@ The logging module comes with some predefined log levels and allows some more customization. You may choose if you want to get timestamps or source file and line number logged automatically when outputting a message. - +The following function call initializes a debug logger with all of the above +information: +```C + log = ucx_logger_new(stdout, UCX_LOGGER_DEBUG, + UCX_LOGGER_LEVEL | UCX_LOGGER_TIMESTAMP | UCX_LOGGER_SOURCE); +``` +Afterwards you can use this logger with the predefined macros +```C + ucx_logger_trace(log, "Verbose output"); + ucx_logger_debug(log, "Debug message"); + ucx_logger_info(log, "Information"); + ucx_logger_warn(log, "Warning"); + ucx_logger_error(log, "Error message"); +``` +or you use +```C + ucx_logger_log(log, CUSTOM_LEVEL, "Some message") +``` +When you use your custom log level, don't forget to register it with +```C + ucx_logger_register_level(log, CUSTOM_LEVEL, "CUSTOM") +``` +where the last argument must be a string literal. ## Map @@ -308,6 +330,7 @@ ucx_map_free(myprops); fclose(file); ``` + ## Stack *Header file:* [stack.h](api/stack_8h.html) diff -r bfa935ab7f85 -r 7fc65395188e src/logging.c --- a/src/logging.c Fri May 11 18:46:31 2018 +0200 +++ b/src/logging.c Fri May 11 19:48:19 2018 +0200 @@ -50,6 +50,8 @@ ucx_map_int_put(logger->levels, l, (void*) "[WARNING]"); l = UCX_LOGGER_INFO; ucx_map_int_put(logger->levels, l, (void*) "[INFO]"); + l = UCX_LOGGER_DEBUG; + ucx_map_int_put(logger->levels, l, (void*) "[DEBUG]"); l = UCX_LOGGER_TRACE; ucx_map_int_put(logger->levels, l, (void*) "[TRACE]"); } @@ -75,6 +77,9 @@ if ((logger->mask & UCX_LOGGER_LEVEL) > 0) { text = (char*) ucx_map_int_get(logger->levels, level); + if (!text) { + text = "[UNKNOWN]"; + } n = strlen(text); n = n > 256 ? 256 : n; memcpy(msg+k, text, n); diff -r bfa935ab7f85 -r 7fc65395188e src/ucx/logging.h --- a/src/ucx/logging.h Fri May 11 18:46:31 2018 +0200 +++ b/src/ucx/logging.h Fri May 11 19:48:19 2018 +0200 @@ -177,6 +177,18 @@ const unsigned int line, const char* format, ...); /** + * Registers a custom log level. + * @param logger the logger + * @param level the log level as unsigned integer + * @param name a string literal describing the level + */ +#define ucx_logger_register_level(logger, level, name) {\ + unsigned int l; \ + l = level; \ + ucx_map_int_put(logger->levels, l, (void*) "[" name "]"); \ + } while (0); + +/** * Logs a message at the specified level. * @param logger the logger to use * @param level the level to log the message on