diff options
-rw-r--r-- | Makefile.files | 3 | ||||
-rw-r--r-- | debug.c | 41 | ||||
-rw-r--r-- | debug.h | 10 | ||||
-rw-r--r-- | debug_filter.c | 12 | ||||
-rw-r--r-- | debug_syslog.c | 4 |
5 files changed, 50 insertions, 20 deletions
diff --git a/Makefile.files b/Makefile.files index a75fb39..0e44523 100644 --- a/Makefile.files +++ b/Makefile.files @@ -5,7 +5,8 @@ DEBUG_SOURCES = \ debug.c \ - debug_syslog.c + debug_syslog.c \ + debug_filter.c DEBUG_EXTRA_DIST = \ debug.h \ @@ -56,6 +56,7 @@ struct dbg_config_t { #endif int fd; int file_fd; + int stdout_no_date; #ifdef WITH_DBG_SYSLOG const char* syslog_host; int syslog_port; @@ -110,6 +111,7 @@ dbg_status_t dbg_init(unsigned int flags, ...) dbg_config.fd = -1; dbg_config.file_fd = -1; + dbg_config.stdout_no_date = 0; int end = 0; @@ -124,6 +126,9 @@ dbg_status_t dbg_init(unsigned int flags, ...) case DBG_OPTION_FD: dbg_config.fd = va_arg(vl, int); break; + case DBG_OPTION_STDOUT_NO_DATE: + dbg_config.stdout_no_date = va_arg(vl, int); + break; case DBG_OPTION_FILENAME: if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_FILE) { const char *filename = (const char*)va_arg(vl, char*); @@ -177,9 +182,6 @@ void dbg_close() dbg_mutex_close(); } -const char * const debug_class_str[] = - { "fixme", "err", "warn", "info", "debug" }; - /* static unsigned int gettid() { @@ -203,16 +205,20 @@ static int dbg_create_header(char *hdr, size_t size) t.tm_sec); } -static int dbg_output_fd(int fd, const char *msg) +static int dbg_output_fd(int fd, const char *msg, bool withdate) { + int s; + if(fd == -1) return 1; - char hdr[32]; - dbg_create_header(hdr, sizeof(hdr)); + if(withdate) { + char hdr[32]; + dbg_create_header(hdr, sizeof(hdr)); + + s = write(fd, hdr, strlen(hdr)); + s = write(fd, " ", 1); + } - int s = -1; - s = write(fd, hdr, strlen(hdr)); - s = write(fd, " ", 1); s = write(fd, msg, strlen(msg)); if(msg[strlen(msg) - 1] != '\n') { s = write(fd, "\n", 1); @@ -227,6 +233,10 @@ int __debug(const char *func, const int line, int result = 0; int sz; + // NOTE: This must be identical to the debug_class_str in debug_filter.c + const char * const debug_class_str[] = + { "fixme", "err", "warn", "info", "debug" }; + dbg_mutex_lock(); #ifdef WITH_DBG_FILTER @@ -238,7 +248,6 @@ int __debug(const char *func, const int line, // char buf[1024]; - sz = snprintf(buf, sizeof(buf), "%s:%s:%s:%d ", debug_class_str[(unsigned)cl], ch, func, line); @@ -252,26 +261,28 @@ int __debug(const char *func, const int line, // if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_STDOUT) { - dbg_output_fd(STDOUT_FILENO, buf); + dbg_output_fd(STDOUT_FILENO, buf, dbg_config.stdout_no_date == 0); } if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_STDERR) { - dbg_output_fd(STDERR_FILENO, buf); + dbg_output_fd(STDERR_FILENO, buf, true); } if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_FD) { - dbg_output_fd(dbg_config.fd, buf); + dbg_output_fd(dbg_config.fd, buf, true); } if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_FILE) { - dbg_output_fd(dbg_config.file_fd, buf); + dbg_output_fd(dbg_config.file_fd, buf, true); } +#ifdef WITH_DBG_SYSLOG if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_SYSLOG) { dbg_syslog_output(buf); } +#endif -// done: +done: dbg_mutex_unlock(); return result; @@ -73,6 +73,16 @@ enum DBG_OPTION { DBG_OPTION_FD, /** + * Set this option to make the stdout output to be printed without any date + * information in the header. + * Parameter is an integer. + * Values: + * 0 := use date + * 1 := do not use date. + */ + DBG_OPTION_STDOUT_NO_DATE, + + /** * Host and port to use when logging on an external server. * Host is a const char* argument, port is an integer. * To be used with the DBG_FLAG_USE_SYSLOG flag. diff --git a/debug_filter.c b/debug_filter.c index 4caf4be..23e83dd 100644 --- a/debug_filter.c +++ b/debug_filter.c @@ -31,7 +31,7 @@ #include <stdlib.h> #ifndef WITH_DBG_FILTER -#error debug_filter.c compiled but WITH_DBG_FILTER not defined +#warning debug_filter.c compiled but WITH_DBG_FILTER not defined #endif #define NELEM(x) (sizeof(x)/sizeof((x)[0])) @@ -45,7 +45,9 @@ struct __debug_channel static struct __debug_channel debug_channel[__DEBUG_CHANNEL_MAX]; static unsigned n_debug_channel = 0; -static unsigned debug_flags = (1 << __class_err) | (1 << __class_fixme); + +// Default is to enable everything... +static unsigned debug_flags = 0xffffffff;//(1<<__class_err)|(1<<__class_fixme); int dbg_filter_enabled(const enum __debug_class cl, const char *ch) { @@ -58,8 +60,6 @@ int dbg_filter_enabled(const enum __debug_class cl, const char *ch) return debug_flags & (1 << cl); } -extern const char * const *debug_class_str; - /* * fmt := [set[,set]*]* * set := [+-]channel @@ -72,6 +72,10 @@ int dbg_filter_parse(const char *filter) char *next; char *opt; + // NOTE: This must be identical to the debug_class_str in debug.c + const char * const debug_class_str[] = + { "fixme", "err", "warn", "info", "debug" }; + if(!(s = strdup(filter))) return 1; for(opt = s; opt; opt = next) { diff --git a/debug_syslog.c b/debug_syslog.c index c3f8062..5ed82a9 100644 --- a/debug_syslog.c +++ b/debug_syslog.c @@ -37,6 +37,10 @@ #include <netinet/in.h> #include <errno.h> +#ifndef WITH_DBG_SYSLOG +#warning debug_syslog.c compiled but WITH_DBG_SYSLOG not defined +#endif + #define SYSLOG_MSGLEN 1024 #define SYSLOG_PRILEN 5 #define SYSLOG_TIMELEN 32 |