From 4b12c6a3600eb2f8bfb8e440db5a526054bd2d9b Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 23 Jun 2016 12:11:30 +0200 Subject: [PATCH] Modules: changes to logging function. This commit changes what provided by PR #3315 (merged) in order to let the user specify the log level as a string. The define could be also used, but when this happens, they must be decoupled from the defines in the Redis core, like in the other part of the Redis modules implementations, so that a switch statement (or a function) remaps between the two, otherwise we are no longer free to change the internal Redis defines. --- src/module.c | 29 +++++++++++++++++++++++------ src/modules/API.md | 20 +++++++++++++++----- src/redismodule.h | 9 +-------- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/module.c b/src/module.c index a71d442c..dff7feb3 100644 --- a/src/module.c +++ b/src/module.c @@ -2772,17 +2772,35 @@ void RM_EmitAOF(RedisModuleIO *io, const char *cmdname, const char *fmt, ...) { * Logging * -------------------------------------------------------------------------- */ -/* Produces a log message to the standard Redis log. */ -void RM_Log(RedisModuleCtx *ctx, int level, const char *fmt, ...) -{ +/* Produces a log message to the standard Redis log, the format accepts + * printf-alike specifiers, while level is a string describing the log + * level to use when emitting the log, and must be one of the following: + * + * * "debug" + * * "verbose" + * * "notice" + * * "warning" + * + * If the specified log level is invalid, verbose is used by default. + * There is a fixed limit to the length of the log line this function is able + * to emit, this limti is not specified but is guaranteed to be more than + * a few lines of text. + */ +void RM_Log(RedisModuleCtx *ctx, const char *levelstr, const char *fmt, ...) { va_list ap; char msg[LOG_MAX_LEN]; size_t name_len; + int level; - if ((level&0xff) < server.verbosity) return; if (!ctx->module) return; /* Can only log if module is initialized */ - name_len = snprintf(msg, sizeof(msg),"%s: ", ctx->module->name); + if (!strcasecmp(levelstr,"debug")) level = LL_DEBUG; + else if (!strcasecmp(levelstr,"verbose")) level = LL_VERBOSE; + else if (!strcasecmp(levelstr,"notice")) level = LL_NOTICE; + else if (!strcasecmp(levelstr,"warning")) level = LL_WARNING; + else level = LL_VERBOSE; /* Default. */ + + name_len = snprintf(msg, sizeof(msg),"<%s> ", ctx->module->name); va_start(ap, fmt); vsnprintf(msg + name_len, sizeof(msg) - name_len, fmt, ap); @@ -2791,7 +2809,6 @@ void RM_Log(RedisModuleCtx *ctx, int level, const char *fmt, ...) serverLogRaw(level,msg); } - /* -------------------------------------------------------------------------- * Modules API internals * -------------------------------------------------------------------------- */ diff --git a/src/modules/API.md b/src/modules/API.md index 24768c5b..021b2aa1 100644 --- a/src/modules/API.md +++ b/src/modules/API.md @@ -1117,9 +1117,19 @@ handling is performed by Redis itself. ## `RM_Log` - void RM_Log(RedisModuleCtx *ctx, int level, const char *fmt, ...); + void RM_Log(RedisModuleCtx *ctx, const char *levelstr, const char *fmt, ...); + +Produces a log message to the standard Redis log, the format accepts +printf-alike specifiers, while level is a string describing the log +level to use when emitting the log, and must be one of the following: + +* "debug" +* "verbose" +* "notice" +* "warning" + +If the specified log level is invalid, verbose is used by default. +There is a fixed limit to the length of the log line this function is able +to emit, this limti is not specified but is guaranteed to be more than +a few lines of text. -Produce a log message into the standard Redis log. All standard Redis logging -configuration applies here. Messages can only be logged after a module has -initialized, and are prefixed by the name of the module. Log level is -specified using the REDISMODULE_LOG_* macros. diff --git a/src/redismodule.h b/src/redismodule.h index aa43a736..f376c36c 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -68,13 +68,6 @@ #define REDISMODULE_POSITIVE_INFINITE (1.0/0.0) #define REDISMODULE_NEGATIVE_INFINITE (-1.0/0.0) -/* Logging levels */ -#define REDISMODULE_LOG_DEBUG 0 -#define REDISMODULE_LOG_VERBOSE 1 -#define REDISMODULE_LOG_NOTICE 2 -#define REDISMODULE_LOG_WARNING 3 - - /* ------------------------- End of common defines ------------------------ */ #ifndef REDISMODULE_CORE @@ -187,7 +180,7 @@ RedisModuleString *REDISMODULE_API_FUNC(RedisModule_LoadString)(RedisModuleIO *i char *REDISMODULE_API_FUNC(RedisModule_LoadStringBuffer)(RedisModuleIO *io, size_t *lenptr); void REDISMODULE_API_FUNC(RedisModule_SaveDouble)(RedisModuleIO *io, double value); double REDISMODULE_API_FUNC(RedisModule_LoadDouble)(RedisModuleIO *io); -void REDISMODULE_API_FUNC(RedisModule_Log)(RedisModuleCtx *ctx, int level, const char *fmt, ...); +void REDISMODULE_API_FUNC(RedisModule_Log)(RedisModuleCtx *ctx, const char *level, const char *fmt, ...); /* This is included inline inside each Redis module. */ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver) __attribute__((unused));