MXS-2075: Redirect stdout and stderr to the log

If the log file is successfully opened, both stdout and stderr are
redirected to it. This helps catch ASAN reports without having to read the
system journal files.

As the output is redirected to a file, some of the output was made visible
only in non-daemon mode. This helps keep the log file clean and readable.
This commit is contained in:
Markus Mäkelä 2018-10-01 10:01:13 +03:00
parent 3fd0c2c407
commit 62e5f988c5
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
3 changed files with 30 additions and 9 deletions

View File

@ -350,9 +350,12 @@ static void sigterm_handler(int i)
if (n_shutdowns == 1)
{
if (write(STDERR_FILENO, shutdown_msg, sizeof(shutdown_msg) - 1) == -1)
if (!daemon_mode)
{
printf("Failed to write shutdown message!\n");
if (write(STDERR_FILENO, shutdown_msg, sizeof(shutdown_msg) - 1) == -1)
{
printf("Failed to write shutdown message!\n");
}
}
}
else
@ -369,16 +372,22 @@ sigint_handler(int i)
if (n_shutdowns == 1)
{
if (write(STDERR_FILENO, shutdown_msg, sizeof(shutdown_msg) - 1) == -1)
if (!daemon_mode)
{
printf("Failed to write shutdown message!\n");
if (write(STDERR_FILENO, shutdown_msg, sizeof(shutdown_msg) - 1) == -1)
{
printf("Failed to write shutdown message!\n");
}
}
}
else if (n_shutdowns == 2)
{
if (write(STDERR_FILENO, patience_msg, sizeof(patience_msg) - 1) == -1)
if (!daemon_mode)
{
printf("Failed to write shutdown message!\n");
if (write(STDERR_FILENO, patience_msg, sizeof(patience_msg) - 1) == -1)
{
printf("Failed to write shutdown message!\n");
}
}
}
else
@ -500,8 +509,11 @@ sigfatal_handler(int i)
void *addrs[128];
int count = backtrace(addrs, 128);
// First print the stack trace to stderr as malloc is likely broken
backtrace_symbols_fd(addrs, count, STDERR_FILENO);
if (!daemon_mode)
{
// First print the stack trace to stderr as malloc is likely broken
backtrace_symbols_fd(addrs, count, STDERR_FILENO);
}
MXS_ALERT("Fatal: MaxScale " MAXSCALE_VERSION " received fatal signal %d. "
"Attempting backtrace.", i);

View File

@ -572,6 +572,10 @@ static bool logmanager_init_nomutex(const char* ident,
succ = true;
lm->lm_enabled = true;
// Redirect stdout and stderr to the log file
freopen(lm->lm_logfile.lf_full_file_name, "a", stdout);
freopen(lm->lm_logfile.lf_full_file_name, "a", stderr);
return_succ:
if (err != 0)
{
@ -2155,6 +2159,12 @@ static bool thr_flush_file(logmanager_t *lm, filewriter_t *fwr)
LOG_ERROR("MaxScale Log: Error, could not re-open log file %s.\n",
lf->lf_full_file_name);
}
else
{
// Redirect stdout and stderr to the new log file
freopen(lf->lf_full_file_name, "a", stdout);
freopen(lf->lf_full_file_name, "a", stderr);
}
}
return true;

View File

@ -839,7 +839,6 @@ void skygw_file_close(skygw_file_t* file)
}
else
{
ss_dfprintf(stderr, "Closed %s\n", file->sf_fname);
skygw_file_free(file);
}
}