
Daniel Frey wrote:
John Torjo wrote:
I think it's the wrong approach. In our company we have implemented logging through macros which has one very important property: When I write
LOG_DEBUG( whatever() );
into my source, then whatever() is only evaluated when the debug-logger is active. The macro basically expands to something like this:
if( Base::hasLogger( LogLevel::DEBUG ) ) Base::log( whatever(), LogLevel::DEBUG );
I think this cannot be done without macros as normal function calls (including operator<< for streams) need to evaluate their arguments before they are called. As the difference in the application execution speed can vary by a factor of 1000, this is a very valuable optimization for us. My 2ยข.
Man, you must be doing a LOT of logging ;)
Well, too much for my taste, but some other team-members (namely by boss :) insists on logging almost everything. And I have to admit that it can be a real help from time-to-time. I just wish I would be allowed to implement a better hasLogger() so I can selectively enable only the log-messages/areas/levels/... that I'm interested in :)
I do quite a lot of logging myself, but I've never needed anything like above.
Maybe because when the framework doesn't support the feature I mentioned above, you won't add anything that makes the programm 1000x slower to your logging? ;)
Anyway, in my code I have functions, which return a log_stream (a thread-safe wrapper over a stream).
And use it like this:
activity_log() << "My app. started at" << time(0) << std::endl; error_log() << "should have not arrived here" << std::endl; // etc.
And if I truly want, I can implement an operator bool(), and be able to disable it at runtime, like this:
if (activity_log()) activity_log() << ...;
Hm, too verbose for my taste. With the macro, you get it for free.
True. I can also add a macro, something like: log(activity) << ...; Best, John