From 6119bdf90397bd89cc2e94eb286fd780dcae49e5 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 20 Aug 2018 12:13:53 +0200 Subject: [PATCH] log: remove escape sequences from log output if not in a tty --- include/villas/log.h | 2 ++ lib/log.c | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/include/villas/log.h b/include/villas/log.h index bda5ee582..c5c299fa3 100644 --- a/include/villas/log.h +++ b/include/villas/log.h @@ -28,6 +28,7 @@ extern "C" { #endif #include +#include #include #include @@ -97,6 +98,7 @@ struct log { const char *path; /**< Path of the log file. */ char *prefix; /**< Prefix each line with this string. */ int syslog; /**< Whether or not to log to syslogd. */ + bool tty; /**< Is the log file a tty? */ FILE *file; /**< Send all log output to this file / stdout / stderr. */ }; diff --git a/lib/log.c b/lib/log.c index e024cea26..6e149cf41 100644 --- a/lib/log.c +++ b/lib/log.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -170,12 +171,14 @@ int log_open(struct log *l) else l->file = stderr; - l->state = STATE_OPENED; + l->tty = isatty(fileno(l->file)); if (l->syslog) { openlog(NULL, LOG_PID, LOG_DAEMON); } + l->state = STATE_OPENED; + debug(LOG_LOG | 5, "Log sub-system started: level=%d, faciltities=%#lx, path=%s", l->level, l->facilities, l->path); return 0; @@ -272,7 +275,7 @@ void log_print(struct log *l, const char *lvl, const char *fmt, ...) void log_vprint(struct log *l, const char *lvl, const char *fmt, va_list ap) { struct timespec ts = time_now(); - char *buf = alloc(512); + thread_local char buf[1024]; /* Optional prefix */ if (l->prefix) @@ -288,11 +291,17 @@ 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 - if (l->file) + if (l->file) { + if (l->tty == false) + decolor(buf); + fprintf(l->file, "%s\n", buf); + } + + if (l->syslog) { + if (l->tty == true) // Only decolor if not done before + decolor(buf); - if (l->syslog) vsyslog(LOG_INFO, fmt, ap); - - free(buf); + } }