log: use facilitynames to set/get log facility (#81)

* log: use facilitynames to set/get log facility

Define our own facilitynames[] instead of defining SYSLOG_NAMES
This commit is contained in:
Tsuyoshi Uchida 2017-01-17 10:20:34 -08:00 committed by Daniel Verkamp
parent 86e8a920bf
commit 950b48de61
4 changed files with 77 additions and 29 deletions

View File

@ -58,6 +58,7 @@ extern unsigned int spdk_g_notice_stderr_flag;
spdk_errlog(__FILE__, __LINE__, __func__, __VA_ARGS__)
int spdk_set_log_facility(const char *facility);
const char *spdk_get_log_facility(void);
int spdk_set_log_priority(const char *priority);
void spdk_noticelog(const char *file, const int line, const char *func,
const char *format, ...) __attribute__((__format__(__printf__, 4, 5)));

View File

@ -123,10 +123,9 @@ spdk_app_config_dump_global_section(FILE *fp)
if (NULL == fp)
return;
/* FIXME - lookup log facility and put it in place of "local7" below */
fprintf(fp, GLOBAL_CONFIG_TMPL,
spdk_app_get_core_mask(), spdk_trace_get_tpoint_group_mask(),
"local7");
spdk_get_log_facility());
}
int
@ -176,7 +175,7 @@ spdk_app_get_running_config(char **config_str, char *name)
}
static const char *
spdk_get_log_facility(struct spdk_conf *config)
spdk_app_get_log_facility(struct spdk_conf *config)
{
struct spdk_conf_section *sp;
const char *logfacility;
@ -288,7 +287,7 @@ spdk_app_init(struct spdk_app_opts *opts)
/* open log files */
if (opts->log_facility == NULL) {
opts->log_facility = spdk_get_log_facility(g_spdk_app.config);
opts->log_facility = spdk_app_get_log_facility(g_spdk_app.config);
if (opts->log_facility == NULL) {
fprintf(stderr, "NULL logfacility\n");
spdk_conf_free(g_spdk_app.config);

View File

@ -45,41 +45,78 @@
static TAILQ_HEAD(, spdk_trace_flag) g_trace_flags = TAILQ_HEAD_INITIALIZER(g_trace_flags);
unsigned int spdk_g_notice_stderr_flag = 1;
unsigned int spdk_g_log_facility = LOG_DAEMON;
int spdk_g_log_facility = LOG_DAEMON;
unsigned int spdk_g_log_priority = LOG_NOTICE;
SPDK_LOG_REGISTER_TRACE_FLAG("debug", SPDK_TRACE_DEBUG)
#define MAX_TMPBUF 1024
struct syslog_code {
const char *c_name;
int c_val;
};
static const struct syslog_code facilitynames[] = {
{ "auth", LOG_AUTH, },
{ "authpriv", LOG_AUTHPRIV, },
{ "cron", LOG_CRON, },
{ "daemon", LOG_DAEMON, },
{ "ftp", LOG_FTP, },
{ "kern", LOG_KERN, },
{ "lpr", LOG_LPR, },
{ "mail", LOG_MAIL, },
{ "news", LOG_NEWS, },
{ "syslog", LOG_SYSLOG, },
{ "user", LOG_USER, },
{ "uucp", LOG_UUCP, },
{ "local0", LOG_LOCAL0, },
{ "local1", LOG_LOCAL1, },
{ "local2", LOG_LOCAL2, },
{ "local3", LOG_LOCAL3, },
{ "local4", LOG_LOCAL4, },
{ "local5", LOG_LOCAL5, },
{ "local6", LOG_LOCAL6, },
{ "local7", LOG_LOCAL7, },
#ifdef __FreeBSD__
{ "console", LOG_CONSOLE, },
{ "ntp", LOG_NTP, },
{ "security", LOG_SECURITY, },
#endif
{ NULL, -1, }
};
int
spdk_set_log_facility(const char *facility)
{
if (strcasecmp(facility, "daemon") == 0) {
spdk_g_log_facility = LOG_DAEMON;
} else if (strcasecmp(facility, "auth") == 0) {
spdk_g_log_facility = LOG_AUTH;
} else if (strcasecmp(facility, "authpriv") == 0) {
spdk_g_log_facility = LOG_AUTHPRIV;
} else if (strcasecmp(facility, "local1") == 0) {
spdk_g_log_facility = LOG_LOCAL1;
} else if (strcasecmp(facility, "local2") == 0) {
spdk_g_log_facility = LOG_LOCAL2;
} else if (strcasecmp(facility, "local3") == 0) {
spdk_g_log_facility = LOG_LOCAL3;
} else if (strcasecmp(facility, "local4") == 0) {
spdk_g_log_facility = LOG_LOCAL4;
} else if (strcasecmp(facility, "local5") == 0) {
spdk_g_log_facility = LOG_LOCAL5;
} else if (strcasecmp(facility, "local6") == 0) {
spdk_g_log_facility = LOG_LOCAL6;
} else if (strcasecmp(facility, "local7") == 0) {
spdk_g_log_facility = LOG_LOCAL7;
} else {
spdk_g_log_facility = LOG_DAEMON;
return -1;
int i;
for (i = 0; facilitynames[i].c_name != NULL; i++) {
if (strcasecmp(facilitynames[i].c_name, facility) == 0) {
spdk_g_log_facility = facilitynames[i].c_val;
return 0;
}
}
return 0;
spdk_g_log_facility = LOG_DAEMON;
return -1;
}
const char *
spdk_get_log_facility(void)
{
const char *def_name = NULL;
int i;
for (i = 0; facilitynames[i].c_name != NULL; i++) {
if (facilitynames[i].c_val == spdk_g_log_facility) {
return facilitynames[i].c_name;
} else if (facilitynames[i].c_val == LOG_DAEMON) {
def_name = facilitynames[i].c_name;
}
}
return def_name;
}
int

View File

@ -45,11 +45,22 @@ static void
log_test(void)
{
int rc = 0;
const char *buf;
rc = spdk_set_log_facility("test");
CU_ASSERT(rc == -1);
CU_ASSERT_EQUAL(spdk_g_log_facility, LOG_DAEMON);
rc = spdk_set_log_facility("local7");
CU_ASSERT(rc == 0);
CU_ASSERT_EQUAL(spdk_g_log_facility, LOG_LOCAL7);
spdk_g_log_facility = -1;
buf = spdk_get_log_facility();
CU_ASSERT_STRING_EQUAL(buf, "daemon");
spdk_g_log_facility = LOG_LOCAL7;
buf = spdk_get_log_facility();
CU_ASSERT_STRING_EQUAL(buf, "local7");
rc = spdk_set_log_priority("test");
CU_ASSERT(rc == -1);
rc = spdk_set_log_priority("debug");