Skip to content

Commit

Permalink
Redirect any output from the server to stderr to the log file
Browse files Browse the repository at this point in the history
Mainly to capture the output of any 3rd party libraries
  • Loading branch information
lakeman committed Jan 27, 2016
1 parent 389f16d commit bf0abad
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion log.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion log.h
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion server.c
Expand Up @@ -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\")"));
Expand All @@ -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". */
Expand Down

0 comments on commit bf0abad

Please sign in to comment.