]> mj.ucw.cz Git - libucw.git/blobdiff - lib/log.c
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / lib / log.c
index eb3c0ab26d4736034b906fca3d5dacc445f61649..0063f3ea7eed13d2239f28f85cda35d8a475d927 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
 /*
 /*
- *     Sherlock Library -- Logging
+ *     UCW Library -- Logging
  *
  *
- *     (c) 1997--2001 Martin Mares <mj@ucw.cz>
+ *     (c) 1997--2006 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 "lib/lib.h"
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdarg.h>
 #include <string.h>
 #include <string.h>
-#include <fcntl.h>
 #include <unistd.h>
 #include <unistd.h>
+#include <sys/time.h>
 #include <time.h>
 #include <time.h>
+#include <alloca.h>
 
 
-static char *log_progname, log_name_patt[64], log_name[64];
-static pid_t log_pid;
-static int log_params;
+static char log_progname[32];
+char *log_filename;
+char *log_title;
+int log_pid;
+int log_precise_timings;
+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;
-  char name[64];
-
-  if (!log_name_patt[0] ||
-      log_name[0] && !log_params)
-    return;
-  strftime(name, sizeof(name), log_name_patt, tm);
-  if (!strcmp(name, log_name))
-    return;
-  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);
-}
-
-static void
-vlog(unsigned int cat, const char *msg, va_list args)
+vmsg(unsigned int cat, const char *fmt, va_list args)
 {
 {
-  time_t tim = time(NULL);
-  struct tm *tm = localtime(&tim);
-  char buf[32];
-
-  log_switch(tm);
-  strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
-  fprintf(stderr, "%c %s ", cat, buf);
-  if (log_progname)
+  struct timeval tv;
+  struct tm tm;
+  byte *buf, *p;
+  int buflen = 256;
+  int l, l0, r;
+  va_list args2;
+
+  gettimeofday(&tv, NULL);
+  if (!localtime_r(&tv.tv_sec, &tm))
+    bzero(&tm, sizeof(tm));
+
+  if (log_switch_hook)
+    log_switch_hook(&tm);
+  while (1)
     {
     {
-      if (log_pid)
-       fprintf(stderr, "[%s (%d)] ", log_progname, log_pid);
+      p = buf = alloca(buflen);
+      *p++ = cat;
+      /* We cannot use strftime() here, because it's not re-entrant */
+      p += sprintf(p, " %4d-%02d-%02d %02d:%02d:%02d", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
+                  tm.tm_hour, tm.tm_min, tm.tm_sec);
+      if (log_precise_timings)
+        p += sprintf(p, ".%06d", (int)tv.tv_usec);
+      *p++ = ' ';
+      if (log_title)
+       {
+         if (log_pid)
+           p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
+         else
+           p += sprintf(p, "[%s] ", log_title);
+       }
       else
       else
-       fprintf(stderr, "[%s] ", log_progname);
+       {
+         if (log_pid)
+           p += sprintf(p, "[%d] ", log_pid);
+       }
+      l0 = p - buf + 1;
+      r = buflen - l0;
+      va_copy(args2, args);
+      l = vsnprintf(p, r, fmt, args2);
+      va_end(args2);
+      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;
     }
     }
-  else
-    {
-      if (log_pid)
-       fprintf(stderr, "[%d] ", log_pid);
-    }
-  vfprintf(stderr, msg, args);
-  fputc('\n', stderr);
-  fflush(stderr);
 }
 
 void
 }
 
 void
-log(unsigned int cat, const char *msg, ...)
+msg(unsigned int cat, const char *fmt, ...)
 {
   va_list args;
 
 {
   va_list args;
 
-  va_start(args, msg);
-  vlog(cat, msg, args);
+  va_start(args, fmt);
+  vmsg(cat, fmt, args);
   va_end(args);
 }
 
 void
   va_end(args);
 }
 
 void
-die(byte *msg, ...)
+die(const char *fmt, ...)
 {
   va_list args;
 
 {
   va_list args;
 
-  va_start(args, msg);
-  vlog(L_FATAL, msg, args);
+  va_start(args, fmt);
+  vmsg(L_FATAL, fmt, 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
 }
 
 }
 
-static byte *
-log_basename(byte *n)
+void
+assert_failed(const char *assertion, const char *file, int line)
 {
 {
-  byte *p = n;
-
-  while (*n)
-       if (*n++ == '/')
-         p = n;
-  return p;
+  msg(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
+  abort();
 }
 
 void
 }
 
 void
-log_init(byte *argv0)
+assert_failed_noinfo(void)
 {
 {
-  if (argv0)
-    log_progname = log_basename(argv0);
+  die("Internal error: Assertion failed.");
+}
+
+static const char *
+log_basename(const char *n)
+{
+  const char *p = n;
+
+  while (*n)
+    if (*n++ == '/')
+      p = n;
+  return p;
 }
 
 void
 }
 
 void
-log_file(byte *name)
+log_init(const char *argv0)
 {
 {
-  if (name)
+  if (argv0)
     {
     {
-      time_t tim = time(NULL);
-      struct tm *tm = localtime(&tim);
-      strcpy(log_name_patt, name);
-      log_params = !!strchr(name, '%');
-      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;
     }
 }
     }
 }