mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
added new option to redirect log output to a file
This commit is contained in:
parent
4f3d61786d
commit
27f86f11ed
2 changed files with 18 additions and 3 deletions
|
@ -74,6 +74,11 @@ struct log {
|
|||
/** Debug facilities used by the debug() macro. */
|
||||
long facilities;
|
||||
|
||||
/** Path of the log file */
|
||||
const char *path;
|
||||
|
||||
/** Send all log output to this file / stdout / stderr */
|
||||
FILE *file;
|
||||
};
|
||||
|
||||
/** Initialize log object */
|
||||
|
|
16
lib/log.c
16
lib/log.c
|
@ -79,10 +79,11 @@ int log_parse(struct log *l, config_setting_t *cfg)
|
|||
cerror(cfg, "Setting 'log' must be a group.");
|
||||
|
||||
config_setting_lookup_int(cfg, "level", &l->level);
|
||||
config_setting_lookup_string(cfg, "file", &l->path);
|
||||
|
||||
if (config_setting_lookup_string(cfg, "facilities", &facilities))
|
||||
log_set_facility_expression(l, facilities);
|
||||
|
||||
|
||||
l->state = STATE_PARSED;
|
||||
|
||||
return 0;
|
||||
|
@ -91,7 +92,13 @@ int log_parse(struct log *l, config_setting_t *cfg)
|
|||
int log_start(struct log *l)
|
||||
{
|
||||
l->epoch = time_now();
|
||||
|
||||
|
||||
l->file = l->path ? fopen(l->path, "a+") : stderr;
|
||||
if (!l->file) {
|
||||
l->file = stderr;
|
||||
error("Failed to open log file '%s'", l->path);
|
||||
}
|
||||
|
||||
l->state = STATE_STARTED;
|
||||
|
||||
return 0;
|
||||
|
@ -101,6 +108,9 @@ int log_stop(struct log *l)
|
|||
{
|
||||
assert(l->state == STATE_STARTED);
|
||||
|
||||
if (l->file != stderr && l->file != stdout)
|
||||
fclose(l->file);
|
||||
|
||||
l->state = STATE_STOPPED;
|
||||
|
||||
return 0;
|
||||
|
@ -213,7 +223,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(stderr, "\r%s\n", buf);
|
||||
fprintf(l->file, "\r%s\n", buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue