# 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     : public PionLogAppender
    00045 {
    00046 public:
    00047     // default constructor and destructor
    00048     LogServiceAppender(void);
    00049     virtual ~LogServiceAppender() {}
    00050
    00052     inline void setMaxEvents(unsigned int n) { m_max_events = n; }
    00053
    00055     void addLogString(const std::string& log_string);
    00056
    00058     void writeLogEvents(pion::net::HTTPResponseWriterPtr& writer);
    00059
    00060 private:
    00062     static const unsigned int               DEFAULT_MAX_EVENTS;
    00063
    00065     unsigned int                            m_max_events;
    00066
    00068     unsigned int                            m_num_events;
    00069
    00071     std::list<std::string>                  m_log_events;
    00072
    00074     boost::mutex                            m_log_mutex;
    00075
    00076 #if defined(PION_USE_LOG4CXX)
    00077     public:
    00078         // member functions inherited from the Appender interface class
    00079         virtual void close() {}
    00080         virtual bool requiresLayout() const { return false; }
    00081     protected:
    00083         virtual void append(const log4cxx::spi::LoggingEventPtr& event);
    00084         // version 0.10.x adds a second "pool" argument -> just ignore it
    00085         virtual void append(const log4cxx::spi::LoggingEventPtr& event,
    00086                             log4cxx::helpers::Pool& pool)
    00087         {
    00088             append(event);
    00089         }
    00090 #elif defined(PION_USE_LOG4CPLUS)
    00091     public:
    00092         // member functions inherited from the Appender interface class
    00093         virtual void close() {}
    00094     protected:
    00095         virtual void append(const log4cplus::spi::InternalLoggingEvent& event);
    00096     private:
    00098         log4cplus::LogLevelManager      m_log_level_manager;
    00099 #elif defined(PION_USE_LOG4CPP)
    00100     public:
    00101         // member functions inherited from the AppenderSkeleton class
    00102         virtual void close() {}
    00103         virtual bool requiresLayout() const { return true; }
    00104         virtual void setLayout(log4cpp::Layout* layout) { m_layout_ptr.reset(layout); }
    00105     protected:
    00107         virtual void _append(const log4cpp::LoggingEvent& event);
    00108     private:
    00110         boost::scoped_ptr<log4cpp::Layout>      m_layout_ptr;
    00111 #endif
    00112
    00113 };
    00114
    00115
    00119 class LogService :
    00120     public pion::net::WebService
    00121 {
    00122 public:
    00123     // default constructor and destructor
    00124     LogService(void);
    00125     virtual ~LogService();
    00126
    00128     virtual void operator()(pion::net::HTTPRequestPtr& request,
    00129                             pion::net::TCPConnectionPtr& tcp_conn);
    00130
    00132     inline LogServiceAppender& getLogAppender(void) {
    00133         return dynamic_cast<LogServiceAppender&>(*m_log_appender_ptr);
    00134     }
    00135
    00136 private:
    00138     PionLogAppenderPtr      m_log_appender_ptr;
    00139 };
    00140
    00141
    00142 }   // end namespace plugins
    00143 }   // end namespace pion
    00144
    00145 #endif