From dd727096c813abe54a7eb438d3cde3c0120f9dd3 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 10 May 2004 18:24:30 +0000 Subject: [PATCH] Split logging functions to two files to avoid linking the full switching machinery to all programs which use trivial logging to stdout. --- lib/Makefile | 2 +- lib/lib.h | 7 +++- lib/log-file.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/log.c | 89 ++++------------------------------------ 4 files changed, 121 insertions(+), 84 deletions(-) create mode 100644 lib/log-file.c diff --git a/lib/Makefile b/lib/Makefile index 50803a6a..76215b24 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -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 \ diff --git a/lib/lib.h b/lib/lib.h index b8bf20de..2186b5d4 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -57,14 +57,17 @@ 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 index 00000000..5916acb5 --- /dev/null +++ b/lib/log-file.c @@ -0,0 +1,107 @@ +/* + * Sherlock Library -- Keeping of Log Files + * + * (c) 1997--2004 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/lib.h" + +#include +#include +#include +#include +#include +#include + +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 + * (c) 1997--2004 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -13,67 +13,16 @@ #include #include #include -#include #include #include #include -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); - } -} -- 2.39.2