MXS-2075: Only enable stdout redirection for maxscale

The stdout redirection must not be enabled for the other programs as they
are not run as daemon processes.
This commit is contained in:
Markus Mäkelä
2018-10-01 11:01:09 +03:00
parent 048aee1fb7
commit 288fbc8c33
3 changed files with 19 additions and 4 deletions

View File

@ -106,6 +106,7 @@ void mxs_log_set_maxlog_enabled(bool enabled);
void mxs_log_set_highprecision_enabled(bool enabled); void mxs_log_set_highprecision_enabled(bool enabled);
void mxs_log_set_augmentation(int bits); void mxs_log_set_augmentation(int bits);
void mxs_log_set_throttling(const MXS_LOG_THROTTLING* throttling); void mxs_log_set_throttling(const MXS_LOG_THROTTLING* throttling);
void mxs_log_redirect_stdout(bool redirect);
void mxs_log_get_throttling(MXS_LOG_THROTTLING* throttling); void mxs_log_get_throttling(MXS_LOG_THROTTLING* throttling);
json_t* mxs_logs_to_json(const char* host); json_t* mxs_logs_to_json(const char* host);

View File

@ -1985,6 +1985,11 @@ int main(int argc, char **argv)
log_target = MXS_LOG_TARGET_SHMEM; log_target = MXS_LOG_TARGET_SHMEM;
} }
if (!to_stdout && daemon_mode)
{
mxs_log_redirect_stdout(true);
}
succp = mxs_log_init(NULL, get_logdir(), log_target); succp = mxs_log_init(NULL, get_logdir(), log_target);
if (!succp) if (!succp)

View File

@ -160,6 +160,7 @@ static bool flushall_flag;
static bool flushall_started_flag; static bool flushall_started_flag;
static bool flushall_done_flag; static bool flushall_done_flag;
static HASHTABLE* message_stats; static HASHTABLE* message_stats;
static bool redirect_stdout = false;
/** This is used to detect if the initialization of the log manager has failed /** This is used to detect if the initialization of the log manager has failed
* and that it isn't initialized again after a failure has occurred. */ * and that it isn't initialized again after a failure has occurred. */
@ -572,9 +573,12 @@ static bool logmanager_init_nomutex(const char* ident,
succ = true; succ = true;
lm->lm_enabled = true; lm->lm_enabled = true;
// Redirect stdout and stderr to the log file if (redirect_stdout)
freopen(lm->lm_logfile.lf_full_file_name, "a", stdout); {
freopen(lm->lm_logfile.lf_full_file_name, "a", stderr); // 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: return_succ:
if (err != 0) if (err != 0)
@ -2159,7 +2163,7 @@ static bool thr_flush_file(logmanager_t *lm, filewriter_t *fwr)
LOG_ERROR("MaxScale Log: Error, could not re-open log file %s.\n", LOG_ERROR("MaxScale Log: Error, could not re-open log file %s.\n",
lf->lf_full_file_name); lf->lf_full_file_name);
} }
else else if (redirect_stdout)
{ {
// Redirect stdout and stderr to the new log file // Redirect stdout and stderr to the new log file
freopen(lf->lf_full_file_name, "a", stdout); freopen(lf->lf_full_file_name, "a", stdout);
@ -3131,3 +3135,8 @@ json_t* mxs_logs_to_json(const char* host)
return mxs_json_resource(host, MXS_JSON_API_LOGS, data); return mxs_json_resource(host, MXS_JSON_API_LOGS, data);
} }
void mxs_log_redirect_stdout(bool redirect)
{
redirect_stdout = redirect;
}