diff --git a/conf_schema.h b/conf_schema.h index 710e98da..79e57aec 100644 --- a/conf_schema.h +++ b/conf_schema.h @@ -210,7 +210,7 @@ ATOM(char, vomp, 0, cf_opt_char_boolean,, "") END_STRUCT 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_time, 1, cf_opt_int_boolean,, "If true, all log lines contain time stamp") END_STRUCT diff --git a/log.c b/log.c index 301e238e..38d0f41e 100644 --- a/log.c +++ b/log.c @@ -79,15 +79,21 @@ static FILE *_open_logging() logpath = config.log.file; } if (!logpath || !logpath[0]) { - logfile = stderr; //fopen("/tmp/foo", "a"); + logfile = 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 { - logfile = stderr; //fopen("/tmp/bar", "a"); - WARNF_perror("fopen(%s)", logpath); - WARNF("Cannot append to %s -- falling back to stderr", logpath); + char path[1024]; + if (!FORM_SERVAL_INSTANCE_PATH(path, 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; diff --git a/tests/config b/tests/config index cd0abfbe..3e30e474 100755 --- a/tests/config +++ b/tests/config @@ -264,4 +264,24 @@ test_InterfacesModernIncompatible() { --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 "$@"