]> mj.ucw.cz Git - libucw.git/commitdiff
Split logging functions to two files to avoid linking the full switching
authorMartin Mares <mj@ucw.cz>
Mon, 10 May 2004 18:24:30 +0000 (18:24 +0000)
committerMartin Mares <mj@ucw.cz>
Mon, 10 May 2004 18:24:30 +0000 (18:24 +0000)
machinery to all programs which use trivial logging to stdout.

lib/Makefile
lib/lib.h
lib/log-file.c [new file with mode: 0644]
lib/log.c

index 50803a6a757bcf62e3cffb810625493ab1cd363c..76215b2475b42d90d576ac7ec53dd62f6dc3b078 100644 (file)
@@ -5,7 +5,7 @@ PROGS+=obj/lib/db-tool obj/lib/buckettool
 TESTS+=obj/lib/regex-ut
 
 LIBSH_MODS=alloc alloc_str ctmatch db fastbuf fb-file fb-mem lists \
-       log log2 md5 md5hex mmap pagecache patimatch patmatch pool pool-str \
+       log log-file log2 md5 md5hex mmap pagecache patimatch patmatch pool pool-str \
        prime random realloc regex timer url wildmatch \
        wordsplit str_ctype str_upper str_lower bucket conf object sorter \
        finger proctitle ipaccess profile bitsig randomkey \
index b8bf20dea9a5c3b5f7a4b9a0b53a5429f1da190d..2186b5d456fa4492e18400983614ef2b07cbb914 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
 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 int log_pid;                    /* 0 if shouldn't be logged */
 extern void (*log_die_hook)(void);
+struct tm;
+extern void (*log_switch_hook)(struct tm *tm);
 
 void log_msg(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
 #define log log_msg
 void vlog_msg(unsigned int cat, const char *msg, va_list args);
 void die(byte *, ...) NONRET;
-void log_init(byte *);
-void log_file(byte *);
+void log_init(byte *argv0);
+void log_file(byte *name);
 void log_fork(void);
 void log_switch(void);
 
diff --git a/lib/log-file.c b/lib/log-file.c
new file mode 100644 (file)
index 0000000..5916acb
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ *     Sherlock Library -- Keeping of Log Files
+ *
+ *     (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 <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <time.h>
+
+static char *log_name_patt;
+static int log_params;
+static int log_filename_size;
+int log_switch_nest;
+
+static void
+do_log_switch(struct tm *tm)
+{
+  int fd, l;
+  char name[log_filename_size];
+
+  if (!log_name_patt ||
+      log_filename[0] && !log_params)
+    return;
+  log_switch_nest++;
+  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_filename))
+    {
+      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);
+      close(2);
+      dup(fd);
+      close(fd);
+      close(1);
+      dup(2);
+    }
+  log_switch_nest--;
+}
+
+void
+log_switch(void)
+{
+  time_t tim = time(NULL);
+  do_log_switch(localtime(&tim));
+}
+
+static void
+internal_log_switch(struct tm *tm)
+{
+  if (!log_switch_nest)
+    do_log_switch(tm);
+}
+
+void
+log_file(byte *name)
+{
+  if (name)
+    {
+      if (log_name_patt)
+       xfree(log_name_patt);
+      if (log_filename)
+       {
+         xfree(log_filename);
+         log_filename = NULL;
+       }
+      log_name_patt = xstrdup(name);
+      log_params = !!strchr(name, '%');
+      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();
+      log_switch_hook = internal_log_switch;
+      close(0);
+      open("/dev/null", O_RDWR, 0);
+    }
+}
+
+void
+log_fork(void)
+{
+  log_pid = getpid();
+}
+
+#ifdef TEST
+
+int main(int argc, char **argv)
+{
+  log_init(argv[0]);
+  log_file("/proc/self/fd/1");
+  for (int i=1; i<argc; i++)
+    log(L_INFO, argv[i]);
+  return 0;
+}
+
+#endif
index e1fc2bed185afc946bca9cdfe03ec6f27fa96df4..0c91bcd15ec109fb49e4631647eb2cafdef99850 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -1,7 +1,7 @@
 /*
  *     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.
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
-#include <fcntl.h>
 #include <unistd.h>
 #include <time.h>
 #include <alloca.h>
 
-static char log_progname[32], *log_name_patt;
+static char log_progname[32];
 char *log_filename;
 char *log_title;
-static int log_pid;
-static int log_params;
-static int log_filename_size;
-int log_switch_nest;
+int log_pid;
 void (*log_die_hook)(void);
-
-void
-log_fork(void)
-{
-  log_pid = getpid();
-}
-
-static void
-do_log_switch(struct tm *tm)
-{
-  int fd, l;
-  char name[log_filename_size];
-
-  if (!log_name_patt ||
-      log_filename[0] && !log_params)
-    return;
-  log_switch_nest++;
-  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_filename))
-    {
-      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);
-      close(2);
-      dup(fd);
-      close(fd);
-      close(1);
-      dup(2);
-    }
-  log_switch_nest--;
-}
-
-void
-log_switch(void)
-{
-  time_t tim = time(NULL);
-  do_log_switch(localtime(&tim));
-}
-
-static inline void
-internal_log_switch(struct tm *tm)
-{
-  if (!log_switch_nest)
-    do_log_switch(tm);
-}
+void (*log_switch_hook)(struct tm *tm);
 
 void
 vlog_msg(unsigned int cat, const char *msg, va_list args)
@@ -84,7 +33,8 @@ vlog_msg(unsigned int cat, const char *msg, va_list args)
   int buflen = 256;
   int l, l0, r;
 
-  internal_log_switch(tm);
+  if (log_switch_hook)
+    log_switch_hook(tm);
   while (1)
     {
       p = buf = alloca(buflen);
@@ -171,8 +121,8 @@ log_basename(byte *n)
   byte *p = n;
 
   while (*n)
-       if (*n++ == '/')
-         p = n;
+    if (*n++ == '/')
+      p = n;
   return p;
 }
 
@@ -186,26 +136,3 @@ log_init(byte *argv0)
       log_title = log_progname;
     }
 }
-
-void
-log_file(byte *name)
-{
-  if (name)
-    {
-      if (log_name_patt)
-       xfree(log_name_patt);
-      if (log_filename)
-       {
-         xfree(log_filename);
-         log_filename = NULL;
-       }
-      log_name_patt = xstrdup(name);
-      log_params = !!strchr(name, '%');
-      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);
-    }
-}