From 28d87d5d833f2de320169aa766feaccc0bd98262 Mon Sep 17 00:00:00 2001 From: Barak Sason Date: Thu, 24 Oct 2019 16:28:31 +0300 Subject: libglusterfs/logger - code cleaning Fixes: bz#1765186 1 - Removed code duplication of 'log_levles' array 2 - Declared all static methods at the begining of the .c file 3 - Removed unused structs that existed in the .c file Change-Id: I37330cb133fc65deac69b7ca60779f70501c24ea Signed-off-by: Barak Sason --- libglusterfs/src/logging.c | 91 ++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 56 deletions(-) (limited to 'libglusterfs') diff --git a/libglusterfs/src/logging.c b/libglusterfs/src/logging.c index 431ae979cba..e900df08a4b 100644 --- a/libglusterfs/src/logging.c +++ b/libglusterfs/src/logging.c @@ -48,7 +48,7 @@ #define TEST_LOG(__msg, __args...) \ gf_log("logging-infra", GF_LOG_DEBUG, __msg, ##__args); -void +static void gf_log_flush_timeout_cbk(void *data); int @@ -57,26 +57,24 @@ gf_log_inject_timer_event(glusterfs_ctx_t *ctx); static void gf_log_flush_extra_msgs(glusterfs_ctx_t *ctx, uint32_t new); -static char *gf_level_strings[] = {"", /* NONE */ - "M", /* EMERGENCY */ - "A", /* ALERT */ - "C", /* CRITICAL */ - "E", /* ERROR */ - "W", /* WARNING */ - "N", /* NOTICE */ - "I", /* INFO */ - "D", /* DEBUG */ - "T", /* TRACE */ - ""}; - -/* Ideally this should get moved to logging.h */ -struct _msg_queue { - struct list_head msgs; -}; - -struct _log_msg { - const char *msg; - struct list_head queue; +static int +log_buf_init(log_buf_t *buf, const char *domain, const char *file, + const char *function, int32_t line, gf_loglevel_t level, + int errnum, uint64_t msgid, char **appmsgstr, int graph_id); +static void +gf_log_rotate(glusterfs_ctx_t *ctx); + +static char gf_level_strings[] = { + ' ', /* NONE */ + 'M', /* EMERGENCY */ + 'A', /* ALERT */ + 'C', /* CRITICAL */ + 'E', /* ERROR */ + 'W', /* WARNING */ + 'N', /* NOTICE */ + 'I', /* INFO */ + 'D', /* DEBUG */ + 'T', /* TRACE */ }; void @@ -478,7 +476,7 @@ gf_openlog(const char *ident, int option, int facility) * buf = "I/O error\u001bon /tmp/bar file" * */ -char * +static char * _json_escape(const char *str, char *buf, size_t len) { static const unsigned char json_exceptions[UCHAR_MAX + 1] = { @@ -782,18 +780,6 @@ _gf_log_callingfn(const char *domain, const char *file, const char *function, if (skip_logging(this, level)) goto out; - static const char *level_strings[] = {"", /* NONE */ - "M", /* EMERGENCY */ - "A", /* ALERT */ - "C", /* CRITICAL */ - "E", /* ERROR */ - "W", /* WARNING */ - "N", /* NOTICE */ - "I", /* INFO */ - "D", /* DEBUG */ - "T", /* TRACE */ - ""}; - if (!domain || !file || !function || !fmt) { fprintf(stderr, "logging: %s:%s():%d: invalid argument\n", __FILE__, __PRETTY_FUNCTION__, __LINE__); @@ -840,8 +826,8 @@ _gf_log_callingfn(const char *domain, const char *file, const char *function, gf_time_fmt(timestr, sizeof timestr, tv.tv_sec, gf_timefmt_FT); ret = gf_asprintf( - &logline, "[%s.%" GF_PRI_SUSECONDS "] %s [%s:%d:%s] %s %d-%s: %s\n", - timestr, tv.tv_usec, level_strings[level], basename, line, function, + &logline, "[%s.%" GF_PRI_SUSECONDS "] %c [%s:%d:%s] %s %d-%s: %s\n", + timestr, tv.tv_usec, gf_level_strings[level], basename, line, function, callstr, ((this->graph) ? this->graph->id : 0), domain, msg); if (-1 == ret) { goto out; @@ -1139,7 +1125,7 @@ _gf_msg_nomem(const char *domain, const char *file, const char *function, * of 0. Need to enhance this to support format as configured */ wlen = snprintf( msg, sizeof msg, - "[%s.%" GF_PRI_SUSECONDS "] %s [MSGID: %" PRIu64 + "[%s.%" GF_PRI_SUSECONDS "] %c [MSGID: %" PRIu64 "]" " [%s:%d:%s] %s: no memory " "available for size (%" GF_PRI_SIZET @@ -1320,7 +1306,7 @@ gf_log_glusterlog(glusterfs_ctx_t *ctx, const char *domain, const char *file, if (!callstr) { ret = gf_asprintf(&header, "[%s.%" GF_PRI_SUSECONDS - "] %s [%s:%d:%s]" + "] %c [%s:%d:%s]" " %d-%s: %s", timestr, tv.tv_usec, gf_level_strings[level], file, line, function, graph_id, domain, @@ -1328,7 +1314,7 @@ gf_log_glusterlog(glusterfs_ctx_t *ctx, const char *domain, const char *file, } else { ret = gf_asprintf(&header, "[%s.%" GF_PRI_SUSECONDS - "] %s [%s:%d:%s] %s" + "] %c [%s:%d:%s] %s" " %d-%s: %s", timestr, tv.tv_usec, gf_level_strings[level], file, line, function, callstr, graph_id, domain, @@ -1339,7 +1325,7 @@ gf_log_glusterlog(glusterfs_ctx_t *ctx, const char *domain, const char *file, * print enhanced log format */ if (!callstr) { ret = gf_asprintf(&header, - "[%s.%" GF_PRI_SUSECONDS "] %s [MSGID: %" PRIu64 + "[%s.%" GF_PRI_SUSECONDS "] %c [MSGID: %" PRIu64 "]" " [%s:%d:%s] %d-%s: %s", timestr, tv.tv_usec, gf_level_strings[level], @@ -1347,7 +1333,7 @@ gf_log_glusterlog(glusterfs_ctx_t *ctx, const char *domain, const char *file, *appmsgstr); } else { ret = gf_asprintf(&header, - "[%s.%" GF_PRI_SUSECONDS "] %s [MSGID: %" PRIu64 + "[%s.%" GF_PRI_SUSECONDS "] %c [MSGID: %" PRIu64 "]" " [%s:%d:%s] %s %d-%s: %s", timestr, tv.tv_usec, gf_level_strings[level], @@ -1467,7 +1453,7 @@ gf_glusterlog_log_repetitions(glusterfs_ctx_t *ctx, const char *domain, gf_log_rotate(ctx); ret = gf_asprintf(&header, - "The message \"%s [MSGID: %" PRIu64 + "The message \"%c [MSGID: %" PRIu64 "]" " [%s:%d:%s] %d-%s: %s", gf_level_strings[level], msgid, file, line, function, @@ -1547,6 +1533,11 @@ gf_log_print_with_repetitions(glusterfs_ctx_t *ctx, const char *domain, appmsgstr, callstr, refcount, oldest, latest, graph_id); break; } + /* NOTE: If syslog control file is absent, which is another + * way to control logging to syslog, then we will fall through + * to the gluster log. The ideal way to do things would be to + * not have the extra control file check */ + case gf_logger_glusterlog: ret = gf_glusterlog_log_repetitions( ctx, domain, file, function, line, level, errnum, msgid, @@ -2044,18 +2035,6 @@ _gf_log(const char *domain, const char *file, const char *function, int line, if (skip_logging(this, level)) goto out; - static const char *level_strings[] = {"", /* NONE */ - "M", /* EMERGENCY */ - "A", /* ALERT */ - "C", /* CRITICAL */ - "E", /* ERROR */ - "W", /* WARNING */ - "N", /* NOTICE */ - "I", /* INFO */ - "D", /* DEBUG */ - "T", /* TRACE */ - ""}; - if (!domain || !file || !function || !fmt) { fprintf(stderr, "logging: %s:%s():%d: invalid argument\n", __FILE__, __PRETTY_FUNCTION__, __LINE__); @@ -2126,8 +2105,8 @@ log: gf_time_fmt(timestr, sizeof timestr, tv.tv_sec, gf_timefmt_FT); ret = gf_asprintf( - &logline, "[%s.%" GF_PRI_SUSECONDS "] %s [%s:%d:%s] %d-%s: %s\n", - timestr, tv.tv_usec, level_strings[level], basename, line, function, + &logline, "[%s.%" GF_PRI_SUSECONDS "] %c [%s:%d:%s] %d-%s: %s\n", + timestr, tv.tv_usec, gf_level_strings[level], basename, line, function, ((this->graph) ? this->graph->id : 0), domain, msg); if (-1 == ret) { goto err; -- cgit