Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
a1b1fd4f
Commit
a1b1fd4f
authored
Oct 02, 2016
by
antirez
Browse files
Modules: API to log from module I/O callbacks.
parent
4674efde
Changes
2
Show whitespace changes
Inline
Side-by-side
src/module.c
View file @
a1b1fd4f
...
...
@@ -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
);
...
...
src/redismodule.h
View file @
a1b1fd4f
...
...
@@ -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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment