]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/log.c
Logging: Documentation and minor cleanups of headers.
[libucw.git] / ucw / log.c
index f3c6aa146f5cc61a29cfdb403226669af356ad77..7761401b8ac4ca49fdc7a7a91c102927ad670611 100644 (file)
--- a/ucw/log.c
+++ b/ucw/log.c
@@ -10,6 +10,7 @@
 
 #include "ucw/lib.h"
 #include "ucw/log.h"
+#include "ucw/log-internal.h"
 #include "ucw/simple-lists.h"
 
 #include <stdio.h>
@@ -27,18 +28,18 @@ void (*log_die_hook)(void);
 
 /*** The default log stream, which logs to stderr ***/
 
-static int default_log_handler(struct log_stream *ls, struct log_msg *m)
+static int default_log_handler(struct log_stream *ls UNUSED, struct log_msg *m)
 {
   // This is a completely bare version of the log-file module. Errors are ignored.
-  write(ls->idata, m->m, m->m_len);
+  write(2, m->m, m->m_len);
   return 0;
 }
 
-const struct log_stream log_stream_default = {
+struct log_stream log_stream_default = {
   .name = "stderr",
-  .idata = 2,
+  .use_count = 1000000,
   .handler = default_log_handler,
-  .levels = LS_ALL_LEVELS,
+  .levels = ~0U,
   .msgfmt = LSFMT_DEFAULT,
   // an empty clist
   .substreams.head.next = (cnode *) &log_stream_default.substreams.head,
@@ -64,7 +65,7 @@ log_stream_by_flags(uns flags)
 {
   int n = LS_GET_STRNUM(flags);
   if (n < 0 || n >= log_streams_after || log_streams.ptr[n]->regnum == -1)
-    return (n ? NULL : (struct log_stream *) &log_stream_default);
+    return (n ? NULL : &log_stream_default);
   return log_streams.ptr[n];
 }
 
@@ -87,12 +88,12 @@ vmsg(uns cat, const char *fmt, va_list args)
   /* Check the stream existence */
   if (!ls)
     {
-      msg((LS_INTERNAL_MASK&cat)|L_WARN, "No log_stream with number %d! Logging to the default log.", LS_GET_STRNUM(cat));
-      ls = (struct log_stream *) &log_stream_default;
+      msg((LS_CTRL_MASK&cat)|L_WARN, "No log_stream with number %d! Logging to the default log.", LS_GET_STRNUM(cat));
+      ls = &log_stream_default;
     }
 
   /* Get the current time */
-  if (!(cat & LSFLAG_SIGHANDLER))
+  if (!(cat & L_SIGHANDLER))
     {
       /* CAVEAT: These calls are not safe in signal handlers. */
       gettimeofday(&tv, NULL);
@@ -139,7 +140,7 @@ vmsg(uns cat, const char *fmt, va_list args)
   if (log_pass_msg(0, ls, &m))
     {
       /* Error (such as infinite loop) occurred */
-      log_pass_msg(0, (struct log_stream *) &log_stream_default, &m);
+      log_pass_msg(0, &log_stream_default, &m);
     }
 
   if (m.raw_msg != msgbuf)
@@ -158,9 +159,9 @@ log_pass_msg(int depth, struct log_stream *ls, struct log_msg *m)
   if (depth > LS_MAX_DEPTH)
     {
       struct log_msg errm = *m;
-      errm.flags = L_ERROR | (m->flags & LS_INTERNAL_MASK);
+      errm.flags = L_ERROR | (m->flags & LS_CTRL_MASK);
       errm.raw_msg = "Loop in the log_stream system detected.";
-      log_pass_msg(0, (struct log_stream *) &log_stream_default, &errm);
+      log_pass_msg(0, &log_stream_default, &errm);
     }
 
   /* Filter by level and hook function */
@@ -333,6 +334,8 @@ int main(void)
 {
   struct log_stream *ls = log_new_syslog(LOG_USER, "syslog");
   msg(L_INFO | ls->regnum, "Brum <%300s>", ":-)");
+  log_set_format(log_default_stream(), ~0U, LSFMT_USEC);
+  msg(L_INFO, "Brum <%300s>", ":-)");
   return 0;
 }