From 3d3eb1430b9d804c1444104a5bc69417c84b8b0a Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sun, 20 Aug 2017 10:51:41 +0200 Subject: [PATCH] log: do proper default initialization --- lib/log.c | 37 +++++++++++++++++++++++-------------- lib/log_helper.c | 12 ++++++------ src/node.c | 4 ---- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/lib/log.c b/lib/log.c index 2dcfb8902..bb1ca50db 100644 --- a/lib/log.c +++ b/lib/log.c @@ -39,19 +39,23 @@ #include "OpalPrint.h" #endif -/** The global log instance. */ struct log *global_log; -struct log default_log = { - .level = V, - .facilities = LOG_ALL, - .file = NULL, - .path = NULL, - .epoch = { -1 , -1 }, - .window = { - .ws_row = LOG_HEIGHT, - .ws_col = LOG_WIDTH - } -}; + +/* We register a default log instance */ +__attribute__((constructor)) +void register_default_log() +{ + int ret; + static struct log default_log; + + ret = log_init(&default_log, V, LOG_ALL); + if (ret) + error("Failed to initalize log"); + + ret = log_start(&default_log); + if (ret) + error("Failed to start log"); +} /** List of debug facilities as strings */ static const char *facilities_strs[] = { @@ -156,6 +160,7 @@ int log_init(struct log *l, int level, long facilitites) int log_start(struct log *l) { l->epoch = time_now(); + l->prefix = getenv("VILLAS_LOG_PREFIX"); l->file = l->path ? fopen(l->path, "a+") : stderr; if (!l->file) { @@ -186,7 +191,7 @@ int log_destroy(struct log *l) { default_log.epoch = l->epoch; - global_log = NULL; + global_log = &default_log; l->state = STATE_DESTROYED; @@ -256,6 +261,10 @@ void log_vprint(struct log *l, const char *lvl, const char *fmt, va_list ap) { struct timespec ts = time_now(); char *buf = alloc(512); + + /* Optional prefix */ + if (l->prefix) + strcatf(&buf, "%s", l->prefix); /* Timestamp & Severity */ strcatf(&buf, "%10.3f %5s ", time_delta(&l->epoch, &ts), lvl); @@ -275,7 +284,7 @@ void log_vprint(struct log *l, const char *lvl, const char *fmt, va_list ap) #ifdef ENABLE_OPAL_ASYNC OpalPrint("VILLASnode: %s\n", buf); #endif - fprintf(l->file ? l->file : stderr, "\33[2K\r%s\n", buf); + fprintf(l->file ? l->file : stderr, "%s\n", buf); free(buf); } diff --git a/lib/log_helper.c b/lib/log_helper.c index d5868d670..cc9f3cd5a 100644 --- a/lib/log_helper.c +++ b/lib/log_helper.c @@ -30,7 +30,7 @@ void debug(long class, const char *fmt, ...) { va_list ap; - struct log *l = global_log ? global_log : &default_log; + struct log *l = global_log; int lvl = class & 0xFF; int fac = class & ~0xFF; @@ -46,7 +46,7 @@ void info(const char *fmt, ...) { va_list ap; - struct log *l = global_log ? global_log : &default_log; + struct log *l = global_log; va_start(ap, fmt); log_vprint(l, LOG_LVL_INFO, fmt, ap); @@ -57,7 +57,7 @@ void warn(const char *fmt, ...) { va_list ap; - struct log *l = global_log ? global_log : &default_log; + struct log *l = global_log; va_start(ap, fmt); log_vprint(l, LOG_LVL_WARN, fmt, ap); @@ -68,7 +68,7 @@ void stats(const char *fmt, ...) { va_list ap; - struct log *l = global_log ? global_log : &default_log; + struct log *l = global_log; va_start(ap, fmt); log_vprint(l, LOG_LVL_STATS, fmt, ap); @@ -79,7 +79,7 @@ void error(const char *fmt, ...) { va_list ap; - struct log *l = global_log ? global_log : &default_log; + struct log *l = global_log; va_start(ap, fmt); log_vprint(l, LOG_LVL_ERROR, fmt, ap); @@ -94,7 +94,7 @@ void serror(const char *fmt, ...) va_list ap; char *buf = NULL; - struct log *l = global_log ? global_log : &default_log; + struct log *l = global_log; va_start(ap, fmt); vstrcatf(&buf, fmt, ap); diff --git a/src/node.c b/src/node.c index 6377935c7..435a8efe6 100644 --- a/src/node.c +++ b/src/node.c @@ -114,7 +114,6 @@ int main(int argc, char *argv[]) else if (argc > 2) usage(); #endif - info("This is VILLASnode %s (built on %s, %s)", CLR_BLD(CLR_YEL(BUILDID)), CLR_BLD(CLR_MAG(__DATE__)), CLR_BLD(CLR_MAG(__TIME__))); @@ -126,9 +125,6 @@ int main(int argc, char *argv[]) #endif /* __linux__ */ signals_init(quit); - log_init(&sn.log, V, LOG_ALL); - log_start(&sn.log); - super_node_init(&sn); super_node_parse_cli(&sn, argc, argv); super_node_check(&sn);