diff --git a/examples/basic.c b/examples/basic.c index aafc119..0f144e2 100644 --- a/examples/basic.c +++ b/examples/basic.c @@ -58,6 +58,7 @@ static void usage(int exit_code) " --trust-tls Trust TLS certificate.\n" " --legacy-ssl Use old style SSL.\n" " --legacy-auth Allow legacy authentication.\n" + " --verbose Increase the verbosity level.\n" " --tcp-keepalive Configure TCP keepalive.\n\n" "Note: --disable-tls conflicts with --mandatory-tls or " "--legacy-ssl\n"); @@ -72,7 +73,7 @@ int main(int argc, char **argv) xmpp_log_t *log; char *jid = NULL, *password = NULL, *cert = NULL, *key = NULL, *host = NULL; long flags = 0; - int tcp_keepalive = 0; + int tcp_keepalive = 0, verbosity = 0; int i; unsigned long port = 0; @@ -90,6 +91,8 @@ int main(int argc, char **argv) flags |= XMPP_CONN_FLAG_LEGACY_SSL; else if (strcmp(argv[i], "--legacy-auth") == 0) flags |= XMPP_CONN_FLAG_LEGACY_AUTH; + else if (strcmp(argv[i], "--verbose") == 0) + verbosity++; else if (strcmp(argv[i], "--tcp-keepalive") == 0) tcp_keepalive = 1; else if ((strcmp(argv[i], "--jid") == 0) && (++i < argc)) @@ -124,6 +127,7 @@ int main(int argc, char **argv) log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* create a context */ ctx = xmpp_ctx_new(NULL, log); + xmpp_ctx_set_verbosity(ctx, verbosity); /* create a connection */ conn = xmpp_conn_new(ctx); diff --git a/src/common.h b/src/common.h index bf018d4..4f2a363 100644 --- a/src/common.h +++ b/src/common.h @@ -77,6 +77,7 @@ typedef struct _xmpp_connlist_t { struct _xmpp_ctx_t { const xmpp_mem_t *mem; const xmpp_log_t *log; + int verbosity; xmpp_rand_t *rand; xmpp_loop_status_t loop_status; @@ -103,6 +104,8 @@ void xmpp_error(const xmpp_ctx_t *ctx, const char *area, const char *fmt, ...); void xmpp_warn(const xmpp_ctx_t *ctx, const char *area, const char *fmt, ...); void xmpp_info(const xmpp_ctx_t *ctx, const char *area, const char *fmt, ...); void xmpp_debug(const xmpp_ctx_t *ctx, const char *area, const char *fmt, ...); +void xmpp_debug_verbose( + int level, const xmpp_ctx_t *ctx, const char *area, const char *fmt, ...); /** connection **/ diff --git a/src/conn.c b/src/conn.c index 9f32864..5221f3a 100644 --- a/src/conn.c +++ b/src/conn.c @@ -1478,5 +1478,6 @@ static int _send_raw(xmpp_conn_t *conn, char *data, size_t len) } conn->send_queue_len++; xmpp_debug(conn->ctx, "conn", "SENT: %s", data); + xmpp_debug_verbose(1, conn->ctx, "conn", "Added queue element: %p", item); return XMPP_EOK; } diff --git a/src/ctx.c b/src/ctx.c index 6e295ef..15bbd09 100644 --- a/src/ctx.c +++ b/src/ctx.c @@ -377,6 +377,30 @@ void xmpp_debug(const xmpp_ctx_t *ctx, const char *area, const char *fmt, ...) va_end(ap); } +/** Write to the log at the DEBUG level if verbosity is enabled. + * This is a convenience function for writing to the log at the DEBUG level. + * It takes a printf-style format string followed by a variable list of + * arguments for formatting. + * + * @param level the verbosity level + * @param ctx a Strophe context object + * @param area the area to log for + * @param fmt a printf-style format string followed by a variable list of + * arguments to format + */ +void xmpp_debug_verbose( + int level, const xmpp_ctx_t *ctx, const char *area, const char *fmt, ...) +{ + va_list ap; + + if (ctx->verbosity < level) + return; + + va_start(ap, fmt); + xmpp_log(ctx, XMPP_LEVEL_DEBUG, area, fmt, ap); + va_end(ap); +} + /** Create and initialize a Strophe context object. * If mem is NULL, a default allocation setup will be used which * wraps malloc(), free(), and realloc() from the standard library. @@ -449,3 +473,15 @@ void xmpp_ctx_set_timeout(xmpp_ctx_t *ctx, unsigned long timeout) { ctx->timeout = timeout; } + +/** Set the verbosity level of a Strophe context. + * + * @param ctx a Strophe context object + * @param level the verbosity level + * + * @ingroup Context + */ +void xmpp_ctx_set_verbosity(xmpp_ctx_t *ctx, int level) +{ + ctx->verbosity = level; +} diff --git a/src/event.c b/src/event.c index 1edddac..c3b7d21 100644 --- a/src/event.c +++ b/src/event.c @@ -134,6 +134,8 @@ void xmpp_run_once(xmpp_ctx_t *ctx, unsigned long timeout) break; /* partial write or an error */ /* all data for this queue item written, delete and move on */ + xmpp_debug_verbose(1, ctx, "xmpp", + "Finished writing queue element: %p.", sq); xmpp_free(ctx, sq->data); tsq = sq; sq = sq->next; diff --git a/strophe.h b/strophe.h index e16ee08..ce90793 100644 --- a/strophe.h +++ b/strophe.h @@ -125,6 +125,9 @@ typedef struct _xmpp_ctx_t xmpp_ctx_t; xmpp_ctx_t *xmpp_ctx_new(const xmpp_mem_t *mem, const xmpp_log_t *log); void xmpp_ctx_free(xmpp_ctx_t *ctx); +/* set the verbosity level of the ctx */ +void xmpp_ctx_set_verbosity(xmpp_ctx_t *ctx, int level); + /* free some blocks returned by other APIs, for example the buffer you get from xmpp_stanza_to_text */ void xmpp_free(const xmpp_ctx_t *ctx, void *p);