lib/log: allow omiting filename/lineno information

In spdk_log() accept filename = NULL.
If filename is NULL then source information as well
as log level is not displayed.

This change allows to replace all usages of
printf() and fprintf(stderr,) by
SPDK_PRINTF() and SPDK_ERRLOG() which use spdk_log().

Using spdk_log() instead of printf() is always prefered
since SPDK can be used inside of another application
where SPDK logs could be redirected.

SPDK uses printf()
places where location info is not needed
we cannot replace it by SPDK_NOTICELOG().

This change is in the scope earlier planned task:
https://trello.com/c/lZzBjrw3/10-remove-use-of-printf-fprintf-and-perror-for-logging-in-library-code

Change-Id: I55c24da4a2092bd118fa2c121092d253cedb1cf8
Signed-off-by: Vitaliy Mysak <vitaliy.mysak@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/1942
Community-CI: Mellanox Build Bot
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Vitaliy Mysak 2020-04-17 17:33:52 +02:00 committed by Tomasz Zawadzki
parent 18a2fc606a
commit caa9d4c87a
2 changed files with 13 additions and 3 deletions

View File

@ -138,6 +138,8 @@ enum spdk_log_level spdk_log_get_print_level(void);
spdk_log(SPDK_LOG_WARN, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define SPDK_ERRLOG(...) \
spdk_log(SPDK_LOG_ERROR, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define SPDK_PRINTF(...) \
spdk_log(SPDK_LOG_NOTICE, NULL, -1, NULL, __VA_ARGS__)
/**
* Write messages to the log file. If \c level is set to \c SPDK_LOG_DISABLED,

View File

@ -150,12 +150,20 @@ spdk_log(enum spdk_log_level level, const char *file, const int line, const char
vsnprintf(buf, sizeof(buf), format, ap);
if (level <= g_spdk_log_print_level) {
if (file) {
fprintf(stderr, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf);
spdk_log_unwind_stack(stderr, level);
} else {
fprintf(stderr, "%s", buf);
}
}
if (level <= g_spdk_log_level) {
if (file) {
syslog(severity, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf);
} else {
syslog(severity, "%s", buf);
}
}
va_end(ap);