From a1de389e4523b964230624f7af2cb3d910658c6b Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Tue, 6 Jul 2021 11:18:51 +0200 Subject: [PATCH] return early when nothing would be logged This returns early in case 1. no log handler is set 2. the default log handler is set and the log level would result in nothing being printed. No need to snprintf() and whatnot if there's no consumer. Signed-off-by: Steffen Jaeckel --- src/ctx.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ctx.c b/src/ctx.c index cc4134b..6e295ef 100644 --- a/src/ctx.c +++ b/src/ctx.c @@ -265,6 +265,13 @@ void xmpp_log(const xmpp_ctx_t *ctx, char *buf; va_list copy; + if (!ctx->log->handler) + return; + + if (ctx->log->handler == xmpp_default_logger && + level < *(xmpp_log_level_t *)ctx->log->userdata) + return; + va_copy(copy, ap); ret = xmpp_vsnprintf(smbuf, sizeof(smbuf), fmt, ap); if (ret >= (int)sizeof(smbuf)) { @@ -288,8 +295,7 @@ void xmpp_log(const xmpp_ctx_t *ctx, } va_end(copy); - if (ctx->log->handler) - ctx->log->handler(ctx->log->userdata, level, area, buf); + ctx->log->handler(ctx->log->userdata, level, area, buf); if (buf != smbuf) xmpp_free(ctx, buf);