X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Flog.c;h=cad127f47bb241a1ab7fb2c850ff046c24879226;hb=d5713848229da7765d89ecc2db81c5f99552f8b4;hp=bcbce64b6115e010059e1788e16070f3f367e63a;hpb=fc078dbe4fe9653bee78853f0cbefc5b79c322a6;p=libucw.git diff --git a/lib/log.c b/lib/log.c index bcbce64b..cad127f4 100644 --- a/lib/log.c +++ b/lib/log.c @@ -1,7 +1,7 @@ /* * Sherlock Library -- Logging * - * (c) 1997--2001 Martin Mares + * (c) 1997--2004 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -11,77 +11,74 @@ #include #include -#include #include -#include #include #include +#include -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; +void (*log_die_hook)(void); +void (*log_switch_hook)(struct tm *tm); 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) +vlog_msg(unsigned int cat, const char *msg, va_list args) { time_t tim = time(NULL); struct tm *tm = localtime(&tim); - char buf[32]; + byte *buf, *p; + int buflen = 256; + int l, l0, r; - log_switch(tm); - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm); - fprintf(stderr, "%c %s ", cat, buf); - if (log_progname) + 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; + p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", tm); + if (log_title) + { + if (log_pid) + p += sprintf(p, "[%s (%d)] ", log_title, log_pid); + else + p += sprintf(p, "[%s] ", log_title); + } else - fprintf(stderr, "[%s] ", log_progname); - } - else - { - if (log_pid) - fprintf(stderr, "[%d] ", log_pid); + { + if (log_pid) + p += sprintf(p, "[%d] ", log_pid); + } + l0 = p - buf + 1; + r = buflen - l0; + l = vsnprintf(p, r, msg, args); + 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; } - vfprintf(stderr, msg, args); - fputc('\n', stderr); - fflush(stderr); } void -log(unsigned int cat, const char *msg, ...) +log_msg(unsigned int cat, const char *msg, ...) { va_list args; va_start(args, msg); - vlog(cat, msg, args); + vlog_msg(cat, msg, args); va_end(args); } @@ -91,9 +88,15 @@ die(byte *msg, ...) va_list args; va_start(args, msg); - vlog(L_FATAL, msg, args); + vlog_msg(L_FATAL, msg, args); va_end(args); + if (log_die_hook) + log_die_hook(); +#ifdef DEBUG_DIE_BY_ABORT + abort(); +#else exit(1); +#endif } #ifdef DEBUG @@ -117,8 +120,8 @@ log_basename(byte *n) byte *p = n; while (*n) - if (*n++ == '/') - p = n; + if (*n++ == '/') + p = n; return p; } @@ -126,19 +129,9 @@ void 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); - 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; } }