Hi,
I have quite a tricky problem, and can't figure out a proper solution.
You see, I want to log debug strings in my application, so I wrote a module where the only entry point is a pure method "procedure Log(const LogLevel: TLogLevel; const Str: String);".
So the goal would be to only log strings if the logger is set to log at least that log level. That way, I can easily change the logger to only log error strings for example.
So far so good.
Problem is, when I write things like "Log(llTrace, 'Test ' + GetLastError + BuildStringFromContext); for example, the string will be evaluated wether I log it or not, because the log filter is done inside the function.
I was wandering if there was a way to avoid the evaluation of the string when I know I won't log it?
Of course the obvious solution would be to write
"if CanLog(llTrace) then Log('Test ' + GetLastError + BuildStringFromContext);"
But that would be tedious to write, and make the code harder to read.
So I though of two possibilities:
- First: Using inline function, to replace at compile time the first Log line by the if/then statement. Unfortunately, even with inline function, the arguments are fully eveluated when the function is called (tested). So it's a dead end.
- I also though to use a preprocessor macro, to replace at compile time the fist line by the if/then statement, but Delphi doesn't support C/C++ like defines, it can only set/evaluate booleans.
So do you think of a solution i could've overlooked?
Without any solution, I'll stick to my first Log version, as I value code cleanliness over performance, but it's still a bit frustrating knowing my code will execute instructions for nothing at all if I disable my log....
Regards,