From 8ea532bfd73db1b23d444dac3ebb2581a013a90a Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 14 Jan 2001 17:51:22 +0000 Subject: [PATCH] New logging mechanism. Also cleaned up lib.h. --- lib/lib.h | 45 ++++++++++++++++++++++----------------------- lib/log.c | 45 ++++++++++++++++++++------------------------- lib/log.h | 14 -------------- 3 files changed, 42 insertions(+), 62 deletions(-) delete mode 100644 lib/log.h diff --git a/lib/lib.h b/lib/lib.h index 7e4a3b56..48b71737 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -1,7 +1,7 @@ /* * Sherlock Library -- Miscellaneous Functions * - * (c) 1997--2000 Martin Mares + * (c) 1997--2001 Martin Mares */ /* @@ -43,28 +43,21 @@ void open_temp(struct tempfile *, byte *); void delete_temp(struct tempfile *); u32 temprand(uns); -/* FIXME: Remove? */ -#define TF_GENERIC "t" -#define TF_QUEUE_CONTROL "c" -#define TF_QUEUE_DATA "d" -#define TF_DECODE "x" -#define TF_TRANSFORM "s" -#define TF_OBJECT "o" - /* Logging */ -/* FIXME: Define new logging mechanism? */ - -#define L_DEBUG "<0>" -#define L_INFO "<2>" -#define L_WARN "<4>" -#define L_ERROR "<6>" -#define L_FATAL "<9>" +#define L_DEBUG 'D' /* Debugging messages */ +#define L_INFO 'I' /* Informational msgs, warnings and errors */ +#define L_WARN 'W' +#define L_ERROR 'E' +#define L_INFO_R 'i' /* Errors caused by external events */ +#define L_WARN_R 'w' +#define L_ERROR_R 'e' +#define L_FATAL '!' /* die() */ -void log(byte *, ...); +void log(unsigned int cat, byte *msg, ...); void die(byte *, ...) NONRET; -void initlog(byte *); -void open_log_file(byte *); +void log_init(byte *); +void log_file(byte *); #ifdef DEBUG #define ASSERT(x) do { if (!(x)) die("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0) @@ -72,7 +65,13 @@ void open_log_file(byte *); #define ASSERT(x) do { } while(0) #endif -/* Allocation */ +#ifdef LOCAL_DEBUG +#define DBG(x,y...) log(L_DEBUG, x,##y) +#else +#define DBG(x,y...) do { } while(0) +#endif + +/* Memory allocation */ #ifdef DMALLOC /* @@ -91,7 +90,7 @@ void open_log_file(byte *); * their own xmalloc and we don't want to interfere with them, hence * the renaming. */ -#define xmalloc bird_xmalloc +#define xmalloc sh_xmalloc void *xmalloc(unsigned); void *xrealloc(void *, unsigned); #define xfree(x) free(x) @@ -103,7 +102,7 @@ byte *stralloc(byte *); int match_ct_patt(byte *, byte *); -/* Binary log */ +/* log2.c */ int log2(u32); @@ -111,7 +110,7 @@ int log2(u32); int wordsplit(byte *, byte **, uns); -/* pat(i)match.c */ +/* pat(i)match.c: Matching of shell patterns */ int match_pattern(byte *, byte *); int match_pattern_nocase(byte *, byte *); diff --git a/lib/log.c b/lib/log.c index 86f8607e..97b46713 100644 --- a/lib/log.c +++ b/lib/log.c @@ -1,7 +1,7 @@ /* * Sherlock Library -- Logging * - * (c) 1997 Martin Mares + * (c) 1997--2001 Martin Mares */ #include "lib/lib.h" @@ -13,38 +13,34 @@ #include #include -static byte *progname = "???"; -static pid_t pid; +static byte *log_progname; +static pid_t log_pid; static void -logit(int level, byte *msg, va_list args) +vlog(unsigned int cat, byte *msg, va_list args) { - time_t tim; - struct tm *tm; + time_t tim = time(NULL); + struct tm *tm = localtime(&tim); char buf[32]; - tim = time(NULL); - tm = localtime(&tim); - strftime(buf, sizeof(buf), "%d-%m-%Y %H:%M:%S", tm); - fprintf(stderr, "%s %s [%d] <%d> ", buf, progname, pid, level); + if (!log_pid) + log_pid = getpid(); + strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm); + fprintf(stderr, "%c %s ", cat, buf); + if (log_progname) + fprintf(stderr, "[%s (%d)] ", log_progname, log_pid); vfprintf(stderr, msg, args); fputc('\n', stderr); fflush(stderr); } void -log(byte *msg, ...) +log(unsigned int cat, byte *msg, ...) { - int level = 2; va_list args; va_start(args, msg); - if (msg[0] == '<' && msg[1] >= '0' && msg[1] <= '9' && msg[2] == '>') - { - level = msg[1] - '0'; - msg += 3; - } - logit(level, msg, args); + vlog(cat, msg, args); va_end(args); } @@ -54,9 +50,9 @@ die(byte *msg, ...) va_list args; va_start(args, msg); - logit(9, msg, args); + vlog(L_FATAL, msg, args); va_end(args); - exit(99); + exit(1); } static byte * @@ -71,21 +67,20 @@ basename(byte *n) } void -initlog(byte *argv0) +log_init(byte *argv0) { if (argv0) - progname = basename(argv0); - pid = getpid(); + log_progname = basename(argv0); } void -open_log_file(byte *name) +log_file(byte *name) { if (name) { int fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666); if (fd < 0) - die("Unable to open log file"); + die("Unable to open log file %s: %m", name); close(2); dup(fd); close(fd); diff --git a/lib/log.h b/lib/log.h deleted file mode 100644 index e33c77c7..00000000 --- a/lib/log.h +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Sherlock Library -- Logging - * - * (c) 1997 Martin Mares - */ - -#define L_DEBUG "<0>" -#define L_INFO "<2>" -#define L_WARN "<4>" -#define L_ERROR "<6>" -#define L_FATAL "<9>" - -int log(byte *, ...); -void initlog(byte *); -- 2.39.2