From a1afa99e636d2f4c5e339e6747b67ee6329b334e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Tue, 16 Apr 2024 20:51:49 +0200 Subject: [PATCH] Fix lazy evaluation of log arguments. Fixes https://github.com/vibe-d/vibe-http/pull/36 --- source/vibe/core/log.d | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/source/vibe/core/log.d b/source/vibe/core/log.d index 74baca60..9f7b180e 100644 --- a/source/vibe/core/log.d +++ b/source/vibe/core/log.d @@ -941,8 +941,12 @@ private void doLog(S, T...)(LogLevel level, string mod, string func, string file nothrow { try { - static if(T.length != 0) - auto args_copy = args; + static if(T.length != 0) { + if (!getLoggers().any!(l => l.minLevel <= level)) + return; + + T args_copy = args; + } foreach (l; getLoggers()) if (l.minLevel <= level) { // WARNING: TYPE SYSTEM HOLE: accessing field of shared class! @@ -957,6 +961,13 @@ private void doLog(S, T...)(LogLevel level, string mod, string func, string file } catch(Exception e) debug assert(false, e.msg); } +unittest { // ensure arguments are evaluated lazily + int i = 0; + setLogLevel(LogLevel.info); + logDebug("not visible: %s", i++); + assert(i == 0); +} + private struct LogOutputRange { LogLine info; ScopedLock!Logger* logger;