]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/log.c
Logging: Adapted the `logger' utility to the new numbering of levels.
[libucw.git] / ucw / log.c
index afda7a0ca05c3521e9733d51a2e97d9e2eb119d9..5c4a006982f863b760e9309d34deb51eaaa05e09 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,19 +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;
 }
 
 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,
@@ -65,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 : LOG_STREAM_DEFAULT);
+    return (n ? NULL : &log_stream_default);
   return log_streams.ptr[n];
 }
 
@@ -88,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 = 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);
@@ -140,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, LOG_STREAM_DEFAULT, &m);
+      log_pass_msg(0, &log_stream_default, &m);
     }
 
   if (m.raw_msg != msgbuf)
@@ -159,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, LOG_STREAM_DEFAULT, &errm);
+      log_pass_msg(0, &log_stream_default, &errm);
     }
 
   /* Filter by level and hook function */
@@ -219,7 +219,7 @@ log_pass_msg(int depth, struct log_stream *ls, struct log_msg *m)
   /* Process name, PID ( |log_title| + 6 + (|PID|<=10) chars ) */
   if ((ls->msgfmt & LSFMT_TITLE) && log_title)
     {
-      if (ls->msgfmt & LSFMT_PID)
+      if ((ls->msgfmt & LSFMT_PID) && log_pid)
        p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
       else
        p += sprintf(p, "[%s] ", log_title);
@@ -332,8 +332,11 @@ log_fork(void)
 
 int main(void)
 {
-  struct log_stream *ls = log_new_syslog(LOG_USER, "syslog");
+  struct log_stream *ls = log_new_syslog("local3", 0);
   msg(L_INFO | ls->regnum, "Brum <%300s>", ":-)");
+  log_set_format(log_default_stream(), ~0U, LSFMT_USEC);
+  msg(L_INFO, "Brum <%300s>", ":-)");
+  log_close_all();
   return 0;
 }