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.
This commit is contained in:
antirez 2016-06-23 12:11:30 +02:00
parent 715794b829
commit 4b12c6a360
3 changed files with 39 additions and 19 deletions

View File

@ -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
* -------------------------------------------------------------------------- */

View File

@ -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.

View File

@ -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));