Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lazy evaluation of log arguments #399

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions source/vibe/core/log.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't compile because if T.length == 0 args_copy is undefined

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is inside of static if (T.length != 0) { ... }, so it should still work.

Copy link
Contributor

@Boris-Barboris Boris-Barboris Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see, my fork is stuck in an old version where formattedWrite is not in static if block

}

foreach (l; getLoggers())
if (l.minLevel <= level) { // WARNING: TYPE SYSTEM HOLE: accessing field of shared class!
Expand All @@ -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;
Expand Down
Loading