From: Mathew Heard Date: Tue, 29 Jun 2021 00:24:00 +0000 (+1000) Subject: Bail out of logging early. X-Git-Url: https://tinc-vpn.org/git/browse?a=commitdiff_plain;h=a459e57de5fa4d32ee1162e49770fd8730e3707d;p=tinc Bail out of logging early. Low hanging fruit, 2% of perf trace. --- diff --git a/src/logger.c b/src/logger.c index 062f7598..9c02a3d8 100644 --- a/src/logger.c +++ b/src/logger.c @@ -37,22 +37,21 @@ static FILE *logfile = NULL; static HANDLE loghandle = NULL; #endif static const char *logident = NULL; -bool logcontrol = false; +bool logcontrol = false; // controlled by REQ_LOG int umbilical = 0; +static bool should_log(int level) { + return (level <= debug_level && logmode != LOGMODE_NULL) || logcontrol; +} + static void real_logger(int level, int priority, const char *message) { char timestr[32] = ""; static bool suppress = false; - // Bail out early if there is nothing to do. if(suppress) { return; } - if(!logcontrol && (level > debug_level || logmode == LOGMODE_NULL)) { - return; - } - if(level <= debug_level) { switch(logmode) { case LOGMODE_STDERR: @@ -125,6 +124,10 @@ void logger(int level, int priority, const char *format, ...) { va_list ap; char message[1024] = ""; + if(!should_log(level)) { + return; + } + va_start(ap, format); int len = vsnprintf(message, sizeof(message), format, ap); message[sizeof(message) - 1] = 0; @@ -142,6 +145,10 @@ static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap char message[1024]; size_t msglen = sizeof(message); + if(!should_log(DEBUG_ALWAYS)) { + return; + } + int len = vsnprintf(message, msglen, format, ap); message[sizeof(message) - 1] = 0;