1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

log: do proper default initialization

This commit is contained in:
Steffen Vogel 2017-08-20 10:51:41 +02:00
parent 0239a15a84
commit 3d3eb1430b
3 changed files with 29 additions and 24 deletions

View file

@ -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);
}

View file

@ -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);

View file

@ -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);