From bf0abad7194a69157b360c4ab3f33704e8430a90 Mon Sep 17 00:00:00 2001 From: Jeremy Lakeman Date: Mon, 25 Jan 2016 17:09:07 +1030 Subject: [PATCH] Redirect any output from the server to stderr to the log file Mainly to capture the output of any 3rd party libraries --- log.c | 7 ++++++- log.h | 2 +- server.c | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/log.c b/log.c index 76877f7e..d456c996 100644 --- a/log.c +++ b/log.c @@ -498,6 +498,11 @@ static void _open_log_file(_log_iterator *it) _log_current_datetime(it, LOG_LEVEL_INFO); log_mkdir_trace(dir); _logs_printf_nl(LOG_LEVEL_INFO, __NOWHERE__, "Logging to %s (fd %d)", _log_file_path, fileno(_log_file)); + + // if stderr should be redirected + if (logfile_stderr == NO_FILE) + dup2(fileno(_log_file),STDERR_FILENO); + // Update the log symlink to point to the latest log file. strbuf sbsymlink = strbuf_alloca(400); strbuf_system_log_path(sbsymlink); @@ -634,7 +639,7 @@ static void _flush_log_stderr() fflush(logfile_stderr); } -void disable_log_stderr() +void redirect_stderr_to_log() { if (logfile_stderr && logfile_stderr != NO_FILE) { fflush(logfile_stderr); diff --git a/log.h b/log.h index 1d6b880e..e99a0c60 100644 --- a/log.h +++ b/log.h @@ -50,7 +50,7 @@ int string_to_log_level(const char *text); // Log output control. extern int logLevel_NoLogFileConfigured; void close_log_file(); -void disable_log_stderr(); +void redirect_stderr_to_log(); void logFlush(); void logConfigChanged(); diff --git a/server.c b/server.c index 1461b934..1c156fdd 100644 --- a/server.c +++ b/server.c @@ -754,7 +754,6 @@ static int app_server_start(const struct cli_parsed *parsed, struct cli_context */ DEBUG(verbose, "Grand-Child Process, reopening log"); close_log_file(); - disable_log_stderr(); int fd; if ((fd = open("/dev/null", O_RDWR, 0)) == -1) exit(WHY_perror("open(\"/dev/null\")")); @@ -770,6 +769,7 @@ static int app_server_start(const struct cli_parsed *parsed, struct cli_context exit(WHYF_perror("dup2(%d,stderr)", fd)); if (fd > 2) (void)close(fd); + redirect_stderr_to_log(); /* The execpath option is provided so that a JNI call to "start" can be made which creates a new server daemon process with the correct argv[0]. Otherwise, the servald process appears as a process with argv[0] = "org.servalproject". */