From 8f53cc656359e686a7ec987c9eb7b450fc1e11a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Thu, 8 Nov 2012 23:25:46 +0100 Subject: [PATCH] Restructure logging internal. Add raw logging capability --- src/ucore_logging.c | 87 +++++++++++++++++++++++++++++++-------------- src/ucore_logging.h | 19 ++++++++-- 2 files changed, 76 insertions(+), 30 deletions(-) diff --git a/src/ucore_logging.c b/src/ucore_logging.c index 79ea462..c019fb5 100644 --- a/src/ucore_logging.c +++ b/src/ucore_logging.c @@ -37,6 +37,7 @@ struct uc_log_args { int log_level; const char *file; int line; + int raw; }; @@ -299,17 +300,19 @@ int uc_log_module_set_loglevel(int module, enum UC_LOG_LEVEL level) return rc; } -static inline void uc_vlog_internal(const struct uc_log_args *args, const char *fmt, va_list ap) +static inline void uc_vlog_file(const struct uc_log_args *args, const char *fmt, va_list ap) { struct uc_log_destination *dest = args->dest; - - if(dest->dest_type != UC_LDEST_SYSLOG && dest->type.file.file != NULL) { - char time_buf[48]; - time_t now; - struct tm t; + char time_buf[48]; + time_t now; + struct tm t; - now = time(NULL); - localtime_r(&now, &t); + if(dest->type.file.file == NULL) + return; + + now = time(NULL); + localtime_r(&now, &t); + if(!args->raw) { snprintf(time_buf, sizeof time_buf, "%04d-%02d-%02d %02d:%02d:%02d", t.tm_year + 1900, t.tm_mon + 1, @@ -318,42 +321,61 @@ static inline void uc_vlog_internal(const struct uc_log_args *args, const char * t.tm_min, t.tm_sec); + time_buf[sizeof time_buf -1] = 0; + + if(dest->log_location) { - fprintf(dest->type.file.file, "[%s] %s %s:%d [%s] : ", uc_ll_2_str(args->log_level), - time_buf, args->file, args->line, args->module->short_name); + fprintf(dest->type.file.file, "[%s] %s %s:%d [%s] : ", + uc_ll_2_str(args->log_level), time_buf, args->file, + args->line, args->module->short_name); } else { fprintf(dest->type.file.file, "[%s] %s [%s]: ", uc_ll_2_str(args->log_level), time_buf, args->module->short_name); } + } - vfprintf(dest->type.file.file, fmt, ap); + vfprintf(dest->type.file.file, fmt, ap); - fflush(dest->type.file.file); //strictly not needed for stderr - // though stderr could be redirected to a file + fflush(dest->type.file.file); //strictly not needed for stderr + // though stderr could be redirected to a file +} - } else if(dest->dest_type == UC_LDEST_SYSLOG) { - int pri = uc_ll_2_syslog(args->log_level); +static inline void uc_vlog_syslog(const struct uc_log_args *args, const char *fmt, va_list ap) +{ + struct uc_log_destination *dest = args->dest; + + int pri = uc_ll_2_syslog(args->log_level); - //go via a buffer as we must do just 1 syslog call - char buf[4096]; - int len = 0; + //go via a buffer as we must do just 1 syslog call + char buf[4096]; + int offset = 0; + buf[0] = 0; + if(!args->raw) { + int rc; if(dest->log_location) { - len = snprintf(buf, sizeof buf, "%s:%d ", args->file, args->line); + rc = snprintf(buf, sizeof buf, "%s:%d ", args->file, args->line); + if(rc < 0) + goto log; + offset += rc; } - len += snprintf(&buf[len], sizeof buf - (size_t)len, "[%s] ", args->module->short_name); - - vsnprintf(&buf[len], sizeof buf - (size_t)len, fmt, ap); - - syslog(pri, "%s", buf); - + rc = snprintf(&buf[offset], sizeof buf - (size_t)offset, "[%s] ", + args->module->short_name); + if(rc < 0) + goto log; + offset +=rc; } + + vsnprintf(&buf[offset], sizeof buf - (size_t)offset, fmt, ap); +log: + syslog(pri, "%s", buf); } -void uc_logf(int log_level, int module, const char *file, int line, const char *fmt, ...) +void uc_logf(int log_level, int module, int raw, + const char *file, int line, const char *fmt, ...) { va_list ap; struct uc_log_destination *dest; @@ -386,12 +408,23 @@ void uc_logf(int log_level, int module, const char *file, int line, const char * .log_level = log_level, .file = file, .line = line, + .raw = raw }; //log func will mess with the va_list, //so make a copy va_copy(apc, ap); - uc_vlog_internal(&args, fmt, apc); + + switch(dest->dest_type) { + case UC_LDEST_SYSLOG: + uc_vlog_syslog(&args, fmt, apc); + break; + case UC_LDEST_STDERR: + case UC_LDEST_FILE: + uc_vlog_file(&args, fmt, apc); + break; + } + va_end(apc); } } diff --git a/src/ucore_logging.h b/src/ucore_logging.h index e342a34..81d41d3 100644 --- a/src/ucore_logging.h +++ b/src/ucore_logging.h @@ -210,12 +210,18 @@ int uc_log_reopen_files(void); */ void uc_log_init(struct uc_log_modules *user_modules); -void uc_logf(int log_level, int module, const char *file, int line, const char *fmt, ...) __attribute__((format(printf, 5, 6))); +void uc_logf(int log_level, int module, int raw, + const char *file, int line, const char *fmt, ...) + __attribute__((format(printf, 6, 7))); #ifdef DEBUG -# define UC_DEBUGF(mod, fmt, ...) uc_logf(UC_LL_DEBUG, mod, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +# define UC_DEBUGF(mod, fmt, ...)\ + uc_logf(UC_LL_DEBUG, mod,0 , __FILE__, __LINE__, fmt, ## __VA_ARGS__) +# define UC_DEBUGFR(mod, fmt, ...)\ + uc_logf(UC_LL_DEBUG, mod,1 , , __LINE__, fmt, ## __VA_ARGS__) #else # define UC_DEBUGF(mod, fmt, ...) +# define UC_DEBUGFR(mod, fmt, ...) #endif /** Main macro that should be used for logging. @@ -228,9 +234,16 @@ void uc_logf(int log_level, int module, const char *file, int line, const char * * @param fmt printf style format string * @param ... printf style arguments */ -#define UC_LOGF(lvl, mod, fmt, ...) uc_logf(lvl, mod, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +#define UC_LOGF(lvl, mod, fmt, ...) \ + uc_logf(lvl, mod, 0, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +/** Raw logging ,Like UC_LOGF, but only prints the supplied + * data, not timestamp, log level etc. + */ +#define UC_LOGFR(lvl, mod, fmt, ...) \ + uc_logf(lvl, mod, 1, __FILE__, __LINE__, fmt, ## __VA_ARGS__) + #ifdef __cplusplus } #endif