# common/include/pion/PionLogger.hpp

    00001 // -----------------------------------------------------------------------
    00002 // pion-common: a collection of common libraries used by the Pion Platform
    00003 // -----------------------------------------------------------------------
    00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
    00005 //
    00006 // Distributed under the Boost Software License, Version 1.0.
    00007 // See http://www.boost.org/LICENSE_1_0.txt
    00008 //
    00009
    00010 #ifndef __PION_PIONLOGGER_HEADER__
    00011 #define __PION_PIONLOGGER_HEADER__
    00012
    00013 #include <pion/PionConfig.hpp>
    00014
    00015
    00016 #if defined(PION_USE_LOG4CXX)
    00017
    00018     // unfortunately, the current version of log4cxx has many problems that
    00019     // produce very annoying warnings
    00020
    00021     // log4cxx headers
    00022     #include <log4cxx/logger.h>
    00023 #ifdef _MSC_VER
    00024     #pragma warning(push)
    00025     #pragma warning(disable: 4231) // nonstandard extension used : 'extern' before template explicit instantiation
    00026 #endif
    00027     #include <log4cxx/basicconfigurator.h>
    00028     #include <log4cxx/propertyconfigurator.h>
    00029 #ifdef _MSC_VER
    00030     #pragma warning(pop)
    00031 #endif
    00032
    00033     #if defined _MSC_VER
    00034         #if defined _DEBUG
    00035             #pragma comment(lib, "log4cxxd")
    00036         #else
    00037             #pragma comment(lib, "log4cxx")
    00038         #endif
    00039         #pragma comment(lib, "odbc32")
    00040     #endif
    00041
    00042     namespace pion {
    00043         typedef log4cxx::LoggerPtr  PionLogger;
    00044         typedef log4cxx::AppenderSkeleton   PionLogAppender;
    00045         typedef PionLogAppender *   PionLogAppenderPtr;
    00046     }
    00047
    00048     #define PION_LOG_CONFIG_BASIC   log4cxx::BasicConfigurator::configure();
    00049     #define PION_LOG_CONFIG(FILE)   log4cxx::PropertyConfigurator::configure(FILE);
    00050     #define PION_GET_LOGGER(NAME)   log4cxx::Logger::getLogger(NAME)
    00051
    00052     #define PION_LOG_SETLEVEL_DEBUG(LOG)    LOG->setLevel(log4cxx::Level::toLevel(log4cxx::Level::DEBUG_INT));
    00053     #define PION_LOG_SETLEVEL_INFO(LOG)     LOG->setLevel(log4cxx::Level::toLevel(log4cxx::Level::INFO_INT));
    00054     #define PION_LOG_SETLEVEL_WARN(LOG)     LOG->setLevel(log4cxx::Level::toLevel(log4cxx::Level::WARN_INT));
    00055     #define PION_LOG_SETLEVEL_ERROR(LOG)    LOG->setLevel(log4cxx::Level::toLevel(log4cxx::Level::ERROR_INT));
    00056     #define PION_LOG_SETLEVEL_FATAL(LOG)    LOG->setLevel(log4cxx::Level::toLevel(log4cxx::Level::FATAL_INT));
    00057     #define PION_LOG_SETLEVEL_UP(LOG)       LOG->setLevel(LOG->getLevel()->toInt()+1);
    00058     #define PION_LOG_SETLEVEL_DOWN(LOG)     LOG->setLevel(LOG->getLevel()->toInt()-1);
    00059
    00060     #define PION_LOG_DEBUG  LOG4CXX_DEBUG
    00061     #define PION_LOG_INFO   LOG4CXX_INFO
    00062     #define PION_LOG_WARN   LOG4CXX_WARN
    00063     #define PION_LOG_ERROR  LOG4CXX_ERROR
    00064     #define PION_LOG_FATAL  LOG4CXX_FATAL
    00065
    00066 #elif defined(PION_USE_LOG4CPLUS)
    00067
    00068
    00069     // log4cplus headers
    00070     #include <log4cplus/logger.h>
    00071     #include <log4cplus/configurator.h>
    00072     #include <log4cplus/appender.h>
    00073     #include <log4cplus/spi/loggingevent.h>
    00074     #include <log4cplus/loglevel.h>
    00075
    00076     #include <boost/circular_buffer.hpp>
    00077     #include <boost/thread/mutex.hpp>
    00078
    00079     #if defined _MSC_VER
    00080         #if defined _DEBUG
    00081             #pragma comment(lib, "log4cplusD")
    00082         #else
    00083             #pragma comment(lib, "log4cplus")
    00084         #endif
    00085     #endif
    00086
    00087     namespace pion {
    00088         typedef log4cplus::Logger   PionLogger;
    00089         typedef log4cplus::Appender PionLogAppender;
    00090         typedef log4cplus::SharedAppenderPtr    PionLogAppenderPtr;
    00091
    00095         class CircularBufferAppender : public log4cplus::Appender
    00096         {
    00097         public:
    00098             typedef boost::circular_buffer<log4cplus::spi::InternalLoggingEvent> LogEventBuffer;
    00099
    00100             // default constructor and destructor
    00101             CircularBufferAppender(void) : m_log_events(1000) {};
    00102             virtual ~CircularBufferAppender() {}
    00103
    00105             const LogEventBuffer& getLogIterator() const {
    00106                 return m_log_events;
    00107             }
    00108
    00109         public:
    00110             // member functions inherited from the Appender interface class
    00111             virtual void close() {}
    00112         protected:
    00113             virtual void append(const log4cplus::spi::InternalLoggingEvent& event) {
    00114                 boost::mutex::scoped_lock log_lock(m_log_mutex);
    00115                 m_log_events.push_back(*event.clone());
    00116             }
    00117
    00118         private:
    00120             LogEventBuffer  m_log_events;
    00121
    00123             boost::mutex    m_log_mutex;
    00124         };
    00125     }
    00126
    00127     #define PION_LOG_CONFIG_BASIC   log4cplus::BasicConfigurator::doConfigure();
    00128     #define PION_LOG_CONFIG(FILE)   log4cplus::PropertyConfigurator::doConfigure(FILE);
    00129     #define PION_GET_LOGGER(NAME)   log4cplus::Logger::getInstance(NAME)
    00130
    00131     #define PION_LOG_SETLEVEL_DEBUG(LOG)    LOG.setLogLevel(log4cplus::DEBUG_LOG_LEVEL);
    00132     #define PION_LOG_SETLEVEL_INFO(LOG)     LOG.setLogLevel(log4cplus::INFO_LOG_LEVEL);
    00133     #define PION_LOG_SETLEVEL_WARN(LOG)     LOG.setLogLevel(log4cplus::WARN_LOG_LEVEL);
    00134     #define PION_LOG_SETLEVEL_ERROR(LOG)    LOG.setLogLevel(log4cplus::ERROR_LOG_LEVEL);
    00135     #define PION_LOG_SETLEVEL_FATAL(LOG)    LOG.setLogLevel(log4cplus::FATAL_LOG_LEVEL);
    00136     #define PION_LOG_SETLEVEL_UP(LOG)       LOG.setLogLevel(LOG.getLogLevel()+1);
    00137     #define PION_LOG_SETLEVEL_DOWN(LOG)     LOG.setLogLevel(LOG.getLogLevel()-1);
    00138
    00139     #define PION_LOG_DEBUG  LOG4CPLUS_DEBUG
    00140     #define PION_LOG_INFO   LOG4CPLUS_INFO
    00141     #define PION_LOG_WARN   LOG4CPLUS_WARN
    00142     #define PION_LOG_ERROR  LOG4CPLUS_ERROR
    00143     #define PION_LOG_FATAL  LOG4CPLUS_FATAL
    00144
    00145
    00146 #elif defined(PION_USE_LOG4CPP)
    00147
    00148
    00149     // log4cpp headers
    00150     #include <log4cpp/Category.hh>
    00151     #include <log4cpp/BasicLayout.hh>
    00152     #include <log4cpp/OstreamAppender.hh>
    00153     #include <log4cpp/AppenderSkeleton.hh>
    00154
    00155     namespace pion {
    00156         typedef log4cpp::Category*  PionLogger;
    00157         typedef log4cpp::AppenderSkeleton   PionLogAppender;
    00158         typedef PionLogAppender *   PionLogAppenderPtr;
    00159     }
    00160
    00161     #define PION_LOG_CONFIG_BASIC   { log4cpp::OstreamAppender *app = new log4cpp::OstreamAppender("cout", &std::cout); app->setLayout(new log4cpp::BasicLayout()); log4cpp::Category::getRoot().setAppender(app); }
    00162     #define PION_LOG_CONFIG(FILE)   { log4cpp::PropertyConfigurator::configure(FILE); }
    00163     #define PION_GET_LOGGER(NAME)   (&log4cpp::Category::getInstance(NAME))
    00164
    00165     #define PION_LOG_SETLEVEL_DEBUG(LOG)    { LOG->setPriority(log4cpp::Priority::DEBUG); }
    00166     #define PION_LOG_SETLEVEL_INFO(LOG)     { LOG->setPriority(log4cpp::Priority::INFO); }
    00167     #define PION_LOG_SETLEVEL_WARN(LOG)     { LOG->setPriority(log4cpp::Priority::WARN); }
    00168     #define PION_LOG_SETLEVEL_ERROR(LOG)    { LOG->setPriority(log4cpp::Priority::ERROR); }
    00169     #define PION_LOG_SETLEVEL_FATAL(LOG)    { LOG->setPriority(log4cpp::Priority::FATAL); }
    00170     #define PION_LOG_SETLEVEL_UP(LOG)       { LOG->setPriority(LOG.getPriority()+1); }
    00171     #define PION_LOG_SETLEVEL_DOWN(LOG)     { LOG->setPriority(LOG.getPriority()-1); }
    00172
    00173     #define PION_LOG_DEBUG(LOG, MSG)    if (LOG->getPriority()>=log4cpp::Priority::DEBUG) { LOG->debugStream() << MSG; }
    00174     #define PION_LOG_INFO(LOG, MSG)     if (LOG->getPriority()>=log4cpp::Priority::INFO) { LOG->infoStream() << MSG; }
    00175     #define PION_LOG_WARN(LOG, MSG)     if (LOG->getPriority()>=log4cpp::Priority::WARN) { LOG->warnStream() << MSG; }
    00176     #define PION_LOG_ERROR(LOG, MSG)    if (LOG->getPriority()>=log4cpp::Priority::ERROR) { LOG->errorStream() << MSG; }
    00177     #define PION_LOG_FATAL(LOG, MSG)    if (LOG->getPriority()>=log4cpp::Priority::FATAL) { LOG->fatalStream() << MSG; }
    00178
    00179 #elif defined(PION_DISABLE_LOGGING)
    00180
    00181     // Logging is disabled -> add do-nothing stubs for logging
    00182     namespace pion {
    00183         typedef int     PionLogger;
    00184     }
    00185
    00186     #define PION_LOG_CONFIG_BASIC   {}
    00187     #define PION_LOG_CONFIG(FILE)   {}
    00188     #define PION_GET_LOGGER(NAME)   0
    00189
    00190     // use "++LOG" to avoid warnings about LOG not being used
    00191     #define PION_LOG_SETLEVEL_DEBUG(LOG)    { if (false) ++LOG; }
    00192     #define PION_LOG_SETLEVEL_INFO(LOG)     { if (false) ++LOG; }
    00193     #define PION_LOG_SETLEVEL_WARN(LOG)     { if (false) ++LOG; }
    00194     #define PION_LOG_SETLEVEL_ERROR(LOG)    { if (false) ++LOG; }
    00195     #define PION_LOG_SETLEVEL_FATAL(LOG)    { if (false) ++LOG; }
    00196     #define PION_LOG_SETLEVEL_UP(LOG)       { if (false) ++LOG; }
    00197     #define PION_LOG_SETLEVEL_DOWN(LOG)     { if (false) ++LOG; }
    00198
    00199     // use "++LOG" to avoid warnings about LOG not being used
    00200     #define PION_LOG_DEBUG(LOG, MSG)    { if (false) ++LOG; }
    00201     #define PION_LOG_INFO(LOG, MSG)     { if (false) ++LOG; }
    00202     #define PION_LOG_WARN(LOG, MSG)     { if (false) ++LOG; }
    00203     #define PION_LOG_ERROR(LOG, MSG)    { if (false) ++LOG; }
    00204     #define PION_LOG_FATAL(LOG, MSG)    { if (false) ++LOG; }
    00205
    00206 #else
    00207
    00208     #define PION_USE_OSTREAM_LOGGING
    00209
    00210     // Logging uses std::cout and std::cerr
    00211     #include <iostream>
    00212     #include <string>
    00213     #include <ctime>
    00214
    00215     namespace pion {
    00216         struct PION_COMMON_API PionLogger {
    00217             enum PionPriorityType {
    00218                 LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARN,
    00219                 LOG_LEVEL_ERROR, LOG_LEVEL_FATAL
    00220             };
    00221             ~PionLogger() {}
    00222             PionLogger(void) : m_name("pion") {}
    00223             PionLogger(const std::string& name) : m_name(name) {}
    00224             PionLogger(const PionLogger& p) : m_name(p.m_name) {}
    00225             std::string                 m_name;
    00226             static PionPriorityType     m_priority;
    00227         };
    00228     }
    00229
    00230     #define PION_LOG_CONFIG_BASIC   {}
    00231     #define PION_LOG_CONFIG(FILE)   {}
    00232     #define PION_GET_LOGGER(NAME)   pion::PionLogger(NAME)
    00233
    00234     #define PION_LOG_SETLEVEL_DEBUG(LOG)    { LOG.m_priority = pion::PionLogger::LOG_LEVEL_DEBUG; }
    00235     #define PION_LOG_SETLEVEL_INFO(LOG)     { LOG.m_priority = pion::PionLogger::LOG_LEVEL_INFO; }
    00236     #define PION_LOG_SETLEVEL_WARN(LOG)     { LOG.m_priority = pion::PionLogger::LOG_LEVEL_WARN; }
    00237     #define PION_LOG_SETLEVEL_ERROR(LOG)    { LOG.m_priority = pion::PionLogger::LOG_LEVEL_ERROR; }
    00238     #define PION_LOG_SETLEVEL_FATAL(LOG)    { LOG.m_priority = pion::PionLogger::LOG_LEVEL_FATAL; }
    00239     #define PION_LOG_SETLEVEL_UP(LOG)       { ++LOG.m_priority; }
    00240     #define PION_LOG_SETLEVEL_DOWN(LOG)     { --LOG.m_priority; }
    00241
    00242     #define PION_LOG_DEBUG(LOG, MSG)    if (LOG.m_priority <= pion::PionLogger::LOG_LEVEL_DEBUG) { std::cout << time(NULL) << " DEBUG " << LOG.m_name << ' ' << MSG << std::endl; }
    00243     #define PION_LOG_INFO(LOG, MSG)     if (LOG.m_priority <= pion::PionLogger::LOG_LEVEL_INFO) { std::cout << time(NULL) << " INFO " << LOG.m_name << ' ' << MSG << std::endl; }
    00244     #define PION_LOG_WARN(LOG, MSG)     if (LOG.m_priority <= pion::PionLogger::LOG_LEVEL_WARN) { std::cerr << time(NULL) << " WARN " << LOG.m_name << ' ' << MSG << std::endl; }
    00245     #define PION_LOG_ERROR(LOG, MSG)    if (LOG.m_priority <= pion::PionLogger::LOG_LEVEL_ERROR) { std::cerr << time(NULL) << " ERROR " << LOG.m_name << ' ' << MSG << std::endl; }
    00246     #define PION_LOG_FATAL(LOG, MSG)    if (LOG.m_priority <= pion::PionLogger::LOG_LEVEL_FATAL) { std::cerr << time(NULL) << " FATAL " << LOG.m_name << ' ' << MSG << std::endl; }
    00247
    00248 #endif
    00249
    00250 #endif