log: Reduce lots of duplicated code

Use the log level enum to eliminate the variants of the log
function.

Change-Id: I5bd0e7b4f84f78dab86a5102baa6baa0808627a8
Signed-off-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-on: https://review.gerrithub.io/365293
Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: Daniel Verkamp <daniel.verkamp@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Ben Walker 2017-06-13 11:35:27 -07:00 committed by Daniel Verkamp
parent c80e7df5c3
commit cca697b0ec
3 changed files with 41 additions and 96 deletions

View File

@ -79,31 +79,24 @@ enum spdk_log_level spdk_log_get_level(void);
extern unsigned int spdk_g_notice_stderr_flag;
#define SPDK_NOTICELOG(...) \
spdk_noticelog(NULL, 0, NULL, __VA_ARGS__)
spdk_log(SPDK_LOG_NOTICE, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define SPDK_WARNLOG(...) \
spdk_warnlog(NULL, 0, NULL, __VA_ARGS__)
spdk_log(SPDK_LOG_WARN, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define SPDK_ERRLOG(...) \
spdk_errlog(__FILE__, __LINE__, __func__, __VA_ARGS__)
spdk_log(SPDK_LOG_ERROR, __FILE__, __LINE__, __func__, __VA_ARGS__)
int spdk_set_log_facility(const char *facility);
const char *spdk_get_log_facility(void);
void spdk_noticelog(const char *file, const int line, const char *func,
const char *format, ...) __attribute__((__format__(__printf__, 4, 5)));
void spdk_warnlog(const char *file, const int line, const char *func,
const char *format, ...) __attribute__((__format__(__printf__, 4, 5)));
void spdk_tracelog(const char *flag, const char *file, const int line,
const char *func, const char *format, ...) __attribute__((__format__(__printf__, 5, 6)));
void spdk_errlog(const char *file, const int line, const char *func,
const char *format, ...) __attribute__((__format__(__printf__, 4, 5)));
void spdk_log(enum spdk_log_level level, const char *file, const int line, const char *func,
const char *format, ...) __attribute__((__format__(__printf__, 5, 6)));
void spdk_trace_dump(const char *label, const uint8_t *buf, size_t len);
bool spdk_log_get_trace_flag(const char *flag);
int spdk_log_set_trace_flag(const char *flag);
int spdk_log_clear_trace_flag(const char *flag);
void spdk_tracelog_usage(FILE *f, const char *trace_arg);
#endif /* SPDK_LOG_H */

View File

@ -68,7 +68,7 @@ __attribute__((constructor)) static void register_trace_flag_##flag(void) \
do { \
extern struct spdk_trace_flag FLAG; \
if (FLAG.enabled) { \
spdk_tracelog(FLAG.name, __FILE__, __LINE__, __func__, __VA_ARGS__); \
spdk_log(SPDK_LOG_INFO, __FILE__, __LINE__, __func__, __VA_ARGS__); \
} \
} while (0)

View File

@ -79,6 +79,14 @@ static const struct syslog_code facilitynames[] = {
{ NULL, -1, }
};
static const char *const spdk_level_names[] = {
[SPDK_LOG_ERROR] = "ERROR",
[SPDK_LOG_WARN] = "WARNING",
[SPDK_LOG_NOTICE] = "NOTICE",
[SPDK_LOG_INFO] = "INFO",
[SPDK_LOG_DEBUG] = "DEBUG",
};
void
spdk_log_open(void)
{
@ -140,99 +148,43 @@ spdk_get_log_facility(void)
}
void
spdk_noticelog(const char *file, const int line, const char *func,
const char *format, ...)
spdk_log(enum spdk_log_level level, const char *file, const int line, const char *func,
const char *format, ...)
{
int severity = LOG_INFO;
char buf[MAX_TMPBUF];
va_list ap;
switch (level) {
case SPDK_LOG_ERROR:
severity = LOG_ERR;
break;
case SPDK_LOG_WARN:
severity = LOG_WARNING;
break;
case SPDK_LOG_NOTICE:
severity = LOG_NOTICE;
break;
case SPDK_LOG_INFO:
case SPDK_LOG_DEBUG:
severity = LOG_INFO;
break;
}
va_start(ap, format);
vsnprintf(buf, sizeof buf, format, ap);
if (file != NULL) {
if (func != NULL) {
if (spdk_g_notice_stderr_flag) {
fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf);
}
syslog(LOG_NOTICE, "%s:%4d:%s: %s", file, line, func, buf);
} else {
if (spdk_g_notice_stderr_flag) {
fprintf(stderr, "%s:%4d: %s", file, line, buf);
}
syslog(LOG_NOTICE, "%s:%4d: %s", file, line, buf);
vsnprintf(buf, sizeof(buf), format, ap);
if (level <= g_spdk_log_level) {
fprintf(stderr, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf);
if (level <= SPDK_LOG_NOTICE) {
syslog(severity, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf);
}
} else {
if (spdk_g_notice_stderr_flag) {
fprintf(stderr, "%s", buf);
}
syslog(LOG_NOTICE, "%s", buf);
}
va_end(ap);
}
void
spdk_warnlog(const char *file, const int line, const char *func,
const char *format, ...)
{
char buf[MAX_TMPBUF];
va_list ap;
va_start(ap, format);
vsnprintf(buf, sizeof buf, format, ap);
if (file != NULL) {
if (func != NULL) {
fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf);
syslog(LOG_WARNING, "%s:%4d:%s: %s",
file, line, func, buf);
} else {
fprintf(stderr, "%s:%4d: %s", file, line, buf);
syslog(LOG_WARNING, "%s:%4d: %s", file, line, buf);
}
} else {
fprintf(stderr, "%s", buf);
syslog(LOG_WARNING, "%s", buf);
}
va_end(ap);
}
void
spdk_tracelog(const char *flag, const char *file, const int line, const char *func,
const char *format, ...)
{
char buf[MAX_TMPBUF];
va_list ap;
va_start(ap, format);
vsnprintf(buf, sizeof buf, format, ap);
if (func != NULL) {
fprintf(stderr, "[%s] %s:%4d:%s: %s", flag, file, line, func, buf);
//syslog(LOG_INFO, "[%s] %s:%4d:%s: %s", flag, file, line, func, buf);
} else {
fprintf(stderr, "[%s] %s:%4d: %s", flag, file, line, buf);
//syslog(LOG_INFO, "[%s] %s:%4d: %s", flag, file, line, buf);
}
va_end(ap);
}
void
spdk_errlog(const char *file, const int line, const char *func,
const char *format, ...)
{
char buf[MAX_TMPBUF];
va_list ap;
va_start(ap, format);
vsnprintf(buf, sizeof buf, format, ap);
if (func != NULL) {
fprintf(stderr, "%s:%4d:%s: ***ERROR*** %s", file, line, func, buf);
syslog(LOG_ERR, "%s:%4d:%s: ***ERROR*** %s", file, line, func, buf);
} else {
fprintf(stderr, "%s:%4d: ***ERROR*** %s", file, line, buf);
syslog(LOG_ERR, "%s:%4d: ***ERROR*** %s", file, line, buf);
}
va_end(ap);
}
static void
fdump(FILE *fp, const char *label, const uint8_t *buf, size_t len)
{