]> mj.ucw.cz Git - libucw.git/blobdiff - lib/log.c
Merged obj2buck.h and buck2obj.h to object.h, the number of includes
[libucw.git] / lib / log.c
index 447e7ad11f1ed65fc719358e7e553b218753879a..cad127f47bb241a1ab7fb2c850ff046c24879226 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -1,7 +1,7 @@
 /*
  *     Sherlock Library -- Logging
  *
 /*
  *     Sherlock Library -- Logging
  *
- *     (c) 1997--2002 Martin Mares <mj@ucw.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.
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
 
 #include <stdio.h>
 #include <stdlib.h>
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdarg.h>
 #include <string.h>
 #include <string.h>
-#include <fcntl.h>
 #include <unistd.h>
 #include <time.h>
 #include <unistd.h>
 #include <time.h>
+#include <alloca.h>
 
 
-static char *log_progname, *log_name_patt, *log_name;
-static pid_t log_pid;
-static int log_params;
-static int log_name_size;
-static int log_switching;
+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);
 
 void
 
 void
-log_fork(void)
-{
-  log_pid = getpid();
-}
-
-static void
-log_switch(struct tm *tm)
-{
-  int fd, l;
-  char name[log_name_size];
-
-  if (!log_name_patt ||
-      log_name[0] && !log_params ||
-      log_switching)
-    return;
-  log_switching++;
-  l = strftime(name, log_name_size, log_name_patt, tm);
-  if (l < 0 || l >= log_name_size)
-    die("Error formatting log file name: %m");
-  if (strcmp(name, log_name))
-    {
-      strcpy(log_name, name);
-      fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
-      if (fd < 0)
-       die("Unable to open log file %s: %m", name);
-      close(2);
-      dup(fd);
-      close(fd);
-    }
-  log_switching--;
-}
-
-static void
-vlog(unsigned int cat, const char *msg, va_list args)
+vlog_msg(unsigned int cat, const char *msg, va_list args)
 {
   time_t tim = time(NULL);
   struct tm *tm = localtime(&tim);
 {
   time_t tim = time(NULL);
   struct tm *tm = localtime(&tim);
@@ -65,18 +32,19 @@ vlog(unsigned int cat, const char *msg, va_list args)
   int buflen = 256;
   int l, l0, r;
 
   int buflen = 256;
   int l, l0, r;
 
-  log_switch(tm);
+  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);
   while (1)
     {
       p = buf = alloca(buflen);
       *p++ = cat;
       p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", tm);
-      if (log_progname)
+      if (log_title)
        {
          if (log_pid)
        {
          if (log_pid)
-           p += sprintf(p, "[%s (%d)] ", log_progname, log_pid);
+           p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
          else
          else
-           p += sprintf(p, "[%s] ", log_progname);
+           p += sprintf(p, "[%s] ", log_title);
        }
       else
        {
        }
       else
        {
@@ -100,17 +68,17 @@ vlog(unsigned int cat, const char *msg, va_list args)
          write(2, buf, l + l0);
          return;
        }
          write(2, buf, l + l0);
          return;
        }
-      buflen = l + l0;
+      buflen = l + l0 + 1;
     }
 }
 
 void
     }
 }
 
 void
-log(unsigned int cat, const char *msg, ...)
+log_msg(unsigned int cat, const char *msg, ...)
 {
   va_list args;
 
   va_start(args, msg);
 {
   va_list args;
 
   va_start(args, msg);
-  vlog(cat, msg, args);
+  vlog_msg(cat, msg, args);
   va_end(args);
 }
 
   va_end(args);
 }
 
@@ -120,9 +88,15 @@ die(byte *msg, ...)
   va_list args;
 
   va_start(args, msg);
   va_list args;
 
   va_start(args, msg);
-  vlog(L_FATAL, msg, args);
+  vlog_msg(L_FATAL, msg, args);
   va_end(args);
   va_end(args);
+  if (log_die_hook)
+    log_die_hook();
+#ifdef DEBUG_DIE_BY_ABORT
+  abort();
+#else
   exit(1);
   exit(1);
+#endif
 }
 
 #ifdef DEBUG
 }
 
 #ifdef DEBUG
@@ -146,8 +120,8 @@ log_basename(byte *n)
   byte *p = n;
 
   while (*n)
   byte *p = n;
 
   while (*n)
-       if (*n++ == '/')
-         p = n;
+    if (*n++ == '/')
+      p = n;
   return p;
 }
 
   return p;
 }
 
@@ -155,28 +129,9 @@ void
 log_init(byte *argv0)
 {
   if (argv0)
 log_init(byte *argv0)
 {
   if (argv0)
-    log_progname = log_basename(argv0);
-}
-
-void
-log_file(byte *name)
-{
-  if (name)
     {
     {
-      time_t tim = time(NULL);
-      struct tm *tm = localtime(&tim);
-      if (log_name_patt)
-       xfree(log_name_patt);
-      if (log_name)
-       {
-         xfree(log_name);
-         log_name = NULL;
-       }
-      log_name_patt = stralloc(name);
-      log_params = !!strchr(name, '%');
-      log_name_size = strlen(name) + 64;       /* 63 is an upper bound on expansion of % escapes */
-      log_name = xmalloc(log_name_size);
-      log_name[0] = 0;
-      log_switch(tm);
+      strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
+      log_progname[sizeof(log_progname)-1] = 0;
+      log_title = log_progname;
     }
 }
     }
 }