Logging Macro Recording

im writing the logging class at the moment. Logger works with streams and also prints the object that is currently being written. Here is the macro:

#define OBJLOG(DL, what) DL <= this->Logger->getDebugLevel() ? *this->Logger << DL << "[" << this->Name << "]: "<< what << std::endl : this->Logger->doNothing();

Pseudocode Varient for a better overview:

#define OBJLOG(debuglevel, what) debuglevel <= logger.debuglevel ? logger.log(what) : logger.doNothing()

Is there a way around the doNothing call, how to do nothing at all? Thanks in advance.

+3
source share
3 answers
#define OBJLOG(DL, what) do { if(DL <= this->Logger->getDebugLevel()) *this->Logger << DL << "[" << this->Name << "]: "<< what << std::endl; } while(0)

See Why use explanations for the seemingly pointless do-while and if-else statements in macros? . ( do {} while(0)This is not strictly necessary here, but I would rather not leak out ostream.)

In addition, the use of macro arguments should always be used in parentheses, for example:

#define OBJLOG(DL, what) do { if((DL) <= this->Logger->getDebugLevel()) *this->Logger << (DL) << "[" << this->Name << "]: "<< (what) << std::endl; } while(0)

, ( ), .

+4
  • logger.log() .

  • : debuglevel <= logger.debuglevel && & & Logger.log

.

+1

If you need an expression that does nothing, try it (void)0.

0
source

All Articles