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

Skipping evaluation of log line expressions with a macro, is it possible? #1862

Closed
dv1 opened this issue Dec 9, 2019 · 1 comment
Closed

Comments

@dv1
Copy link

dv1 commented Dec 9, 2019

In other logging libraries, there are preprocessor macros that make use of an if-expression to make sure the log line expressions are only evaluated if the associated log level is enabled. Example (using iostreams, but fmlib would work as well):

LOG_TRACE("Test " << (4+i) << " " << call_a_slow_function() << " 123");

#define LOG_TRACE(LOGLINE) \
    do {
      if (current_global_log_level >= TRACE) {
        std::cerr << LOGLINE;
      }
    } while (0)

The effect of such a macro is that the expressions within LOGLINE are only evaluated if (current_global_log_level >= TRACE) holds true. So, in other words, (4+1) is not evaluated and call_a_slow_function() is not called if the log level is not at TRACE. This is very useful, since it makes a lot of logging effectively very near zero cost operation (only the integer comparison for the log level check is done always), while still being able to function with log levels that are adjustable at run-time (current_global_log_level could be some internal integer that can be set via some sort of public set_global_log_level() function).

Unfortunately, this is not possible with spdlog, from what it seems. Compile-time macros like SPDLOG_TRACE() use preprocessor macros for enabling/disabling log calls, and do not check the runtime log levels. And the regular logging calls are always evaluated; the decision whether or not to actually log something is done internally.

Or is there a way to achieve what I am describing?

@dv1
Copy link
Author

dv1 commented Dec 9, 2019

Oops, posted in the wrong github project :) Sorry, wrong tab was open.

@dv1 dv1 closed this as completed Dec 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant