mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
log: remove escape sequences from log output if not in a tty
This commit is contained in:
parent
5d77613152
commit
6119bdf903
2 changed files with 17 additions and 6 deletions
|
@ -28,6 +28,7 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
|
@ -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. */
|
||||
};
|
||||
|
|
21
lib/log.c
21
lib/log.c
|
@ -28,6 +28,7 @@
|
|||
#include <unistd.h>
|
||||
#include <syslog.h>
|
||||
#include <signal.h>
|
||||
#include <threads.h>
|
||||
|
||||
#include <villas/config.h>
|
||||
#include <villas/log.h>
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue