Log: Another step in the move from logfiles to priorities.

skygw_[enable|disable]_log has now been removed from the external
interface and priorities must instead be set using
mxs_log_set_priority_enabled(int priority, bool enabled). A bitmask
is already being updated, but internally and as used by the LOG_IF
macros, the actual enabling is still made using logfile ids.

The configuration entries have been replaced as follows:

	log_messages -> log_notice
	log_trace    -> log_info

The old ones can be used, but cause a warning to be logged.

Similarily the maxadmin commands have been updated.
"[enable|disable] log ..." works as expected, but there will be
a message about it being deprecated. Instead there is now a
[enable|disable] log-priority err|warning|notice|info|debug
command that should be used instead.
This commit is contained in:
Johan Wikman 2015-11-13 13:47:10 +02:00
parent 2dcdab29b6
commit bcb918e60b
8 changed files with 238 additions and 238 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -23,6 +23,38 @@
#include <skygw_utils.h>
#include <log_manager.h>
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;

View File

@ -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));
}
}
}

View File

@ -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,

View File

@ -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");

View File

@ -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);
}
}