Add a new xmpp_get_default_logger(level) call to provide access to
(a filtered version of) the default logger which we implemented in the library but was no longer accessible. This makes it much easier for the caller to turn logging on by just passing this default log handler callback pointer into xmpp_ctx_new() instead of having to always implement boilerplate. Another option would have been xmpp_get_default_log_level(ctx, level) to change the filter level of the build in logger, which is installed if you pass NULL to xmpp_ctx_new(). In this method the logger would filter everything by default.
This commit is contained in:
26
src/ctx.c
26
src/ctx.c
@@ -63,14 +63,36 @@ static xmpp_mem_t xmpp_default_mem = {
|
||||
realloc
|
||||
};
|
||||
|
||||
static const char * const xmpp_log_level_name[4] = {"DEBUG", "INFO", "WARN", "ERROR"};
|
||||
static const char * const _xmpp_log_level_name[4] = {"DEBUG", "INFO", "WARN", "ERROR"};
|
||||
static const xmpp_log_level_t _xmpp_default_logger_levels[] = {XMPP_LEVEL_DEBUG,
|
||||
XMPP_LEVEL_INFO,
|
||||
XMPP_LEVEL_WARN,
|
||||
XMPP_LEVEL_ERROR};
|
||||
|
||||
void xmpp_default_logger(void * const userdata,
|
||||
const xmpp_log_level_t level,
|
||||
const char * const area,
|
||||
const char * const msg)
|
||||
{
|
||||
fprintf(stderr, "%s %s %s\n", area, xmpp_log_level_name[level], msg);
|
||||
xmpp_log_level_t filter_level = * (xmpp_log_level_t*)userdata;
|
||||
if (level >= filter_level)
|
||||
fprintf(stderr, "%s %s %s\n", area, _xmpp_log_level_name[level], msg);
|
||||
}
|
||||
|
||||
static const xmpp_log_t _xmpp_default_loggers[] = {
|
||||
{&xmpp_default_logger, (void*)&_xmpp_default_logger_levels[XMPP_LEVEL_DEBUG]},
|
||||
{&xmpp_default_logger, (void*)&_xmpp_default_logger_levels[XMPP_LEVEL_INFO]},
|
||||
{&xmpp_default_logger, (void*)&_xmpp_default_logger_levels[XMPP_LEVEL_WARN]},
|
||||
{&xmpp_default_logger, (void*)&_xmpp_default_logger_levels[XMPP_LEVEL_ERROR]}
|
||||
};
|
||||
|
||||
xmpp_log_t *xmpp_get_default_logger(xmpp_log_level_t level)
|
||||
{
|
||||
/* clamp to the known range */
|
||||
if (level > XMPP_LEVEL_ERROR) level = XMPP_LEVEL_ERROR;
|
||||
if (level < XMPP_LEVEL_DEBUG) level = XMPP_LEVEL_DEBUG;
|
||||
|
||||
return (xmpp_log_t*)&_xmpp_default_loggers[level];
|
||||
}
|
||||
|
||||
static xmpp_log_t xmpp_default_log = { NULL, NULL };
|
||||
|
||||
Reference in New Issue
Block a user