- Main Page
- Classes
- Files

- File List

# include/pion/logger.hpp

    00001 // ---------------------------------------------------------------------
    00002 // pion:  a Boost C++ framework for building lightweight HTTP interfaces
    00003 // ---------------------------------------------------------------------
    00004 // Copyright (C) 2007-2012 Cloudmeter, Inc.  (http://www.cloudmeter.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_LOGGER_HEADER__
    00011 #define __PION_LOGGER_HEADER__
    00012
    00013 #include <pion/config.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  logger;
    00044         typedef log4cxx::AppenderSkeleton   log_appender;
    00045         typedef log_appender *   log_appender_ptr;
    00046     }
    00047
    00048     #define PION_HAS_LOG_APPENDER   1
    00049     #define PION_LOG_CONFIG_BASIC   log4cxx::BasicConfigurator::configure();
    00050     #define PION_LOG_CONFIG(FILE)   log4cxx::PropertyConfigurator::configure(FILE);
    00051     #define PION_GET_LOGGER(NAME)   log4cxx::Logger::get_logger(NAME)
    00052
    00053     #define PION_LOG_SETLEVEL_DEBUG(LOG)    LOG->setLevel(log4cxx::Level::toLevel(log4cxx::Level::DEBUG_INT));
    00054     #define PION_LOG_SETLEVEL_INFO(LOG)     LOG->setLevel(log4cxx::Level::toLevel(log4cxx::Level::INFO_INT));
    00055     #define PION_LOG_SETLEVEL_WARN(LOG)     LOG->setLevel(log4cxx::Level::toLevel(log4cxx::Level::WARN_INT));
    00056     #define PION_LOG_SETLEVEL_ERROR(LOG)    LOG->setLevel(log4cxx::Level::toLevel(log4cxx::Level::ERROR_INT));
    00057     #define PION_LOG_SETLEVEL_FATAL(LOG)    LOG->setLevel(log4cxx::Level::toLevel(log4cxx::Level::FATAL_INT));
    00058     #define PION_LOG_SETLEVEL_UP(LOG)       LOG->setLevel(LOG->getLevel()->toInt()+1);
    00059     #define PION_LOG_SETLEVEL_DOWN(LOG)     LOG->setLevel(LOG->getLevel()->toInt()-1);
    00060
    00061     #define PION_LOG_DEBUG  LOG4CXX_DEBUG
    00062     #define PION_LOG_INFO   LOG4CXX_INFO
    00063     #define PION_LOG_WARN   LOG4CXX_WARN
    00064     #define PION_LOG_ERROR  LOG4CXX_ERROR
    00065     #define PION_LOG_FATAL  LOG4CXX_FATAL
    00066
    00067 #elif defined(PION_USE_LOG4CPLUS)
    00068
    00069
    00070     // log4cplus headers
    00071     #include <log4cplus/logger.h>
    00072     #include <log4cplus/configurator.h>
    00073     #include <log4cplus/appender.h>
    00074     #include <log4cplus/spi/loggingevent.h>
    00075     #include <log4cplus/loglevel.h>
    00076
    00077     #include <boost/circular_buffer.hpp>
    00078     #include <boost/thread/mutex.hpp>
    00079
    00080     #if defined _MSC_VER
    00081         #if defined _DEBUG
    00082             #if defined PION_STATIC_LINKING
    00083                 #pragma comment(lib, "log4cplusSD")
    00084             #else
    00085                 #pragma comment(lib, "log4cplusD")
    00086             #endif
    00087         #else
    00088             #if defined PION_STATIC_LINKING
    00089                 #pragma comment(lib, "log4cplusS")
    00090             #else
    00091                 #pragma comment(lib, "log4cplus")
    00092             #endif
    00093         #endif
    00094     #endif
    00095
    00096     namespace pion {
    00097         typedef log4cplus::Logger   logger;
    00098         typedef log4cplus::Appender log_appender;
    00099         typedef log4cplus::SharedAppenderPtr    log_appender_ptr;
    00100
    00104         class circular_buffer_appender : public log4cplus::Appender
    00105         {
    00106         public:
    00107             typedef boost::circular_buffer<log4cplus::spi::InternalLoggingEvent> LogEventBuffer;
    00108
    00109             // default constructor and destructor
    00110             circular_buffer_appender(void) : m_log_events(1000) {};
    00111             virtual ~circular_buffer_appender() {}
    00112
    00114             const LogEventBuffer& getLogIterator() const {
    00115                 return m_log_events;
    00116             }
    00117
    00118         public:
    00119             // member functions inherited from the Appender interface class
    00120             virtual void close() {}
    00121         protected:
    00122             virtual void append(const log4cplus::spi::InternalLoggingEvent& event) {
    00123                 boost::mutex::scoped_lock log_lock(m_log_mutex);
    00124                 m_log_events.push_back(*event.clone());
    00125             }
    00126
    00127         private:
    00129             LogEventBuffer  m_log_events;
    00130
    00132             boost::mutex    m_log_mutex;
    00133         };
    00134     }
    00135
    00136     #define PION_HAS_LOG_APPENDER   1
    00137     #define PION_LOG_CONFIG_BASIC   log4cplus::BasicConfigurator::doConfigure();
    00138     #define PION_LOG_CONFIG(FILE)   log4cplus::PropertyConfigurator::doConfigure(FILE);
    00139     #define PION_GET_LOGGER(NAME)   log4cplus::Logger::getInstance(NAME)
    00140
    00141     #define PION_LOG_SETLEVEL_DEBUG(LOG)    LOG.setLogLevel(log4cplus::DEBUG_LOG_LEVEL);
    00142     #define PION_LOG_SETLEVEL_INFO(LOG)     LOG.setLogLevel(log4cplus::INFO_LOG_LEVEL);
    00143     #define PION_LOG_SETLEVEL_WARN(LOG)     LOG.setLogLevel(log4cplus::WARN_LOG_LEVEL);
    00144     #define PION_LOG_SETLEVEL_ERROR(LOG)    LOG.setLogLevel(log4cplus::ERROR_LOG_LEVEL);
    00145     #define PION_LOG_SETLEVEL_FATAL(LOG)    LOG.setLogLevel(log4cplus::FATAL_LOG_LEVEL);
    00146     #define PION_LOG_SETLEVEL_UP(LOG)       LOG.setLogLevel(LOG.getLogLevel()+1);
    00147     #define PION_LOG_SETLEVEL_DOWN(LOG)     LOG.setLogLevel(LOG.getLogLevel()-1);
    00148
    00149     #define PION_LOG_DEBUG  LOG4CPLUS_DEBUG
    00150     #define PION_LOG_INFO   LOG4CPLUS_INFO
    00151     #define PION_LOG_WARN   LOG4CPLUS_WARN
    00152     #define PION_LOG_ERROR  LOG4CPLUS_ERROR
    00153     #define PION_LOG_FATAL  LOG4CPLUS_FATAL
    00154
    00155
    00156 #elif defined(PION_USE_LOG4CPP)
    00157
    00158
    00159     // log4cpp headers
    00160     #include <log4cpp/Category.hh>
    00161     #include <log4cpp/BasicLayout.hh>
    00162     #include <log4cpp/OstreamAppender.hh>
    00163     #include <log4cpp/AppenderSkeleton.hh>
    00164
    00165     namespace pion {
    00166         typedef log4cpp::Category*  logger;
    00167         typedef log4cpp::AppenderSkeleton   log_appender;
    00168         typedef log_appender *   log_appender_ptr;
    00169     }
    00170
    00171     #define PION_HAS_LOG_APPENDER   1
    00172     #define PION_LOG_CONFIG_BASIC   { log4cpp::OstreamAppender *app = new log4cpp::OstreamAppender("cout", &std::cout); app->setLayout(new log4cpp::BasicLayout()); log4cpp::Category::getRoot().setAppender(app); }
    00173     #define PION_LOG_CONFIG(FILE)   { log4cpp::PropertyConfigurator::configure(FILE); }
    00174     #define PION_GET_LOGGER(NAME)   (&log4cpp::Category::getInstance(NAME))
    00175
    00176     #define PION_LOG_SETLEVEL_DEBUG(LOG)    { LOG->setPriority(log4cpp::Priority::DEBUG); }
    00177     #define PION_LOG_SETLEVEL_INFO(LOG)     { LOG->setPriority(log4cpp::Priority::INFO); }
    00178     #define PION_LOG_SETLEVEL_WARN(LOG)     { LOG->setPriority(log4cpp::Priority::WARN); }
    00179     #define PION_LOG_SETLEVEL_ERROR(LOG)    { LOG->setPriority(log4cpp::Priority::ERROR); }
    00180     #define PION_LOG_SETLEVEL_FATAL(LOG)    { LOG->setPriority(log4cpp::Priority::FATAL); }
    00181     #define PION_LOG_SETLEVEL_UP(LOG)       { LOG->setPriority(LOG.getPriority()+1); }
    00182     #define PION_LOG_SETLEVEL_DOWN(LOG)     { LOG->setPriority(LOG.getPriority()-1); }
    00183
    00184     #define PION_LOG_DEBUG(LOG, MSG)    if (LOG->getPriority()>=log4cpp::Priority::DEBUG) { LOG->debugStream() << MSG; }
    00185     #define PION_LOG_INFO(LOG, MSG)     if (LOG->getPriority()>=log4cpp::Priority::INFO) { LOG->infoStream() << MSG; }
    00186     #define PION_LOG_WARN(LOG, MSG)     if (LOG->getPriority()>=log4cpp::Priority::WARN) { LOG->warnStream() << MSG; }
    00187     #define PION_LOG_ERROR(LOG, MSG)    if (LOG->getPriority()>=log4cpp::Priority::ERROR) { LOG->errorStream() << MSG; }
    00188     #define PION_LOG_FATAL(LOG, MSG)    if (LOG->getPriority()>=log4cpp::Priority::FATAL) { LOG->fatalStream() << MSG; }
    00189
    00190 #elif defined(PION_DISABLE_LOGGING)
    00191
    00192     // Logging is disabled -> add do-nothing stubs for logging
    00193     namespace pion {
    00194         typedef int     logger;
    00195         typedef int     log_appender;
    00196         typedef log_appender *   log_appender_ptr;
    00197     }
    00198
    00199     #undef PION_HAS_LOG_APPENDER
    00200     #define PION_LOG_CONFIG_BASIC   {}
    00201     #define PION_LOG_CONFIG(FILE)   {}
    00202     #define PION_GET_LOGGER(NAME)   0
    00203
    00204     // use "++LOG" to avoid warnings about LOG not being used
    00205     #define PION_LOG_SETLEVEL_DEBUG(LOG)    { if (false) ++LOG; }
    00206     #define PION_LOG_SETLEVEL_INFO(LOG)     { if (false) ++LOG; }
    00207     #define PION_LOG_SETLEVEL_WARN(LOG)     { if (false) ++LOG; }
    00208     #define PION_LOG_SETLEVEL_ERROR(LOG)    { if (false) ++LOG; }
    00209     #define PION_LOG_SETLEVEL_FATAL(LOG)    { if (false) ++LOG; }
    00210     #define PION_LOG_SETLEVEL_UP(LOG)       { if (false) ++LOG; }
    00211     #define PION_LOG_SETLEVEL_DOWN(LOG)     { if (false) ++LOG; }
    00212
    00213     // use "++LOG" to avoid warnings about LOG not being used
    00214     #define PION_LOG_DEBUG(LOG, MSG)    { if (false) ++LOG; }
    00215     #define PION_LOG_INFO(LOG, MSG)     { if (false) ++LOG; }
    00216     #define PION_LOG_WARN(LOG, MSG)     { if (false) ++LOG; }
    00217     #define PION_LOG_ERROR(LOG, MSG)    { if (false) ++LOG; }
    00218     #define PION_LOG_FATAL(LOG, MSG)    { if (false) ++LOG; }
    00219
    00220 #else
    00221
    00222     #define PION_USE_OSTREAM_LOGGING
    00223
    00224     // Logging uses std::cout and std::cerr
    00225     #include <iostream>
    00226     #include <string>
    00227     #include <ctime>
    00228
    00229     namespace pion {
    00230         struct PION_API logger {
    00231             enum log_priority_type {
    00232                 LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARN,
    00233                 LOG_LEVEL_ERROR, LOG_LEVEL_FATAL
    00234             };
    00235             ~logger() {}
    00236             logger(void) : m_name("pion") {}
    00237             logger(const std::string& name) : m_name(name) {}
    00238             logger(const logger& p) : m_name(p.m_name) {}
    00239             std::string                 m_name;
    00240             static log_priority_type     m_priority;
    00241         };
    00242         typedef int     log_appender;
    00243         typedef log_appender *   log_appender_ptr;
    00244     }
    00245
    00246     #undef PION_HAS_LOG_APPENDER
    00247     #define PION_LOG_CONFIG_BASIC   {}
    00248     #define PION_LOG_CONFIG(FILE)   {}
    00249     #define PION_GET_LOGGER(NAME)   pion::logger(NAME)
    00250
    00251     #define PION_LOG_SETLEVEL_DEBUG(LOG)    { LOG.m_priority = pion::logger::LOG_LEVEL_DEBUG; }
    00252     #define PION_LOG_SETLEVEL_INFO(LOG)     { LOG.m_priority = pion::logger::LOG_LEVEL_INFO; }
    00253     #define PION_LOG_SETLEVEL_WARN(LOG)     { LOG.m_priority = pion::logger::LOG_LEVEL_WARN; }
    00254     #define PION_LOG_SETLEVEL_ERROR(LOG)    { LOG.m_priority = pion::logger::LOG_LEVEL_ERROR; }
    00255     #define PION_LOG_SETLEVEL_FATAL(LOG)    { LOG.m_priority = pion::logger::LOG_LEVEL_FATAL; }
    00256     #define PION_LOG_SETLEVEL_UP(LOG)       { ++LOG.m_priority; }
    00257     #define PION_LOG_SETLEVEL_DOWN(LOG)     { --LOG.m_priority; }
    00258
    00259     #define PION_LOG_DEBUG(LOG, MSG)    if (LOG.m_priority <= pion::logger::LOG_LEVEL_DEBUG) { std::cout << time(NULL) << " DEBUG " << LOG.m_name << ' ' << MSG << std::endl; }
    00260     #define PION_LOG_INFO(LOG, MSG)     if (LOG.m_priority <= pion::logger::LOG_LEVEL_INFO) { std::cout << time(NULL) << " INFO " << LOG.m_name << ' ' << MSG << std::endl; }
    00261     #define PION_LOG_WARN(LOG, MSG)     if (LOG.m_priority <= pion::logger::LOG_LEVEL_WARN) { std::cerr << time(NULL) << " WARN " << LOG.m_name << ' ' << MSG << std::endl; }
    00262     #define PION_LOG_ERROR(LOG, MSG)    if (LOG.m_priority <= pion::logger::LOG_LEVEL_ERROR) { std::cerr << time(NULL) << " ERROR " << LOG.m_name << ' ' << MSG << std::endl; }
    00263     #define PION_LOG_FATAL(LOG, MSG)    if (LOG.m_priority <= pion::logger::LOG_LEVEL_FATAL) { std::cerr << time(NULL) << " FATAL " << LOG.m_name << ' ' << MSG << std::endl; }
    00264
    00265 #endif
    00266
    00267 #endif