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:
@@ -16,16 +16,8 @@
|
||||
|
||||
#include <strophe.h>
|
||||
|
||||
void log_handler(void * const userdata,
|
||||
const xmpp_log_level_t level,
|
||||
const char * const area,
|
||||
const char * const msg)
|
||||
{
|
||||
static const char * const log_level_name[4] = {"DEBUG", "INFO", "WARN", "ERROR"};
|
||||
|
||||
fprintf(stderr, "%s %s %s\n", area, log_level_name[level], msg);
|
||||
}
|
||||
|
||||
/* define a handler for connection events */
|
||||
void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
|
||||
const int error, xmpp_stream_error_t * const stream_error,
|
||||
void * const userdata)
|
||||
@@ -46,10 +38,12 @@ int main(int argc, char **argv)
|
||||
{
|
||||
xmpp_ctx_t *ctx;
|
||||
xmpp_conn_t *conn;
|
||||
xmpp_log_t log;
|
||||
xmpp_log_t *log;
|
||||
char *jid, *pass;
|
||||
char *server;
|
||||
|
||||
/* take a jid and password on the command line,
|
||||
with optional server to connect to */
|
||||
if ((argc < 3) || (argc > 4)) {
|
||||
fprintf(stderr, "Usage: basic <jid> <pass> <server>\n\n");
|
||||
return 1;
|
||||
@@ -57,16 +51,18 @@ int main(int argc, char **argv)
|
||||
|
||||
jid = argv[1];
|
||||
pass = argv[2];
|
||||
server = NULL;
|
||||
/* Normally we pass NULL for the connection domain, in which case
|
||||
the library derives the target host from the jid, but we can
|
||||
override this for testing. */
|
||||
if (argc >= 4) server = argv[3];
|
||||
else server = NULL;
|
||||
|
||||
/* init library */
|
||||
xmpp_initialize();
|
||||
|
||||
/* create a context */
|
||||
log.handler = log_handler;
|
||||
log.userdata = NULL;
|
||||
ctx = xmpp_ctx_new(NULL, &log);
|
||||
log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
|
||||
ctx = xmpp_ctx_new(NULL, log);
|
||||
|
||||
/* create a connection */
|
||||
conn = xmpp_conn_new(ctx);
|
||||
|
||||
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