Overview
Features
Download
Documentation
Community
Add-Ons & Services

Logging convenience macros for stream interface

Tips and tricks for POCO C++ Libraries users.

Logging convenience macros for stream interface

Postby Tavi » 28 Nov 2011, 13:04

Here are some convenience macros to use the logging stream interface.
You can use them like this:
Code: Select all
// mysource.cpp

#include "LogUtils.h"

namespace MyApp
{

LOG_DEFINE_MODULE_DEFAULT("MyApp.MyClass");

MyClass::function()
{
    LOG_DEBUG << "entering the function param=" << param;
}

}


Here are they:

Code: Select all
// LogUtils.h
#include "Poco/LogStream.h"

/// Convenience macros for Poco Logging

/// Define a static function that holds the logger with the specified name.
/// Add this to the cpp file where you want to use LOG_XXX macros
#define LOG_DEFINE_MODULE_DEFAULT(name) static Poco::Logger& logger() { static Poco::Logger& logger = Poco::Logger::get(name); return logger; }

/// you can set the max log level by defining LOG_MAX_LEVEL.
/// Define ENABLE_DEBUG_LOG_IN_RELEASE to have DEBUG and TRACE log level in release build
#ifndef LOG_MAX_LEVEL
    #if defined(_DEBUG) || defined(ENABLE_DEBUG_LOG_IN_RELEASE)
        #define LOG_MAX_LEVEL Poco::Message::PRIO_TRACE
    #else
        #define LOG_MAX_LEVEL Poco::Message::PRIO_INFORMATION
    #endif
#endif

/// Convenience macros to get a logger stream.
/// Use like: LOG_INFO << "my log message var1=" << var1 << " var2=" << var2;
/// A function Poco::Logger& logger() must be available in the current context.
/// Use LOG_DEFINE_MODULE_DEFAULT("Category.Subcategory");
#define LOG_FATAL \
   if(Poco::Message::PRIO_FATAL > LOG_MAX_LEVEL) ; \
   else if (!(logger()).fatal()) ; \
    else LogStreamDirect(logger()).fatal()

#define LOG_CRITICAL \
   if(Poco::Message::PRIO_CRITICAL > LOG_MAX_LEVEL) ; \
   else if (!(logger()).critical()) ; \
    else LogStreamDirect(logger()).critical()

#define LOG_ERROR \
   if(Poco::Message::PRIO_ERROR > LOG_MAX_LEVEL) ; \
   else if (!(logger()).error()) ; \
    else LogStreamDirect(logger()).error()

#define LOG_WARN \
   if(Poco::Message::PRIO_WARNING > LOG_MAX_LEVEL) ; \
   else if (!(logger()).warning()) ; \
    else LogStreamDirect(logger()).warning()

#define LOG_NOTICE \
   if(Poco::Message::PRIO_NOTICE > LOG_MAX_LEVEL) ; \
   else if (!(logger()).notice()) ; \
    else LogStreamDirect(logger()).notice()

#define LOG_INFO \
   if(Poco::Message::PRIO_INFORMATION > LOG_MAX_LEVEL) ; \
   else if (!(logger()).information()) ; \
    else LogStreamDirect(logger()).information()

#define LOG_DEBUG \
    if(Poco::Message::PRIO_DEBUG > LOG_MAX_LEVEL) ; \
   else if (!(logger()).debug()) ; \
    else LogStreamDirect(logger()).debug()

#define LOG_TRACE \
   if(Poco::Message::PRIO_TRACE > LOG_MAX_LEVEL) ; \
   else if (!(logger()).trace()) ; \
    else LogStreamDirect(logger()).trace()


/// LogStream specialization that appends a std::endl in destructor. Used by LOG_XXX macros
class LogStreamDirect : public Poco::LogStream
{
public:
    LogStreamDirect(Poco::Logger& logger, Poco::Message::Priority priority = Poco::Message::PRIO_INFORMATION)
        : Poco::LogStream(logger, priority)
    {
    }

    ~LogStreamDirect()
    {
        // LogStream sends the content only when new line is encountered
        *this << std::endl;
    }
};
Tavi
 
Posts: 7
Joined: 28 Nov 2011, 12:50

Return to Tips & Tricks

Who is online

Users browsing this forum: No registered users and 0 guests

cron