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
--- 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)  
--- 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);
--- 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

mercurial