Suppose we have several levels of logging: tracing, debugging, information, error. I was wondering if there is a way to write the following code:
enum log_level = {trace, debug, info, error}; log_level global_log_level = info; void log(log_level level, string& message){ if (level >= global_log_level){ std::cout << message << std::endl; } } string create_message(){ ... } log_level level = debug; log (level, create_message());
without calling create_message if the level is less than global_severity_level. Indeed, create_message can be quite long, and regardless of what it creates the string. If there are many "debug" logs, they can become significant overhead when working in non-debug mode.
I know that this is possible if the "log" function is a macro that calls create_message () only if the severity> minimum_severity; but is there no other way to do this without macros?
EDIT
In the above example, I did not specify create_message because it could be anything, in particular:
log(level, "Created object " + my_object.getName());
In this case, is there a way to record the log so that the complete line is not created in a relatively transparent way for the programmerβs call log?
Many thanks
c ++ logging lazy-evaluation
Ghl
source share