From 55dbaa49c0fe8af4a73c9ff467a80377dfa83d20 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Mon, 9 Nov 2015 15:53:22 +0200 Subject: [PATCH] Logging target must be explicitly defined. Whether the log-file should be written to the filesystem or to shared memory must now be explicitly defined when calling skygw_logmanager_init() (instead of passing that via the argc/argv construct). Also, the meaning of '-l' when invoking maxscale has been changed. Earlier -l [file|shm] specified whether the trace and debug logs should be written to shared memory (while the error and message logs always were written to the filesystem) and the _default_ was to write them to shared memory. Now, with only one file, '-l' has still the same meaning, but it decides whether the one and only logfile should be written to shared memory, or the filesystem and the _default_ is to write it to the filesystem. --- log_manager/log_manager.cc | 48 ++++++------------- log_manager/log_manager.h | 10 +++- log_manager/test/testlog.c | 40 ++++++++-------- log_manager/test/testorder.c | 2 +- server/core/gateway.c | 30 +++--------- server/core/maxkeys.c | 2 +- server/core/maxpasswd.c | 2 +- server/include/test_utils.h | 2 +- server/modules/filter/test/harness_common.c | 4 +- .../modules/routing/binlog/maxbinlogcheck.c | 2 +- .../modules/routing/binlog/test/testbinlog.c | 2 +- 11 files changed, 58 insertions(+), 86 deletions(-) diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index 454a98800..72af88911 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -90,8 +90,6 @@ ssize_t log_ses_count[LOGFILE_LAST] = {0}; */ const char* shm_pathname_prefix = "/dev/shm/"; -/** Logfile ids from call argument '-s' */ -char* shmem_id_str = NULL; /** Errors are written to syslog too by default */ char* syslog_id_str = strdup("LOGFILE_ERROR"); char* syslog_ident_str = NULL; @@ -231,6 +229,7 @@ struct logmanager fnames_conf_t lm_fnames_conf; logfile_t lm_logfile; filewriter_t lm_filewriter; + log_target_t lm_target; #if defined(SS_DEBUG) skygw_chk_t lm_chk_tail; #endif @@ -274,7 +273,7 @@ static void* thr_filewriter_fun(void* data); static logfile_t* logmanager_get_logfile(logmanager_t* lm); static bool logmanager_register(bool writep); static void logmanager_unregister(void); -static bool logmanager_init_nomutex(const char* logdir, int argc, char* argv[]); +static bool logmanager_init_nomutex(const char* logdir, log_target_t target, int argc, char* argv[]); static void logmanager_done_nomutex(void); static bool logmanager_is_valid_id(logfile_id_t id); @@ -305,7 +304,7 @@ static int find_last_seqno(strpart_t* parts, int seqno, int seqnoidx); void flushall_logfiles(bool flush); bool thr_flushall_check(); -static bool logmanager_init_nomutex(const char* logdir, int argc, char* argv[]) +static bool logmanager_init_nomutex(const char* logdir, log_target_t target, int argc, char* argv[]) { fnames_conf_t* fn; filewriter_t* fw; @@ -319,6 +318,8 @@ static bool logmanager_init_nomutex(const char* logdir, int argc, char* argv[]) err = 1; goto return_succp; } + + lm->lm_target = (target == LOG_TARGET_DEFAULT ? LOG_TARGET_FS : target); #if defined(SS_DEBUG) lm->lm_chk_top = CHK_NUM_LOGMANAGER; lm->lm_chk_tail = CHK_NUM_LOGMANAGER; @@ -414,13 +415,15 @@ return_succp: * Initializes log managing routines in MariaDB Corporation MaxScale. * * @param logdir The directory for the log file. If NULL logging will be made to stdout. - * @param argc number of arguments in argv array - * @param argv arguments array + * @param target Whether the log should be written to filesystem or shared memory. + * Meaningless if logdir is NULL. + * @param argc Number of arguments in argv array. + * @param argv Arguments array. * * @return true if succeed, otherwise false * */ -bool skygw_logmanager_init(const char* logdir, int argc, char* argv[]) +bool skygw_logmanager_init(const char* logdir, log_target_t target, int argc, char* argv[]) { bool succp = false; @@ -432,7 +435,7 @@ bool skygw_logmanager_init(const char* logdir, int argc, char* argv[]) goto return_succp; } - succp = logmanager_init_nomutex(logdir, argc, argv); + succp = logmanager_init_nomutex(logdir, target, argc, argv); return_succp: release_lock(&lmlock); @@ -1620,7 +1623,7 @@ static bool logmanager_register(bool writep) // If someone is logging before the log manager has been inited, // or after the log manager has been finished, the messages are // written to stdout. - succp = logmanager_init_nomutex(NULL, 0, NULL); + succp = logmanager_init_nomutex(NULL, LOG_TARGET_DEFAULT, 0, NULL); } } /** if logmanager existed or was succesfully restarted, increase link */ @@ -1687,8 +1690,7 @@ static bool fnames_conf_init(fnames_conf_t* fn, const char* argstr = "-h - help\n" "-l .......(no default)\n" - "-m ............(argv[0])\n" - "-s .......(no default)\n"; + "-m ............(argv[0])\n"; /** * When init_started is set, clean must be done for it. @@ -1700,7 +1702,7 @@ static bool fnames_conf_init(fnames_conf_t* fn, #endif optind = 1; /**lm_target == LOG_TARGET_SHMEM); bool write_syslog; /** Open syslog immediately. Print pid of loggind process. */ @@ -1811,23 +1809,7 @@ static bool logfiles_init(logmanager_t* lm) { openlog(syslog_ident_str, LOG_PID | LOG_NDELAY, LOG_USER); } - /** - * Initialize log file, pass softlink flag if necessary. - */ - /** - * Check if the file is stored in shared memory. If so, - * a symbolic link will be created to log directory. - */ - if (shmem_id_str != NULL && - strcasestr(shmem_id_str, STRLOGID(LOGFILE_ERROR)) != NULL) - { - store_shmem = true; - } - else - { - store_shmem = false; - } /** * Check if file is also written to syslog. */ diff --git a/log_manager/log_manager.h b/log_manager/log_manager.h index f17a32d02..ec75eb81c 100644 --- a/log_manager/log_manager.h +++ b/log_manager/log_manager.h @@ -42,7 +42,6 @@ typedef enum LOGFILE_LAST = LOGFILE_DEBUG } logfile_id_t; - typedef enum { FILEWRITER_INIT, @@ -50,6 +49,13 @@ typedef enum FILEWRITER_DONE } filewriter_state_t; +typedef enum +{ + LOG_TARGET_DEFAULT = 0, + LOG_TARGET_FS = 1, // File system + LOG_TARGET_SHMEM = 2, // Shared memory +} log_target_t; + /** * Thread-specific logging information. */ @@ -145,7 +151,7 @@ int mxs_log_rotate(); int mxs_log_enable_priority(int priority); int mxs_log_disable_priority(int priority); -bool skygw_logmanager_init(const char* logdir, int argc, char* argv[]); +bool skygw_logmanager_init(const char* logdir, log_target_t target, int argc, char* argv[]); void skygw_logmanager_done(void); void skygw_logmanager_exit(void); diff --git a/log_manager/test/testlog.c b/log_manager/test/testlog.c index 4f94b0cee..496e25aa2 100644 --- a/log_manager/test/testlog.c +++ b/log_manager/test/testlog.c @@ -122,7 +122,7 @@ int main(int argc, char* argv[]) fprintf(stderr, "Couldn't register exit function.\n"); } - succp = skygw_logmanager_init("/tmp", log_argc, log_argv); + succp = skygw_logmanager_init("/tmp", LOG_TARGET_FS, log_argc, log_argv); if (!succp) { @@ -141,7 +141,7 @@ int main(int argc, char* argv[]) tm.tm_min, tm.tm_sec); - skygw_logmanager_init("/tmp", log_argc, log_argv); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, log_argc, log_argv); logstr = ("First write with flush."); err = skygw_log_write_flush(LOGFILE_ERROR, logstr); @@ -189,7 +189,7 @@ int main(int argc, char* argv[]) logstr = "Ph%dlip."; err = skygw_log_write(LOGFILE_TRACE, logstr, 1); - skygw_logmanager_init("/tmp", log_argc, log_argv); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, log_argc, log_argv); logstr = ("A terrible error has occurred!"); err = skygw_log_write_flush(LOGFILE_ERROR, logstr); @@ -335,7 +335,7 @@ int main(int argc, char* argv[]) #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); #endif - succp = skygw_logmanager_init("/tmp", log_argc, log_argv); + succp = skygw_logmanager_init("/tmp", LOG_TARGET_FS, log_argc, log_argv); ss_dassert(succp); logstr = ("\tTEST 3 - test enabling and disabling logs."); @@ -400,7 +400,7 @@ int main(int argc, char* argv[]) #endif /* TEST 3 */ #if defined(TEST4) - succp = skygw_logmanager_init("/tmp", log_argc, log_argv); + succp = skygw_logmanager_init("/tmp", LOG_TARGET_FS, log_argc, log_argv); ss_dassert(succp); #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); @@ -436,7 +436,7 @@ int main(int argc, char* argv[]) skygw_logmanager_done(); - succp = skygw_logmanager_init("/tmp", log_argc, log_argv); + succp = skygw_logmanager_init("/tmp", LOG_TARGET_FS, log_argc, log_argv); ss_dassert(succp); #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); @@ -509,7 +509,7 @@ static void* thr_run(void* data) char* logstr; int err; - skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); skygw_log_flush(LOGFILE_MESSAGE); logstr = ("Hi, how are you?"); err = skygw_log_write(LOGFILE_MESSAGE, logstr); @@ -533,7 +533,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); err = skygw_log_write(LOGFILE_MESSAGE, logstr); - skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("Testing. One, two, three\n"); err = skygw_log_write(LOGFILE_ERROR, logstr); if (err != 0) @@ -541,8 +541,8 @@ static void* thr_run(void* data) TEST_ERROR("Error, log write failed."); } ss_dassert(err == 0); - skygw_logmanager_init("/tmp", 0, NULL); - skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); skygw_log_flush(LOGFILE_ERROR); logstr = ("For automatic and register variables, it is done each time the function or block is entered."); @@ -556,7 +556,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("Rather more surprising, at least at first sight, is the fact that a reference " "to a[i] can also be written as *(a+i). In evaluating a[i], C converts it to *(a+i) " "immediately; the two forms are equivalent. Applying the operatos & to both parts " @@ -568,11 +568,11 @@ static void* thr_run(void* data) TEST_ERROR("Error, log write failed."); } ss_dassert(err == 0); - skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); skygw_logmanager_done(); skygw_log_flush(LOGFILE_ERROR); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("..and you?"); err = skygw_log_write(LOGFILE_MESSAGE, logstr); if (err != 0) @@ -581,7 +581,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("For automatic and register variables, it is done each time the function or block is entered."); #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); @@ -592,7 +592,7 @@ static void* thr_run(void* data) TEST_ERROR("Error, log write failed."); } ss_dassert(err == 0); - skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("Rather more surprising, at least at first sight, is the fact that a reference to " "a[i] can also be written as *(a+i). In evaluating a[i], C converts it to *(a+i) " "immediately; the two forms are equivalent. Applying the operatos & to both parts " @@ -604,7 +604,7 @@ static void* thr_run(void* data) TEST_ERROR("Error, log write failed."); } ss_dassert(err == 0); - skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("..... and you too?"); err = skygw_log_write(LOGFILE_MESSAGE, logstr); if (err != 0) @@ -613,7 +613,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); #endif @@ -629,7 +629,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("Testing. One, two, three, four\n"); err = skygw_log_write(LOGFILE_ERROR, logstr); if (err != 0) @@ -638,7 +638,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("Testing. One, two, three, .. where was I?\n"); err = skygw_log_write(LOGFILE_ERROR, logstr); if (err != 0) @@ -647,7 +647,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); skygw_logmanager_done(); simple_mutex_lock(td->mtx, true); *td->nactive -= 1; diff --git a/log_manager/test/testorder.c b/log_manager/test/testorder.c index edb51fec3..91885f862 100644 --- a/log_manager/test/testorder.c +++ b/log_manager/test/testorder.c @@ -71,7 +71,7 @@ int main(int argc, char** argv) iterations = atoi(argv[1]); interval = atoi(argv[2]); - succp = skygw_logmanager_init(tmp, 1, optstr); + succp = skygw_logmanager_init(tmp, LOG_TARGET_FS, 1, optstr); if (!succp) { diff --git a/server/core/gateway.c b/server/core/gateway.c index cd7e9f64f..49e8cf72b 100644 --- a/server/core/gateway.c +++ b/server/core/gateway.c @@ -1077,7 +1077,7 @@ int main(int argc, char **argv) char* tmp_path; char* tmp_var; int option_index; - int logtofile = 0; /* Use shared memory or file */ + log_target_t log_target = LOG_TARGET_FS; int *syslog_enabled = &config_get_global_options()->syslog; /** Log to syslog */ int *maxscalelog_enabled = &config_get_global_options()->maxlog; /** Log with MaxScale */ ssize_t log_flush_timeout_ms = 0; @@ -1169,9 +1169,9 @@ int main(int argc, char **argv) case 'l': if (strncasecmp(optarg, "file", PATH_MAX) == 0) - logtofile = 1; + log_target = LOG_TARGET_FS; else if (strncasecmp(optarg, "shm", PATH_MAX) == 0) - logtofile = 0; + log_target = LOG_TARGET_SHMEM; else { char* logerr = "Configuration file argument " @@ -1703,8 +1703,6 @@ int main(int argc, char **argv) * argv[0] */ { - char buf[1024]; - char *argv[8]; bool succp; if (mkdir(get_logdir(), 0777) != 0 && errno != EEXIST) @@ -1729,24 +1727,10 @@ int main(int argc, char **argv) logmanager_enable_syslog(*syslog_enabled); logmanager_enable_maxscalelog(*maxscalelog_enabled); - if (logtofile) - { - argv[1] = "-l"; /*< write to syslog */ - /** Logs that should be syslogged */ - argv[2] = "LOGFILE_MESSAGE,LOGFILE_ERROR" - "LOGFILE_DEBUG,LOGFILE_TRACE"; - argv[3] = NULL; - succp = skygw_logmanager_init(get_logdir(), 3, argv); - } - else - { - argv[1] = "-s"; /*< store to shared memory */ - argv[2] = "LOGFILE_DEBUG,LOGFILE_TRACE"; /*< to shm */ - argv[3] = "-l"; /*< write to syslog */ - argv[4] = "LOGFILE_MESSAGE,LOGFILE_ERROR"; /*< to syslog */ - argv[5] = NULL; - succp = skygw_logmanager_init(get_logdir(), 5, argv); - } + char* log_argv[] = { "MaxScale", "-l", "LOGFILE_MESSAGE,LOGFILE_ERROR", NULL }; + int log_argc = sizeof(log_argv) / sizeof(log_argv[0]) - 1; + + succp = skygw_logmanager_init(get_logdir(), log_target, log_argc, log_argv); if (!succp) { diff --git a/server/core/maxkeys.c b/server/core/maxkeys.c index 39651b54c..14850a332 100644 --- a/server/core/maxkeys.c +++ b/server/core/maxkeys.c @@ -60,7 +60,7 @@ int main(int argc, char **argv) arg_vector[0] = "logmanager"; arg_vector[1] = NULL; - skygw_logmanager_init(NULL, arg_count, arg_vector); + skygw_logmanager_init(NULL, LOG_TARGET_DEFAULT, arg_count, arg_vector); free(arg_vector); if (secrets_writeKeys(keyfile)) diff --git a/server/core/maxpasswd.c b/server/core/maxpasswd.c index 96e00dd18..f24ecd21c 100644 --- a/server/core/maxpasswd.c +++ b/server/core/maxpasswd.c @@ -63,7 +63,7 @@ main(int argc, char **argv) arg_vector[0] = "logmanager"; arg_vector[1] = NULL; - skygw_logmanager_init(NULL, arg_count, arg_vector); + skygw_logmanager_init(NULL, LOG_TARGET_DEFAULT, arg_count, arg_vector); free(arg_vector); pw = calloc(81, sizeof(char)); diff --git a/server/include/test_utils.h b/server/include/test_utils.h index 4b1d9bc6f..bd3daa777 100644 --- a/server/include/test_utils.h +++ b/server/include/test_utils.h @@ -20,7 +20,7 @@ void init_test_env(char *path) NULL }; - skygw_logmanager_init(logdir, argc, argv); + skygw_logmanager_init(logdir, LOG_TARGET_DEFAULT, argc, argv); poll_init(); hkinit(); } diff --git a/server/modules/filter/test/harness_common.c b/server/modules/filter/test/harness_common.c index e657fbdd0..6ec0f12a2 100644 --- a/server/modules/filter/test/harness_common.c +++ b/server/modules/filter/test/harness_common.c @@ -18,7 +18,7 @@ int harness_init(int argc, char** argv, HARNESS_INSTANCE** inst){ char** optstr; if(!(argc == 2 && strcmp(argv[1],"-h") == 0)){ - skygw_logmanager_init(NULL,0,NULL); + skygw_logmanager_init(NULL,LOG_TARGET_DEFAULT,0,NULL); } if(!(instance.head = calloc(1,sizeof(FILTERCHAIN)))) @@ -55,7 +55,7 @@ int harness_init(int argc, char** argv, HARNESS_INSTANCE** inst){ optstr = (char**)malloc(sizeof(char*)*2); optstr[0] = strdup("log_manager"); optstr[1] = NULL; - skygw_logmanager_init(tmp, 1, optstr); + skygw_logmanager_init(tmp, LOG_TARGET_DEFAULT, 1, optstr); free(optstr); rval = process_opts(argc,argv); diff --git a/server/modules/routing/binlog/maxbinlogcheck.c b/server/modules/routing/binlog/maxbinlogcheck.c index 50e7e489a..2c8966226 100644 --- a/server/modules/routing/binlog/maxbinlogcheck.c +++ b/server/modules/routing/binlog/maxbinlogcheck.c @@ -136,7 +136,7 @@ int main(int argc, char **argv) { arg_vector[0] = "logmanager"; arg_vector[1] = NULL; - skygw_logmanager_init(NULL, arg_count, arg_vector); + skygw_logmanager_init(NULL, LOG_TARGET_DEFAULT, arg_count, arg_vector); skygw_log_set_augmentation(0); diff --git a/server/modules/routing/binlog/test/testbinlog.c b/server/modules/routing/binlog/test/testbinlog.c index da75005b4..0ee73df77 100644 --- a/server/modules/routing/binlog/test/testbinlog.c +++ b/server/modules/routing/binlog/test/testbinlog.c @@ -101,7 +101,7 @@ int main(int argc, char **argv) { arg_vector[0] = "logmanager"; arg_vector[1] = NULL; - skygw_logmanager_init(NULL, arg_count,arg_vector); + skygw_logmanager_init(NULL, LOG_TARGET_DEFAULT, arg_count,arg_vector); free(arg_vector); skygw_log_disable(LOGFILE_DEBUG);