I need a logger for debug purpose and I'm using Boost.Log (1.54.0 with a patch in the boost.org homepage).
It's all fine I've created some macro like this:
#define LOG_MESSAGE( lvl ) BOOST_LOG_TRIVIAL( lvl )
Now is that a way that LOG_MESSAGE( lvl ) is expaneded in BOOST_LOG_TRIVIAL( lvl ) only in debug mode and ignore in release?
for example:
LOG_MESSAGE( critical ) << "If I read this message we're in debug mode"
edit My first attempt is to create a nullstream... I think that in release mode compiler will optimize it...
#if !defined( NDEBUG )
#include <boost/log/trivial.hpp>
#define LOG_MESSAGE( lvl ) BOOST_LOG_TRIVIAL( lvl )
#else
#if defined( __GNUC__ )
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-value"
#endif
#include <iosfwd>
struct nullstream : public std::ostream {
nullstream() : std::ios(0), std::ostream(0) {}
};
static nullstream g_nullstream;
#define LOG_MESSAGE( lvl ) g_nullstream
#if defined( __GNUC__ )
#pragma GCC diagnostic pop
#endif
#endif
inline
. They're also empty. Calling them is only theoretical, in practice the optimizer will remove every trace ofNullLogger
. – Ophthalmoscopy