pion-net  4.0.9
net/services/LogService.hpp
00001 // ------------------------------------------------------------------
00002 // pion-net: a C++ framework for building lightweight HTTP interfaces
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_LOGSERVICE_HEADER__
00011 #define __PION_LOGSERVICE_HEADER__
00012 
00013 #include <boost/thread/mutex.hpp>
00014 #include <boost/scoped_ptr.hpp>
00015 #include <pion/PionLogger.hpp>
00016 #include <pion/net/WebService.hpp>
00017 #include <pion/net/HTTPResponseWriter.hpp>
00018 #include <string>
00019 #include <list>
00020 
00021 #if defined(PION_USE_LOG4CXX)
00022     #include <log4cxx/appenderskeleton.h>
00023     // version 0.10.x introduces a new data type that is declared in a
00024     // pool.h header file.  If we're using 0.9.x, just declare the type
00025     // as an int since it is not being used at all
00026     #ifndef _LOG4CXX_HELPERS_POOL_H
00027         namespace log4cxx {
00028             namespace helpers {
00029                 typedef int Pool;
00030             }
00031         }
00032     #endif
00033 #endif
00034 
00035 
00036 namespace pion {        // begin namespace pion
00037 namespace plugins {     // begin namespace plugins
00038 
00039     
00043 class LogServiceAppender
00044 #ifdef PION_HAS_LOG_APPENDER
00045     : public PionLogAppender
00046 #endif
00047 {
00048 public:
00049     // default constructor and destructor
00050     LogServiceAppender(void);
00051     virtual ~LogServiceAppender() {}
00052     
00054     inline void setMaxEvents(unsigned int n) { m_max_events = n; }
00055     
00057     void addLogString(const std::string& log_string);
00058 
00060     void writeLogEvents(pion::net::HTTPResponseWriterPtr& writer);
00061 
00062 private:
00064     static const unsigned int               DEFAULT_MAX_EVENTS;
00065     
00067     unsigned int                            m_max_events;
00068     
00070     unsigned int                            m_num_events;
00071 
00073     std::list<std::string>                  m_log_events;
00074 
00076     boost::mutex                            m_log_mutex;
00077 
00078 #if defined(PION_USE_LOG4CXX)
00079     public:
00080         // member functions inherited from the Appender interface class
00081         virtual void close() {}
00082         virtual bool requiresLayout() const { return false; }
00083     protected:
00085         virtual void append(const log4cxx::spi::LoggingEventPtr& event);
00086         // version 0.10.x adds a second "pool" argument -> just ignore it
00087         virtual void append(const log4cxx::spi::LoggingEventPtr& event,
00088                             log4cxx::helpers::Pool& pool)
00089         {
00090             append(event);
00091         }
00092 #elif defined(PION_USE_LOG4CPLUS)
00093     public:
00094         // member functions inherited from the Appender interface class
00095         virtual void close() {}
00096     protected:
00097         virtual void append(const log4cplus::spi::InternalLoggingEvent& event);
00098     private:
00100         log4cplus::LogLevelManager      m_log_level_manager;
00101 #elif defined(PION_USE_LOG4CPP)
00102     public:
00103         // member functions inherited from the AppenderSkeleton class
00104         virtual void close() {}
00105         virtual bool requiresLayout() const { return true; }
00106         virtual void setLayout(log4cpp::Layout* layout) { m_layout_ptr.reset(layout); }
00107     protected:
00109         virtual void _append(const log4cpp::LoggingEvent& event);
00110     private:
00112         boost::scoped_ptr<log4cpp::Layout>      m_layout_ptr;
00113 #endif
00114 
00115 };
00116 
00117 
00121 class LogService :
00122     public pion::net::WebService
00123 {
00124 public:
00125     // default constructor and destructor
00126     LogService(void);
00127     virtual ~LogService();
00128     
00130     virtual void operator()(pion::net::HTTPRequestPtr& request,
00131                             pion::net::TCPConnectionPtr& tcp_conn);
00132 
00134     inline LogServiceAppender& getLogAppender(void) {
00135 #ifdef PION_HAS_LOG_APPENDER
00136         return dynamic_cast<LogServiceAppender&>(*m_log_appender_ptr);
00137 #else
00138         return *m_log_appender_ptr;
00139 #endif
00140     }
00141     
00142 private:
00144 #ifdef PION_HAS_LOG_APPENDER
00145     PionLogAppenderPtr      m_log_appender_ptr;
00146 #else
00147     LogServiceAppender *    m_log_appender_ptr;
00148 #endif
00149 };
00150 
00151     
00152 }   // end namespace plugins
00153 }   // end namespace pion
00154 
00155 #endif