How to remove log debugging entries from a program

I use boost::logas a registrar for my C ++ program. During development, I often use it this way, for example:

#define LOG(severity) BOOST_LOG_SEV(boost::logger::get(), (severity))
#define LOG_ERR LOG(Severity::error)
#define LOG_INFO LOG(Severity::info)
#define LOG_DEBUG LOG(Severity::debug)

where BOOST_LOG_SEV- is the means provided boost::log, and LOG, LOG_ERROR, LOG_INFO, LOG_DEBUG- labels that defined me.

In short, it BOOST_LOG_SEVdynamically compares the current debugging degree with the severity passed to the macro itself to decide whether to output the result or not.

This is an example program that uses the above macros for debugging purposes:

// set at compile time
#define MAX_LOG_SEVERITY Severity::debug

int main() {
   // Print all the messages with a
   // Severity <= MAX_LOG_SEVERITY defined before compiling
   boost::log::set_severity(boost::logger::get(), MAX_LOG_SEVERITY); // set_severity() is fictitious just to give you an idea

   // bool err = ...
   if (err)
      LOG_ERR << "An error occurred";
   else 
      LOG_INFO << "Okay;
   LOG_DEBUG << "main() called";
}

, , Severity::debug . MAX_LOG_SEVERITY Severity::info, , , LOG_DEBUG, . .

, operator<<().

, / LOG_DEBUG, ( ) "" "" , MAX_LOG_SEVERITY Severity::debug?

+5
3

, - . , , <

#ifdef NO_LOG_DEBUG

static class DevNull
{
} dev_null;

template <typename T>
DevNull & operator<<(DevNull & dest, T)
{
    return dest;
}

#define LOG_DEBUG dev_null

#else

#define LOG_DEBUG LOG(Severity::debug)

#endif
+6

@ MartinShobe :

  • g++ (4.7.2) -O1
  • clang++ (3.4) -O2
  • Visual Studio (2008) /OPT:REF
+2

Disables all optimizations in the program and speeds up compilation.

/ Od or boot_log_stop

-2
source

All Articles