From b5337c894f66661fb6a863467bcfac3fa12eaa7c Mon Sep 17 00:00:00 2001 From: Prasanna Kumar Kalever Date: Sun, 4 Jun 2017 12:27:27 +0530 Subject: logger: filter log messages based on severity level This patch adds '--log-level' options supported log levels: { "NONE", "ERROR", "WARNING", "INFO", "DEBUG", "TRACE" } TRACE being max logging $ gluster-blockd --help [...] --log-level Logging severity. Valid options are, TRACE, DEBUG, INFO, WARNING, ERROR and NONE [default: INFO] [...] Change-Id: I5a387eea3e7e7be10a0b56102a61eb81b4ebf2fa Signed-off-by: Prasanna Kumar Kalever --- daemon/gluster-blockd.c | 88 ++++++++++++++++++++++++++++++++++++------------- utils/utils.c | 22 +++++++++++++ utils/utils.h | 40 +++++++++++++--------- 3 files changed, 111 insertions(+), 39 deletions(-) diff --git a/daemon/gluster-blockd.c b/daemon/gluster-blockd.c index 5d546bd..de9953b 100644 --- a/daemon/gluster-blockd.c +++ b/daemon/gluster-blockd.c @@ -33,11 +33,14 @@ glusterBlockDHelp(void) MSG("%s", "gluster-blockd ("PACKAGE_VERSION")\n" "usage:\n" - " gluster-blockd [--glfs-lru-count ]\n" + " gluster-blockd [--glfs-lru-count ] [--log-level ]\n" "\n" "commands:\n" - " --glfs-lru-count \n" - " glfs objects cache capacity [max: 512] (default: 5)\n" + " --glfs-lru-count \n" + " glfs objects cache capacity [max: 512] [default: 5]\n" + " --log-level \n" + " Logging severity. Valid options are,\n" + " TRACE, DEBUG, INFO, WARNING, ERROR and NONE [default: INFO]\n" " --help\n" " show this message and exit.\n" " --version\n" @@ -201,54 +204,93 @@ glusterBlockServerThreadProc(void *vargp) } -int -main (int argc, char **argv) +static int +glusterBlockDParseArgs(int count, char **options) { - int fd; - pthread_t cli_thread; - pthread_t server_thread; - struct flock lock = {0, }; - int errnosv = 0; + size_t optind = 1; size_t opt = 0; + int ret = 0; - if (argc > 1) { - opt = glusterBlockDaemonOptEnumParse(argv[1]); + while (optind < count) { + opt = glusterBlockDaemonOptEnumParse(options[optind++]); if (!opt || opt >= GB_DAEMON_OPT_MAX) { - MSG("unknown option: %s\n", argv[1]); + MSG("unknown option: %s\n", options[optind-1]); return -1; } switch (opt) { case GB_DAEMON_HELP: case GB_DAEMON_USAGE: - if (argc != 2) { - MSG("undesired options for: %s\n", argv[1]); + if (count != 2) { + MSG("undesired options for: '%s'\n", options[optind-1]); + ret = -1; } glusterBlockDHelp(); - return 0; + exit(ret); case GB_DAEMON_VERSION: + if (count != 2) { + MSG("undesired options for: '%s'\n", options[optind-1]); + ret = -1; + } MSG("%s\n", argp_program_version); - return 0; + exit(ret); case GB_DAEMON_GLFS_LRU_COUNT: - if (argc != 3) { - MSG("undesired options for: %s\n", argv[1]); + if (count - optind < 1) { + MSG("option '%s' needs argument \n", options[optind-1]); return -1; } - if (sscanf(argv[2], "%zu", &glfsLruCount) != 1) { - MSG("option '%s' expect argument type integer \n", argv[1]); + if (sscanf(options[optind], "%zu", &glfsLruCount) != 1) { + MSG("option '%s' expect argument type integer \n", + options[optind-1]); return -1; } if (!glfsLruCount || (glfsLruCount > LRU_COUNT_MAX)) { - MSG("glfs-lru-count argument should be [0 < count < %d]\n", LRU_COUNT_MAX); + MSG("glfs-lru-count argument should be [0 < COUNT < %d]\n", + LRU_COUNT_MAX); LOG("mgmt", GB_LOG_ERROR, - "glfs-lru-count argument should be [0 < count < %d]\n", LRU_COUNT_MAX); + "glfs-lru-count argument should be [0 < COUNT < %d]\n", + LRU_COUNT_MAX); + return -1; + } + break; + + case GB_DAEMON_LOG_LEVEL: + if (count - optind < 1) { + MSG("option '%s' needs argument \n", options[optind-1]); + return -1; + } + logLevel = blockLogLevelEnumParse(options[optind]); + if (logLevel >= GB_LOG_MAX) { + MSG("unknown LOG-LEVEL: '%s'\n", options[optind]); return -1; } break; } + + optind++; + } + + return 0; +} + + +int +main (int argc, char **argv) +{ + int fd; + pthread_t cli_thread; + pthread_t server_thread; + struct flock lock = {0, }; + int errnosv = 0; + size_t opt = 0; + + + if (glusterBlockDParseArgs(argc, argv)) { + LOG("mgmt", GB_LOG_ERROR, "%s", "glusterBlockDParseArgs() failed"); + return -1; } if (!glusterBlockLogdirCreate()) { diff --git a/utils/utils.c b/utils/utils.c index 49a5d3d..ec85882 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -13,6 +13,8 @@ # include "config.h" +size_t logLevel = GB_LOG_INFO; + const char *argp_program_version = "" \ PACKAGE_NAME" ("PACKAGE_VERSION")" \ "\nRepository rev: https://github.com/gluster/gluster-block.git\n" \ @@ -68,6 +70,26 @@ glusterBlockDaemonOptEnumParse(const char *opt) } +int +blockLogLevelEnumParse(const char *opt) +{ + int i; + + + if (!opt) { + return GB_LOG_MAX; + } + + for (i = 0; i < GB_LOG_MAX; i++) { + if (!strcmp(opt, LogLevelLookup[i])) { + return i; + } + } + + return i; +} + + int blockMetaKeyEnumParse(const char *opt) { diff --git a/utils/utils.h b/utils/utils.h index 4a544ef..ef5aadc 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -77,22 +77,26 @@ fprintf(stdout, fmt, __VA_ARGS__); \ } while (0) -# define LOG(str, level, fmt, ...) \ - do { \ - FILE *fd; \ - if (!strcmp(str, "mgmt")) \ - fd = fopen (DAEMON_LOG_FILE, "a"); \ - else if (strcmp(str, "cli")) \ - fd = fopen (CLI_LOG_FILE, "a"); \ - else if (strcmp(str, "gfapi")) \ - fd = fopen (GFAPI_LOG_FILE, "a"); \ - else \ - fd = stderr; \ - fprintf(fd, "[%lu] %s: " fmt " [at %s+%d :<%s>]\n", \ - (unsigned long)time(NULL), LogLevelLookup[level],\ - __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__); \ - if (fd != stderr) \ - fclose(fd); \ +extern size_t logLevel; + +# define LOG(str, level, fmt, ...) \ + do { \ + FILE *fd; \ + if (level <= logLevel) { \ + if (!strcmp(str, "mgmt")) \ + fd = fopen (DAEMON_LOG_FILE, "a"); \ + else if (strcmp(str, "cli")) \ + fd = fopen (CLI_LOG_FILE, "a"); \ + else if (strcmp(str, "gfapi")) \ + fd = fopen (GFAPI_LOG_FILE, "a"); \ + else \ + fd = stderr; \ + fprintf(fd, "[%lu] %s: " fmt " [at %s+%d :<%s>]\n", \ + (unsigned long)time(NULL), LogLevelLookup[level],\ + __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__); \ + if (fd != stderr) \ + fclose(fd); \ + } \ } while (0) # define GB_METALOCK_OR_GOTO(lkfd, volume, errCode, errMsg, label) \ @@ -253,6 +257,7 @@ typedef enum gbDaemonCmdlineOption { GB_DAEMON_VERSION = 2, GB_DAEMON_USAGE = 3, GB_DAEMON_GLFS_LRU_COUNT = 4, + GB_DAEMON_LOG_LEVEL = 5, GB_DAEMON_OPT_MAX } gbDaemonCmdlineOption; @@ -263,6 +268,7 @@ static const char *const gbDaemonCmdlineOptLookup[] = { [GB_DAEMON_VERSION] = "version", [GB_DAEMON_USAGE] = "usage", [GB_DAEMON_GLFS_LRU_COUNT] = "glfs-lru-count", + [GB_DAEMON_LOG_LEVEL] = "log-level", [GB_DAEMON_OPT_MAX] = NULL, }; @@ -378,6 +384,8 @@ int glusterBlockCLIOptEnumParse(const char *opt); int glusterBlockDaemonOptEnumParse(const char *opt); +int blockLogLevelEnumParse(const char *opt); + int blockMetaKeyEnumParse(const char *opt); int blockMetaStatusEnumParse(const char *opt); -- cgit