]> mj.ucw.cz Git - libucw.git/commitdiff
The logger now exports the name of the current log file and calls a special
authorMartin Mares <mj@ucw.cz>
Mon, 10 May 2004 16:16:15 +0000 (16:16 +0000)
committerMartin Mares <mj@ucw.cz>
Mon, 10 May 2004 16:16:15 +0000 (16:16 +0000)
hook on die(). Also corrected log_switch_{en,dis}able().

lib/lib.h
lib/log.c

index 8370a6050a21e8ebd4da6fdc565d44ccbb65eb73..b8bf20dea9a5c3b5f7a4b9a0b53a5429f1da190d 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -55,7 +55,9 @@
 #define L_FATAL                '!'             /* die() */
 
 extern char *log_title;                        /* NULL - print no title, default is log_progname */
+extern char *log_filename;             /* Expanded name of the current log file */
 extern int log_switch_nest;            /* log_switch() nesting counter, increment to disable automatic switches */
+extern void (*log_die_hook)(void);
 
 void log_msg(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
 #define log log_msg
@@ -82,8 +84,8 @@ void assert_failed(void) NONRET;
 #define DBG(x,y...) do { } while(0)
 #endif
 
-static inline void log_switch_enable(void) { log_switch_nest++; }
-static inline void log_switch_disable(void) { ASSERT(log_switch_nest); log_switch_nest--; }
+static inline void log_switch_disable(void) { log_switch_nest++; }
+static inline void log_switch_enable(void) { ASSERT(log_switch_nest); log_switch_nest--; }
 
 /* Memory allocation */
 
index e2a3efeda9d6e0a8d3ea129f3b236f162a7e6e85..e1fc2bed185afc946bca9cdfe03ec6f27fa96df4 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
 #include <time.h>
 #include <alloca.h>
 
-static char log_progname[32], *log_name_patt, *log_name;
+static char log_progname[32], *log_name_patt;
+char *log_filename;
 char *log_title;
 static int log_pid;
 static int log_params;
-static int log_name_size;
+static int log_filename_size;
 int log_switch_nest;
+void (*log_die_hook)(void);
 
 void
 log_fork(void)
@@ -35,18 +37,18 @@ static void
 do_log_switch(struct tm *tm)
 {
   int fd, l;
-  char name[log_name_size];
+  char name[log_filename_size];
 
   if (!log_name_patt ||
-      log_name[0] && !log_params)
+      log_filename[0] && !log_params)
     return;
   log_switch_nest++;
-  l = strftime(name, log_name_size, log_name_patt, tm);
-  if (l < 0 || l >= log_name_size)
+  l = strftime(name, log_filename_size, log_name_patt, tm);
+  if (l < 0 || l >= log_filename_size)
     die("Error formatting log file name: %m");
-  if (strcmp(name, log_name))
+  if (strcmp(name, log_filename))
     {
-      strcpy(log_name, name);
+      strcpy(log_filename, name);
       fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
       if (fd < 0)
        die("Unable to open log file %s: %m", name);
@@ -139,6 +141,8 @@ die(byte *msg, ...)
   va_start(args, msg);
   vlog_msg(L_FATAL, msg, args);
   va_end(args);
+  if (log_die_hook)
+    log_die_hook();
 #ifdef DEBUG_DIE_BY_ABORT
   abort();
 #else
@@ -190,16 +194,16 @@ log_file(byte *name)
     {
       if (log_name_patt)
        xfree(log_name_patt);
-      if (log_name)
+      if (log_filename)
        {
-         xfree(log_name);
-         log_name = NULL;
+         xfree(log_filename);
+         log_filename = NULL;
        }
       log_name_patt = xstrdup(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_filename_size = strlen(name) + 64;   /* 63 is an upper bound on expansion of % escapes */
+      log_filename = xmalloc(log_filename_size);
+      log_filename[0] = 0;
       log_switch();
       close(0);
       open("/dev/null", O_RDWR, 0);