]> mj.ucw.cz Git - libucw.git/blobdiff - lib/log.c
- obj_parse_dump() generalized to store apart the header and the body, and
[libucw.git] / lib / log.c
index 55431cc1fb06e2985fcb90d2ceebb544c4d06ab6..cad127f47bb241a1ab7fb2c850ff046c24879226 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -1,49 +1,84 @@
 /*
  *     Sherlock Library -- Logging
  *
- *     (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
+ *     (c) 1997--2004 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
  */
 
+#include "lib/lib.h"
+
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdarg.h>
+#include <string.h>
 #include <unistd.h>
-#include <sys/time.h>
-
-#include "lib.h"
+#include <time.h>
+#include <alloca.h>
 
-static byte *progname = "???";
-static pid_t pid;
+static char log_progname[32];
+char *log_filename;
+char *log_title;
+int log_pid;
+void (*log_die_hook)(void);
+void (*log_switch_hook)(struct tm *tm);
 
-static void
-logit(int level, byte *msg, va_list args)
+void
+vlog_msg(unsigned int cat, const char *msg, va_list args)
 {
-  time_t tim;
-  struct tm *tm;
-  char buf[32];
+  time_t tim = time(NULL);
+  struct tm *tm = localtime(&tim);
+  byte *buf, *p;
+  int buflen = 256;
+  int l, l0, r;
 
-  tim = time(NULL);
-  tm = localtime(&tim);
-  strftime(buf, sizeof(buf), "%d-%m-%y %H:%M:%S", tm);
-  fprintf(stderr, "%s %s [%d] <%d> ", buf, progname, pid, level);
-  vfprintf(stderr, msg, args);
-  fputc('\n', stderr);
-  fflush(stderr);
+  if (log_switch_hook)
+    log_switch_hook(tm);
+  while (1)
+    {
+      p = buf = alloca(buflen);
+      *p++ = cat;
+      p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", tm);
+      if (log_title)
+       {
+         if (log_pid)
+           p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
+         else
+           p += sprintf(p, "[%s] ", log_title);
+       }
+      else
+       {
+         if (log_pid)
+           p += sprintf(p, "[%d] ", log_pid);
+       }
+      l0 = p - buf + 1;
+      r = buflen - l0;
+      l = vsnprintf(p, r, msg, args);
+      if (l < 0)
+       l = r;
+      else if (l < r)
+       {
+         while (*p)
+           {
+             if (*p < 0x20 && *p != '\t')
+               *p = 0x7f;
+             p++;
+           }
+         *p = '\n';
+         write(2, buf, l + l0);
+         return;
+       }
+      buflen = l + l0 + 1;
+    }
 }
 
 void
-log(byte *msg, ...)
+log_msg(unsigned int cat, const char *msg, ...)
 {
-  int level = 2;
   va_list args;
 
   va_start(args, msg);
-  if (msg[0] == '<' && msg[1] >= '0' && msg[1] <= '9' && msg[2] == '>')
-       {
-         level = msg[1] - '0';
-         msg += 3;
-       }
-  logit(level, msg, args);
+  vlog_msg(cat, msg, args);
   va_end(args);
 }
 
@@ -53,25 +88,50 @@ die(byte *msg, ...)
   va_list args;
 
   va_start(args, msg);
-  logit(9, msg, args);
+  vlog_msg(L_FATAL, msg, args);
   va_end(args);
-  exit(99);
+  if (log_die_hook)
+    log_die_hook();
+#ifdef DEBUG_DIE_BY_ABORT
+  abort();
+#else
+  exit(1);
+#endif
+}
+
+#ifdef DEBUG
+void
+assert_failed(char *assertion, char *file, int line)
+{
+  log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
+  abort();
+}
+#else
+void
+assert_failed(void)
+{
+  die("Internal error: Assertion failed.");
 }
+#endif
 
 static byte *
-basename(byte *n)
+log_basename(byte *n)
 {
   byte *p = n;
 
   while (*n)
-       if (*n++ == '/')
-         p = n;
+    if (*n++ == '/')
+      p = n;
   return p;
 }
 
 void
-initlog(byte *argv0)
+log_init(byte *argv0)
 {
-  progname = basename(argv0);
-  pid = getpid();
+  if (argv0)
+    {
+      strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
+      log_progname[sizeof(log_progname)-1] = 0;
+      log_title = log_progname;
+    }
 }