From a1b1fd4f392c59c5fc194e66856e1cb8d6a4f932 Mon Sep 17 00:00:00 2001 From: antirez Date: Sun, 2 Oct 2016 16:51:37 +0200 Subject: [PATCH] Modules: API to log from module I/O callbacks. --- src/module.c | 54 +++++++++++++++++++++++++++++++++-------------- src/redismodule.h | 2 ++ 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/module.c b/src/module.c index b94c98acf..ab63f8b04 100644 --- a/src/module.c +++ b/src/module.c @@ -2908,7 +2908,30 @@ void RM_EmitAOF(RedisModuleIO *io, const char *cmdname, const char *fmt, ...) { * Logging * -------------------------------------------------------------------------- */ -/* Produces a log message to the standard Redis log, the format accepts +/* This is the low level function implementing both: + * + * RM_Log() + * RM_LogIOError() + * + */ +void RM_LogRaw(RedisModule *module, const char *levelstr, const char *fmt, va_list ap) { + char msg[LOG_MAX_LEN]; + size_t name_len; + int level; + + 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> ", module->name); + vsnprintf(msg + name_len, sizeof(msg) - name_len, fmt, ap); + serverLogRaw(level,msg); +} + +/* + * 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: * @@ -2923,26 +2946,24 @@ void RM_EmitAOF(RedisModuleIO *io, const char *cmdname, const char *fmt, ...) { * 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 (!ctx->module) return; /* Can only log if module is initialized */ - 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_list ap; va_start(ap, fmt); - vsnprintf(msg + name_len, sizeof(msg) - name_len, fmt, ap); + RM_LogRaw(ctx->module,levelstr,fmt,ap); va_end(ap); +} - serverLogRaw(level,msg); +/* Log errors from RDB / AOF serialization callbacks. + * + * This function should be used when a callback is returning a critical + * error to the caller since cannot load or save the data for some + * critical reason. */ +void RM_LogIOError(RedisModuleIO *io, const char *levelstr, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + RM_LogRaw(io->type->module,levelstr,fmt,ap); + va_end(ap); } /* -------------------------------------------------------------------------- @@ -3261,6 +3282,7 @@ void moduleRegisterCoreAPI(void) { REGISTER_API(LoadDouble); REGISTER_API(EmitAOF); REGISTER_API(Log); + REGISTER_API(LogIOError); REGISTER_API(StringAppendBuffer); REGISTER_API(RetainString); REGISTER_API(StringCompare); diff --git a/src/redismodule.h b/src/redismodule.h index 75b13ad52..dcc0aa1ea 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -185,6 +185,7 @@ char *REDISMODULE_API_FUNC(RedisModule_LoadStringBuffer)(RedisModuleIO *io, size 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, const char *level, const char *fmt, ...); +void REDISMODULE_API_FUNC(RedisModule_LogIOError)(RedisModuleIO *io, const char *levelstr, const char *fmt, ...); int REDISMODULE_API_FUNC(RedisModule_StringAppendBuffer)(RedisModuleCtx *ctx, RedisModuleString *str, const char *buf, size_t len); void REDISMODULE_API_FUNC(RedisModule_RetainString)(RedisModuleCtx *ctx, RedisModuleString *str); int REDISMODULE_API_FUNC(RedisModule_StringCompare)(RedisModuleString *a, RedisModuleString *b); @@ -282,6 +283,7 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int REDISMODULE_GET_API(LoadDouble); REDISMODULE_GET_API(EmitAOF); REDISMODULE_GET_API(Log); + REDISMODULE_GET_API(LogIOError); REDISMODULE_GET_API(StringAppendBuffer); REDISMODULE_GET_API(RetainString); REDISMODULE_GET_API(StringCompare);