Redirect any output from the server to stderr to the log file

Mainly to capture the output of any 3rd party libraries
This commit is contained in:
Jeremy Lakeman 2016-01-25 17:09:07 +10:30
parent 389f16d2bb
commit bf0abad719
3 changed files with 8 additions and 3 deletions

7
log.c
View File

@ -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);

2
log.h
View File

@ -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();

View File

@ -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". */