Support relative log.file config option

As already documented in doc/Servald-Configuration.md
This commit is contained in:
Andrew Bettison 2013-01-23 14:53:26 +10:30
parent dbd4cb1771
commit 546fccc794
3 changed files with 34 additions and 8 deletions

View File

@ -210,7 +210,7 @@ ATOM(char, vomp, 0, cf_opt_char_boolean,, "")
END_STRUCT END_STRUCT
STRUCT(log) STRUCT(log)
STRING(256, file, "", cf_opt_absolute_path,, "Absolute path of log file") STRING(256, file, "", cf_opt_str_nonempty,, "Path of log file, either absolute or relative to instance directory")
ATOM(int, show_pid, 1, cf_opt_int_boolean,, "If true, all log lines contain PID of logging process") ATOM(int, show_pid, 1, cf_opt_int_boolean,, "If true, all log lines contain PID of logging process")
ATOM(int, show_time, 1, cf_opt_int_boolean,, "If true, all log lines contain time stamp") ATOM(int, show_time, 1, cf_opt_int_boolean,, "If true, all log lines contain time stamp")
END_STRUCT END_STRUCT

20
log.c
View File

@ -79,15 +79,21 @@ static FILE *_open_logging()
logpath = config.log.file; logpath = config.log.file;
} }
if (!logpath || !logpath[0]) { if (!logpath || !logpath[0]) {
logfile = stderr; //fopen("/tmp/foo", "a"); logfile = stderr;
INFO("No logfile configured -- logging to stderr"); INFO("No logfile configured -- logging to stderr");
} else if ((logfile = fopen(logpath, "a"))) {
setlinebuf(logfile);
INFOF("Logging to %s (fd %d)", logpath, fileno(logfile));
} else { } else {
logfile = stderr; //fopen("/tmp/bar", "a"); char path[1024];
WARNF_perror("fopen(%s)", logpath); if (!FORM_SERVAL_INSTANCE_PATH(path, logpath)) {
WARNF("Cannot append to %s -- falling back to stderr", logpath); logfile = stderr;
INFO("Logfile path overrun -- logging to stderr");
} else if ((logfile = fopen(path, "a"))) {
setlinebuf(logfile);
INFOF("Logging to %s (fd %d)", path, fileno(logfile));
} else {
logfile = stderr;
WARNF_perror("fopen(%s)", path);
WARNF("Cannot append to %s -- falling back to stderr", path);
}
} }
} }
return logfile; return logfile;

View File

@ -264,4 +264,24 @@ test_InterfacesModernIncompatible() {
--error-pattern='config file.*not loaded.*incompatible' --error-pattern='config file.*not loaded.*incompatible'
} }
doc_LogFileAbsolute="Absolute log file"
test_LogFileAbsolute() {
executeOk_servald config \
set debug.verbose true \
set log.file "$PWD/log"
executeOk_servald echo one
assertGrep log '^DEBUG:.*echo:argv\[1\]="one"$'
assertGrep --matches=0 --message="log contains no error messages" log '^ERROR:'
}
doc_LogFileRelative="Relative log file"
test_LogFileRelative() {
executeOk_servald config \
set debug.verbose true \
set log.file "log"
executeOk_servald echo one
assertGrep "$SERVALINSTANCE_PATH/log" '^DEBUG:.*echo:argv\[1\]="one"$'
assertGrep --matches=0 --message="log contains no error messages" "$SERVALINSTANCE_PATH/log" '^ERROR:'
}
runTests "$@" runTests "$@"