]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/log.c
Heap: Interface cleanup
[libucw.git] / ucw / log.c
index 35a0cc8392d8a62e2400f89ec1ef1986cba3ccbf..cf5d115ad78ee9aae4dd25e9f370fb3e0f5335e6 100644 (file)
--- a/ucw/log.c
+++ b/ucw/log.c
@@ -8,10 +8,10 @@
  *     of the GNU Lesser General Public License.
  */
 
-#include "ucw/lib.h"
-#include "ucw/log.h"
-#include "ucw/log-internal.h"
-#include "ucw/simple-lists.h"
+#include <ucw/lib.h>
+#include <ucw/log.h>
+#include <ucw/log-internal.h>
+#include <ucw/simple-lists.h>
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -45,8 +45,10 @@ struct log_stream log_stream_default = {
   .types = ~0U,
   .msgfmt = LSFMT_DEFAULT,
   // an empty clist
-  .substreams.head.next = (cnode *) &log_stream_default.substreams.head,
-  .substreams.head.prev = (cnode *) &log_stream_default.substreams.head,
+  .substreams.head = {
+    .next = (cnode *) &log_stream_default.substreams.head,
+    .prev = (cnode *) &log_stream_default.substreams.head,
+  },
 };
 
 /*** Registry of streams and their identifiers ***/
@@ -100,21 +102,25 @@ vmsg(uns cat, const char *fmt, va_list args)
   char msgbuf[256];
   char *p;
   int len;
-  struct log_stream *ls = log_stream_by_flags(cat);
+  uns sighandler = cat & L_SIGHANDLER;
+  struct log_stream *ls;
   struct log_msg m = { .flags = cat };
 
-  /* Check the stream existence */
-  if (!ls)
+  /* Find the destination stream */
+  if (sighandler)
+    ls = &log_stream_default;
+  else if (!(ls = log_stream_by_flags(cat)))
     {
       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 & L_SIGHANDLER))
+  if (!sighandler)
     {
       /* CAVEAT: These calls are not safe in signal handlers. */
       gettimeofday(&tv, NULL);
+      m.tv = &tv;
       if (localtime_r(&tv.tv_sec, &tm))
        m.tm = &tm;
     }
@@ -137,7 +143,7 @@ vmsg(uns cat, const char *fmt, va_list args)
   va_copy(args2, args);
   len = vsnprintf(msgbuf, sizeof(msgbuf), fmt, args2);
   va_end(args2);
-  if (len < (int) sizeof(msgbuf))
+  if (len < (int) sizeof(msgbuf) || sighandler)
     m.raw_msg = msgbuf;
   else
     {
@@ -149,7 +155,7 @@ vmsg(uns cat, const char *fmt, va_list args)
   p = m.raw_msg;
   while (*p)
     {
-      if (*p < 0x20 && *p != '\t')
+      if (*p >= 0 && *p < 0x20 && *p != '\t')
        *p = 0x7f;
       p++;
     }
@@ -240,7 +246,7 @@ log_pass_msg(int depth, struct log_stream *ls, struct log_msg *m)
 
   /* Get a buffer and format the message */
   char *free_buf = NULL;
-  if (len <= 256)
+  if (len <= 256 || (m->flags & L_SIGHANDLER))
     m->m = alloca(len);
   else
     m->m = free_buf = xmalloc(len);
@@ -292,7 +298,7 @@ log_pass_msg(int depth, struct log_stream *ls, struct log_msg *m)
     }
 
   /* Message type ( |type| + 3 chars ) */
-  if (ls->msgfmt & LSFMT_TYPE)
+  if (type)
     p += sprintf(p, "{%s} ", type);
 
   /* The message itself ( |m| + 1 chars ) */
@@ -336,18 +342,22 @@ do_die(void)
 }
 
 void
-die(const char *fmt, ...)
+vdie(const char *fmt, va_list args)
 {
-  va_list args;
-
-  va_start(args, fmt);
   vmsg(L_FATAL, fmt, args);
-  va_end(args);
   if (log_die_hook)
     log_die_hook();
   do_die();
 }
 
+void
+die(const char *fmt, ...)
+{
+  va_list args;
+  va_start(args, fmt);
+  vdie(fmt, args);
+}
+
 void
 assert_failed(const char *assertion, const char *file, int line)
 {