diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index a3584c072..3534f60e5 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -94,6 +94,12 @@ static struct */ int lm_enabled_logfiles_bitmask = 0; +/** + * Variable holding the enabled priorities information. + * Used from logging macros. + */ +int lm_enabled_priorities_bitmask = 0; + /** * Thread-specific struct variable for storing current session id and currently * enabled log files for the session. @@ -310,7 +316,6 @@ static char* blockbuf_get_writepos(blockbuf_t** p_bb, static void blockbuf_register(blockbuf_t* bb); static void blockbuf_unregister(blockbuf_t* bb); -static bool logfile_set_enabled(logfile_id_t id, bool val); static char* add_slash(char* str); static bool check_file_and_path(char* filename, @@ -318,7 +323,6 @@ static bool check_file_and_path(char* filename, bool do_log); static bool file_is_symlink(char* filename); -static int skygw_log_disable_raw(logfile_id_t id, bool emergency); /*< no locking */ static int find_last_seqno(strpart_t* parts, int seqno, int seqnoidx); void flushall_logfiles(bool flush); bool thr_flushall_check(); @@ -1150,7 +1154,7 @@ static blockbuf_t* blockbuf_init() } -int skygw_log_enable(logfile_id_t id) +static int skygw_log_enable(logfile_id_t id) { bool rval = -1; @@ -1158,14 +1162,11 @@ int skygw_log_enable(logfile_id_t id) { CHK_LOGMANAGER(lm); - if (logfile_set_enabled(id, true)) - { - lm->lm_enabled_logfiles |= id; - /** - * Set global variable - */ - lm_enabled_logfiles_bitmask = lm->lm_enabled_logfiles; - } + lm->lm_enabled_logfiles |= id; + /** + * Set global variable + */ + lm_enabled_logfiles_bitmask = lm->lm_enabled_logfiles; logmanager_unregister(); rval = 0; @@ -1174,12 +1175,7 @@ int skygw_log_enable(logfile_id_t id) return rval; } -int skygw_log_disable(logfile_id_t id) /*< no locking */ -{ - return skygw_log_disable_raw(id, false); -} - -static int skygw_log_disable_raw(logfile_id_t id, bool emergency) /*< no locking */ +static int skygw_log_disable(logfile_id_t id) { bool rval = -1; @@ -1187,14 +1183,11 @@ static int skygw_log_disable_raw(logfile_id_t id, bool emergency) /*< no locking { CHK_LOGMANAGER(lm); - if (emergency || logfile_set_enabled(id, false)) - { - lm->lm_enabled_logfiles &= ~id; - /** - * Set global variable - */ - lm_enabled_logfiles_bitmask = lm->lm_enabled_logfiles; - } + lm->lm_enabled_logfiles &= ~id; + /** + * Set global variable + */ + lm_enabled_logfiles_bitmask = lm->lm_enabled_logfiles; logmanager_unregister(); rval = 0; @@ -1204,64 +1197,6 @@ static int skygw_log_disable_raw(logfile_id_t id, bool emergency) /*< no locking } -static bool logfile_set_enabled(logfile_id_t id, bool val) -{ - bool rval = false; - - CHK_LOGMANAGER(lm); - - if (logmanager_is_valid_id(id)) - { - if (!log_config.use_stdout) - { - const char *name; - - switch (id) - { - default: - case LOGFILE_ERROR: - name = "LOGFILE_ERROR"; - break; - - case LOGFILE_MESSAGE: - name = "LOGFILE_MESSAGE"; - break; - - case LOGFILE_TRACE: - name = "LOGFILE_TRACE"; - break; - - case LOGFILE_DEBUG: - name = "LOGFILE_DEBUG"; - break; - } - - const char FORMAT[] = "The logging of %s messages is %s."; - const char *action; - - if (val) - { - action = "enabled"; - } - else - { - action = "disabled"; - } - - MXS_NOTICE(FORMAT, name, action); - } - - rval = true; - } - else - { - MXS_ERROR("Invalid logfile id %d.", id); - ss_dassert(!true); - } - - return rval; -} - /** * Set log augmentation. * @@ -2309,7 +2244,7 @@ static bool thr_flush_file(logmanager_t *lm, filewriter_t *fwr) err, strerror_r(err, errbuf, sizeof(errbuf))); /** Force log off */ - skygw_log_disable_raw(LOGFILE_ERROR, true); + skygw_log_disable(LOGFILE_ERROR); } /** * Reset buffer's counters and mark @@ -2702,44 +2637,50 @@ int mxs_log_rotate() return err; } -static bool convert_priority_to_file(int priority, logfile_id_t* idp, const char** textp) +static logfile_id_t priority_to_file_id(int priority) { - bool converted = true; - - *idp = (logfile_id_t) -1; - *textp = NULL; - switch (priority) { case LOG_DEBUG: - *idp = LOGFILE_DEBUG; - break; + return LOGFILE_DEBUG; case LOG_INFO: - *idp = LOGFILE_TRACE; - break; + return LOGFILE_TRACE; case LOG_NOTICE: - *idp = LOGFILE_MESSAGE; - break; + return LOGFILE_MESSAGE; case LOG_ERR: - *idp = LOGFILE_ERROR; - break; case LOG_WARNING: - *textp = "LOG_WARNING"; - break; case LOG_CRIT: - *textp = "LOG_CRIT"; - break; case LOG_ALERT: - *textp = "LOG_ALERT"; - break; case LOG_EMERG: - *textp = "LOG_EMERG"; - break; default: - converted = false; + return LOGFILE_ERROR; } +} - return converted; +static const char* priority_name(int priority) +{ + switch (priority) + { + case LOG_EMERG: + return "emercency"; + case LOG_ALERT: + return "alert"; + case LOG_CRIT: + return "critical"; + case LOG_ERR: + return "error"; + case LOG_WARNING: + return "warning"; + case LOG_NOTICE: + return "notice"; + case LOG_INFO: + return "informational"; + case LOG_DEBUG: + return "debug"; + default: + assert(!true); + return "unknown"; + } } /** @@ -2750,34 +2691,33 @@ static bool convert_priority_to_file(int priority, logfile_id_t* idp, const char * * @return 0 if the priority was valid, -1 otherwise. */ -int mxs_log_set_priority_enabled(int priority, bool enabled) +int mxs_log_set_priority_enabled(int priority, bool enable) { int rv = -1; - logfile_id_t id; - const char* text; + const char* text = (enable ? "enable" : "disable"); - if (convert_priority_to_file(priority, &id, &text)) + if ((priority & ~LOG_PRIMASK) == 0) { - if (!text) + logfile_id_t id = priority_to_file_id(priority); + int bit = (1 << priority); + + if (enable) { - if (enabled) - { - rv = skygw_log_enable(id); - } - else - { - rv = skygw_log_disable(id); - } + // TODO: Put behind spinlock. + lm_enabled_priorities_bitmask |= bit; + skygw_log_enable(id); } else { - MXS_WARNING("Attempt to enable syslog priority %s, which is not available yet.", text); - rv = 0; + lm_enabled_priorities_bitmask &= ~bit; + skygw_log_disable(id); } + + MXS_NOTICE("The logging of %s messages has been %sd.", priority_name(priority), text); } else { - MXS_ERROR("Attempt to enable unknown syslog priority: %d", priority); + MXS_ERROR("Attempt to %s unknown syslog priority %d.", text, priority); } return rv; diff --git a/log_manager/log_manager.h b/log_manager/log_manager.h index 51f7b2f14..cec3b12df 100644 --- a/log_manager/log_manager.h +++ b/log_manager/log_manager.h @@ -27,6 +27,21 @@ #define STRERROR_BUFLEN 512 #endif +enum mxs_log_priorities +{ + MXS_LOG_EMERG = (1 << LOG_EMERG), + MXS_LOG_ALERT = (1 << LOG_ALERT), + MXS_LOG_CRIT = (1 << LOG_CRIT), + MXS_LOG_ERR = (1 << LOG_ERR), + MXS_LOG_WARNING = (1 << LOG_WARNING), + MXS_LOG_NOTICE = (1 << LOG_NOTICE), + MXS_LOG_INFO = (1 << LOG_INFO), + MXS_LOG_DEBUG = (1 << LOG_DEBUG), + + MXS_LOG_MASK = (MXS_LOG_EMERG | MXS_LOG_ALERT | MXS_LOG_CRIT | MXS_LOG_ERR | + MXS_LOG_WARNING | MXS_LOG_NOTICE | MXS_LOG_INFO | MXS_LOG_DEBUG), +}; + typedef enum { BB_READY = 0x00, @@ -80,7 +95,6 @@ typedef struct log_info (log_ses_count[id] > 0 && \ tls_log_info.li_enabled_logs & id)) ? true : false) - #define LOG_MAY_BE_ENABLED(id) (((lm_enabled_logfiles_bitmask & id) || \ log_ses_count[id] > 0) ? true : false) @@ -119,6 +133,7 @@ typedef enum EXTERN_C_BLOCK_BEGIN +extern int lm_enabled_priorities_bitmask; extern int lm_enabled_logfiles_bitmask; extern ssize_t log_ses_count[]; extern __thread log_info_t tls_log_info; @@ -140,9 +155,6 @@ int mxs_log_message(int priority, const char* file, int line, const char* function, const char* format, ...); -int skygw_log_enable(logfile_id_t id); -int skygw_log_disable(logfile_id_t id); - inline int mxs_log_id_to_priority(logfile_id_t id) { if (id & LOGFILE_ERROR) return LOG_ERR; diff --git a/log_manager/test/testlog.c b/log_manager/test/testlog.c index ddf1c410a..df44490d1 100644 --- a/log_manager/test/testlog.c +++ b/log_manager/test/testlog.c @@ -62,6 +62,38 @@ const int N_THR = 4; #define TEST_ERROR(msg)\ do { fprintf(stderr, "[%s:%d]: %s\n", basename(__FILE__), __LINE__, msg); } while (false) +static logfile_id_t id_to_priority(logfile_id_t id) +{ + switch (id) + { + case LOGFILE_ERROR: + return LOG_ERR; + + case LOGFILE_MESSAGE: + return LOG_NOTICE; + + case LOGFILE_TRACE: + return LOG_INFO; + + case LOGFILE_DEBUG: + return LOG_DEBUG; + + default: + assert(!true); + return LOG_ERR; + } +} + +static void skygw_log_enable(logfile_id_t id) +{ + mxs_log_set_priority_enabled(id_to_priority(id), true); +} + +static void skygw_log_disable(logfile_id_t id) +{ + mxs_log_set_priority_enabled(id_to_priority(id), false); +} + int main(int argc, char* argv[]) { int err = 0; diff --git a/log_manager/test/testorder.c b/log_manager/test/testorder.c index 06494cfdd..6514ecf76 100644 --- a/log_manager/test/testorder.c +++ b/log_manager/test/testorder.c @@ -23,6 +23,38 @@ #include #include +static logfile_id_t id_to_priority(logfile_id_t id) +{ + switch (id) + { + case LOGFILE_ERROR: + return LOG_ERR; + + case LOGFILE_MESSAGE: + return LOG_NOTICE; + + case LOGFILE_TRACE: + return LOG_INFO; + + case LOGFILE_DEBUG: + return LOG_DEBUG; + + default: + assert(!true); + return LOG_ERR; + } +} + +static void skygw_log_enable(logfile_id_t id) +{ + mxs_log_set_priority_enabled(id_to_priority(id), true); +} + +static void skygw_log_disable(logfile_id_t id) +{ + mxs_log_set_priority_enabled(id_to_priority(id), false); +} + int main(int argc, char** argv) { int iterations = 0, i, interval = 10; diff --git a/server/core/config.c b/server/core/config.c index 0d659dbd7..5dc866898 100644 --- a/server/core/config.c +++ b/server/core/config.c @@ -1596,12 +1596,16 @@ config_get_feedback_data() static struct { - char *logname; - logfile_id_t logfile; + char* name; + int priority; + char* replacement; } lognames[] = { - { "log_messages", LOGFILE_MESSAGE }, - { "log_trace", LOGFILE_TRACE }, - { "log_debug", LOGFILE_DEBUG }, + { "log_messages", LOG_NOTICE, "log_notice" }, // Deprecated + { "log_trace", LOG_INFO, "log_info" }, // Deprecated + { "log_debug", LOG_DEBUG, NULL }, + { "log_warning", LOG_WARNING, NULL }, + { "log_notice", LOG_NOTICE, NULL }, + { "log_info", LOG_INFO, NULL }, { NULL, 0 } }; /** @@ -1681,18 +1685,18 @@ handle_global_item(const char *name, const char *value) } else { - for (i = 0; lognames[i].logname; i++) + for (i = 0; lognames[i].name; i++) { - if (strcasecmp(name, lognames[i].logname) == 0) + if (strcasecmp(name, lognames[i].name) == 0) { - if (config_truth_value((char*)value)) + if (lognames[i].replacement) { - skygw_log_enable(lognames[i].logfile); - } - else - { - skygw_log_disable(lognames[i].logfile); + MXS_WARNING("In the configuration file the use of '%s' is deprecated, " + "use '%s' instead.", + lognames[i].name, lognames[i].replacement); } + + mxs_log_set_priority_enabled(lognames[i].priority, config_truth_value((char*)value)); } } } diff --git a/server/modules/routing/binlog/maxbinlogcheck.c b/server/modules/routing/binlog/maxbinlogcheck.c index 8c8e0428f..3116bc845 100644 --- a/server/modules/routing/binlog/maxbinlogcheck.c +++ b/server/modules/routing/binlog/maxbinlogcheck.c @@ -125,13 +125,8 @@ int main(int argc, char **argv) { num_args = optind; mxs_log_init(NULL, NULL, LOG_TARGET_DEFAULT); - mxs_log_set_augmentation(0); - - if (!debug_out) - skygw_log_disable(LOGFILE_DEBUG); - else - skygw_log_enable(LOGFILE_DEBUG); + mxs_log_set_priority_enabled(LOG_DEBUG, debug_out); if ((inst = calloc(1, sizeof(ROUTER_INSTANCE))) == NULL) { LOGIF(LE, (skygw_log_write_flush(LOGFILE_ERROR, diff --git a/server/modules/routing/binlog/test/testbinlog.c b/server/modules/routing/binlog/test/testbinlog.c index ee4609517..257994351 100644 --- a/server/modules/routing/binlog/test/testbinlog.c +++ b/server/modules/routing/binlog/test/testbinlog.c @@ -91,10 +91,10 @@ int main(int argc, char **argv) { mxs_log_init(NULL, NULL, LOG_TARGET_DEFAULT); - skygw_log_disable(LOGFILE_DEBUG); - skygw_log_disable(LOGFILE_TRACE); - skygw_log_disable(LOGFILE_ERROR); - skygw_log_disable(LOGFILE_MESSAGE); + mxs_log_set_priority_enabled(LOG_DEBUG, false); + mxs_log_set_priority_enabled(LOG_INFO, false); + mxs_log_set_priority_enabled(LOG_NOTICE, false); + mxs_log_set_priority_enabled(LOG_ERR, false); service = service_alloc("test_service", "binlogrouter"); service->credentials.name = strdup("foo"); diff --git a/server/modules/routing/debugcmd.c b/server/modules/routing/debugcmd.c index 51dbbf6cb..b21e31b47 100644 --- a/server/modules/routing/debugcmd.c +++ b/server/modules/routing/debugcmd.c @@ -410,22 +410,20 @@ struct subcommand enableoptions[] = { "log", 1, enable_log_action, - "Enable Log options for MaxScale, options trace | error | " - "message E.g. enable log message.", - "Enable Log options for MaxScale, options trace | error | " - "message E.g. enable log message.", + "[deprecated] Enable Log options for MaxScale, options 'trace' | 'error' | 'message'." + "E.g. 'enable log message'.", + "[deprecated] Enable Log options for MaxScale, options 'trace' | 'error' | 'message'." + "E.g. 'enable log message'.", {ARG_TYPE_STRING, 0, 0} }, { "log-priority", 1, enable_log_priority, - "Enable log priority for MaxScale; options LOG_ERR | " - "LOG_WARNING | LOG_NOTICE | LOG_INFO | LOG_DEBUG. " - "E.g.: enable log-priority LOG_INFO.", - "Enable log priority for MaxScale; options LOG_ERR | " - "LOG_WARNING | LOG_NOTICE | LOG_INFO | LOG_DEBUG. " - "E.g.: enable log-priority LOG_INFO.", + "Enable a logging priority; options 'err' | 'warning' | 'notice' | 'info' | 'debug'. " + "E.g.: 'enable log-priority info'.", + "Enable a logging priority; options 'err' | 'warning' | 'notice' | 'info' | 'debug'. " + "E.g.: 'enable log-priority info'.", {ARG_TYPE_STRING, 0, 0} }, { @@ -482,22 +480,20 @@ struct subcommand disableoptions[] = { "log", 1, disable_log_action, - "Disable Log for MaxScale, Options: debug | trace | error | message " - "E.g. disable log debug", - "Disable Log for MaxScale, Options: debug | trace | error | message " - "E.g. disable log debug", + "[deprecated] Disable Log for MaxScale, Options: 'debug' | 'trace' | 'error' | 'message'." + "E.g. 'disable log debug'.", + "[deprecated] Disable Log for MaxScale, Options: 'debug' | 'trace' | 'error' | 'message'." + "E.g. 'disable log debug'.", {ARG_TYPE_STRING, 0, 0} }, { "log-priority", 1, disable_log_priority, - "Disable log priority for MaxScale; options LOG_ERR | " - "LOG_WARNING | LOG_NOTICE | LOG_INFO | LOG_DEBUG. " - "E.g.: enable log-priority LOG_INFO.", - "Disable log priority for MaxScale; options LOG_ERR | " - "LOG_WARNING | LOG_NOTICE | LOG_INFO | LOG_DEBUG. " - "E.g.: enable log-priority LOG_INFO.", + "Disable a logging priority; options 'err' | 'warning' | 'notice' | 'info' | 'debug'. " + "E.g.: 'disable log-priority info'.", + "Disable a logging priority; options 'err' | 'warning' | 'notice' | 'info' | 'debug'. " + "E.g.: 'disable log-priority info'.", {ARG_TYPE_STRING, 0, 0} }, { @@ -962,7 +958,7 @@ execute_cmd(CLI_SESSION *cli) dcb_printf(dcb, "Available options to the %s command:\n", args[1]); for (j = 0; cmds[i].options[j].arg1; j++) { - dcb_printf(dcb, " %-10s %s\n", cmds[i].options[j].arg1, + dcb_printf(dcb, " %-12s %s\n", cmds[i].options[j].arg1, cmds[i].options[j].help); } } @@ -1449,98 +1445,87 @@ static void disable_sess_log_action(DCB *dcb, char *arg1, char *arg2) dcb_printf(dcb, "Session not found: %s\n", arg2); } +struct log_action_entry +{ + const char* name; + int priority; + const char* replacement; +}; + +static bool get_log_action(const char* name, struct log_action_entry* entryp) +{ + static const struct log_action_entry entries[] = + { + { "debug", LOG_DEBUG, "debug" }, + { "trace", LOG_INFO, "info" }, + { "message", LOG_NOTICE, "notice" }, + { "error", LOG_ERR, "err" } + }; + const int n_entries = sizeof(entries) / sizeof(entries[0]); + + bool found = false; + int i = 0; + + while (!found && (i < n_entries)) + { + if (strcmp(name, entries[i].name) == 0) + { + *entryp = entries[i]; + found = true; + } + + ++i; + } + + return found; +} + /** * The log enable action */ - static void enable_log_action(DCB *dcb, char *arg1) { - logfile_id_t type = -1; - int max_len = strlen("message"); - const char* priority; + struct log_action_entry entry; - if (strncmp(arg1, "debug", max_len) == 0) + if (get_log_action(arg1, &entry)) { - type = LOGFILE_DEBUG; - priority = "LOG_DEBUG"; - } - else if (strncmp(arg1, "trace", max_len) == 0) - { - type = LOGFILE_TRACE; - priority = "LOG_INFO"; - } - else if (strncmp(arg1, "error", max_len) == 0) - { - type = LOGFILE_ERROR; - priority = "LOG_ERR"; - } - else if (strncmp(arg1, "message", max_len) == 0) - { - type = LOGFILE_MESSAGE; - priority = "LOG_NOTICE"; - } + mxs_log_set_priority_enabled(entry.priority, true); - if (type != -1) - { - skygw_log_enable(type); dcb_printf(dcb, "'enable log %s' is accepted but deprecated, use 'enable log-priority %s' instead.\n", - arg1, priority); + arg1, entry.replacement); } else { - dcb_printf(dcb, "%s is not supported for enable log\n", arg1); + dcb_printf(dcb, "'%s' is not supported for enable log\n", arg1); } } /** * The log disable action */ - static void disable_log_action(DCB *dcb, char *arg1) { - logfile_id_t type = -1; - int max_len = strlen("message"); - const char* priority; + struct log_action_entry entry; - if (strncmp(arg1, "debug", max_len) == 0) + if (get_log_action(arg1, &entry)) { - type = LOGFILE_DEBUG; - priority = "LOG_DEBUG"; - } - else if (strncmp(arg1, "trace", max_len) == 0) - { - type = LOGFILE_TRACE; - priority = "LOG_INFO"; - } - else if (strncmp(arg1, "error", max_len) == 0) - { - type = LOGFILE_ERROR; - priority = "LOG_ERR"; - } - else if (strncmp(arg1, "message", max_len) == 0) - { - type = LOGFILE_MESSAGE; - priority = "LOG_NOTICE"; - } + mxs_log_set_priority_enabled(entry.priority, false); - if (type != -1) - { - skygw_log_disable(type); dcb_printf(dcb, - "'disable log %s' is accepted but deprecated, use 'disable log-priority %s' instead.\n", - arg1, priority); + "'disable log %s' is accepted but deprecated, use 'enable log-priority %s' instead.\n", + arg1, entry.replacement); } else { - dcb_printf(dcb, "%s is not supported for disable log\n", arg1); + dcb_printf(dcb, "'%s' is not supported for 'disable log'\n", arg1); } } struct log_priority_entry { - int priority; const char* name; + int priority; }; static int compare_log_priority_entries(const void* l, const void* r) @@ -1556,16 +1541,16 @@ static int string_to_priority(const char* name) static const struct log_priority_entry LOG_PRIORITY_ENTRIES[] = { // NOTE: If you make changes to this array, ensure that it remains alphabetically ordered. - { LOG_DEBUG, "LOG_DEBUG" }, - { LOG_ERR, "LOG_ERR" }, - { LOG_INFO, "LOG_INFO" }, - { LOG_NOTICE, "LOG_NOTICE" }, - { LOG_WARNING, "LOG_WARNING" }, + { "debug", LOG_DEBUG }, + { "err", LOG_ERR }, + { "info", LOG_INFO }, + { "notice", LOG_NOTICE }, + { "warning", LOG_WARNING }, }; const size_t N_LOG_PRIORITY_ENTRIES = sizeof(LOG_PRIORITY_ENTRIES) / sizeof(LOG_PRIORITY_ENTRIES[0]); - struct log_priority_entry key = { -1, name }; + struct log_priority_entry key = { name, -1 }; struct log_priority_entry* result = bsearch(&key, LOG_PRIORITY_ENTRIES, N_LOG_PRIORITY_ENTRIES, @@ -1589,7 +1574,7 @@ static void enable_log_priority(DCB *dcb, char *arg1) } else { - dcb_printf(dcb, "%s is not a supported log priority\n", arg1); + dcb_printf(dcb, "'%s' is not a supported log priority\n", arg1); } } @@ -1607,7 +1592,7 @@ static void disable_log_priority(DCB *dcb, char *arg1) } else { - dcb_printf(dcb, "%s is not a supported log priority\n", arg1); + dcb_printf(dcb, "'%s' is not a supported log priority\n", arg1); } }