diff --git a/include/maxscale/config.h b/include/maxscale/config.h index a91af3f61..ce1c6d381 100644 --- a/include/maxscale/config.h +++ b/include/maxscale/config.h @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -281,6 +282,7 @@ typedef struct char peer_hosts[MAX_ADMIN_HOST_LEN]; /**< The protocol, address and port for peers (currently only one) */ char peer_user[MAX_ADMIN_HOST_LEN]; /**< Username for maxscale-to-maxscale traffic */ char peer_password[MAX_ADMIN_HOST_LEN]; /**< Password for maxscale-to-maxscale traffic */ + mxb_log_target_t log_target; /**< Log type */ } MXS_CONFIG; /** diff --git a/server/core/config.cc b/server/core/config.cc index 76eb3f00f..c04ad0e17 100644 --- a/server/core/config.cc +++ b/server/core/config.cc @@ -2653,6 +2653,7 @@ void config_set_global_defaults() gateway.peer_hosts[0] = '\0'; gateway.peer_user[0] = '\0'; gateway.peer_password[0] = '\0'; + gateway.log_target = MXB_LOG_TARGET_DEFAULT; // Note: This is not a valid cache value: it is used to detect that the default value is used gateway.qc_cache_properties.max_size = -1; diff --git a/server/core/gateway.cc b/server/core/gateway.cc index be97d8b33..f14d9b7d8 100644 --- a/server/core/gateway.cc +++ b/server/core/gateway.cc @@ -707,6 +707,29 @@ retblock: return errstr; } +static bool init_log() +{ + bool rval = false; + MXS_CONFIG* cnf = config_get_global_options(); + + if (!cnf->config_check && mkdir(get_logdir(), 0777) != 0 && errno != EEXIST) + { + fprintf(stderr, "Error: Cannot create log directory '%s': %d, %s\n", + default_logdir, errno, strerror(errno)); + + } + else if (mxs_log_init(NULL, get_logdir(), cnf->log_target)) + { + + mxs_log_set_syslog_enabled(cnf->syslog); + mxs_log_set_maxlog_enabled(cnf->maxlog); + + atexit(mxs_log_finish); + rval = true; + } + + return rval; +} /** * @node Provides error printing for non-formatted error strings. @@ -1333,7 +1356,6 @@ int main(int argc, char **argv) int *maxlog_enabled = &cnf->maxlog; /** Log with MaxScale */ sigset_t sigpipe_mask; sigset_t saved_mask; - bool to_stdout = false; int numlocks = 0; bool pid_file_created = false; mxb::Worker* worker; @@ -1426,17 +1448,17 @@ int main(int argc, char **argv) case 'l': if (strncasecmp(optarg, "file", PATH_MAX) == 0) { - to_stdout = false; + cnf->log_target = MXB_LOG_TARGET_FS; } else if (strncasecmp(optarg, "shm", PATH_MAX) == 0) { // Removed in 2.3 - to_stdout = false; + cnf->log_target = MXB_LOG_TARGET_FS; fprintf(stderr, "Warning: Use of `--log=shm` is deprecated. Data will be logged to file.\n"); } else if (strncasecmp(optarg, "stdout", PATH_MAX) == 0) { - to_stdout = true; + cnf->log_target = MXB_LOG_TARGET_STDOUT; } else { @@ -1680,7 +1702,7 @@ int main(int argc, char **argv) if (cnf->config_check) { daemon_mode = false; - to_stdout = true; + cnf->log_target = MXB_LOG_TARGET_STDOUT; } if (!daemon_mode) @@ -1833,39 +1855,10 @@ int main(int argc, char **argv) goto return_main; } - /** - * Init Log Manager for MaxScale. - */ + if (!init_log()) { - bool succp; - - if (!cnf->config_check && mkdir(get_logdir(), 0777) != 0 && errno != EEXIST) - { - fprintf(stderr, - "Error: Cannot create log directory: %s\n", - default_logdir); - goto return_main; - } - - mxs_log_target_t log_target = MXS_LOG_TARGET_FS; - - if (to_stdout) - { - log_target = MXS_LOG_TARGET_STDOUT; - } - - succp = mxs_log_init(NULL, get_logdir(), log_target); - - if (!succp) - { - rc = MAXSCALE_BADCONFIG; - goto return_main; - } - - mxs_log_set_syslog_enabled(*syslog_enabled); - mxs_log_set_maxlog_enabled(*maxlog_enabled); - - atexit(mxs_log_finish); + rc = MAXSCALE_BADCONFIG; + goto return_main; } if (!config_load_global(cnf_file_path))