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 #elif defined(PION_USE_LOG4CPLUS)
00034     #include <log4cplus/appender.h>
00035     #include <log4cplus/loglevel.h>
00036 #elif defined(PION_USE_LOG4CPP)
00037     #include <log4cpp/AppenderSkeleton.hh>
00038 #endif
00039 
00040 
00041 namespace pion {        // begin namespace pion
00042 namespace plugins {     // begin namespace plugins
00043 
00044     
00048 class LogServiceAppender
00049     #if defined(PION_USE_LOG4CXX)
00050         : public log4cxx::AppenderSkeleton
00051     #elif defined(PION_USE_LOG4CPLUS)
00052         : public log4cplus::Appender
00053     #elif defined(PION_USE_LOG4CPP)
00054         : public log4cpp::AppenderSkeleton
00055     #endif
00056 {
00057 public:
00058     // default constructor and destructor
00059     LogServiceAppender(void);
00060     virtual ~LogServiceAppender() {}
00061     
00063     inline void setMaxEvents(unsigned int n) { m_max_events = n; }
00064     
00066     void addLogString(const std::string& log_string);
00067 
00069     void writeLogEvents(pion::net::HTTPResponseWriterPtr& writer);
00070 
00071 private:
00073     static const unsigned int               DEFAULT_MAX_EVENTS;
00074     
00076     unsigned int                            m_max_events;
00077     
00079     unsigned int                            m_num_events;
00080 
00082     std::list<std::string>                  m_log_events;
00083 
00085     boost::mutex                            m_log_mutex;
00086 
00087 #if defined(PION_USE_LOG4CXX)
00088     public:
00089         // member functions inherited from the Appender interface class
00090         virtual void close() {}
00091         virtual bool requiresLayout() const { return false; }
00092     protected:
00094         virtual void append(const log4cxx::spi::LoggingEventPtr& event);
00095         // version 0.10.x adds a second "pool" argument -> just ignore it
00096         virtual void append(const log4cxx::spi::LoggingEventPtr& event,
00097                             log4cxx::helpers::Pool& pool)
00098         {
00099             append(event);
00100         }
00101 #elif defined(PION_USE_LOG4CPLUS)
00102     public:
00103         // member functions inherited from the Appender interface class
00104         virtual void close() {}
00105     protected:
00106         virtual void append(const log4cplus::spi::InternalLoggingEvent& event);
00107     private:
00109         log4cplus::LogLevelManager      m_log_level_manager;
00110 #elif defined(PION_USE_LOG4CPP)
00111     public:
00112         // member functions inherited from the AppenderSkeleton class
00113         virtual void close() {}
00114         virtual bool requiresLayout() const { return true; }
00115         virtual void setLayout(log4cpp::Layout* layout) { m_layout_ptr.reset(layout); }
00116     protected:
00118         virtual void _append(const log4cpp::LoggingEvent& event);
00119     private:
00121         boost::scoped_ptr<log4cpp::Layout>      m_layout_ptr;
00122 #endif
00123 
00124 };
00125 
00126 
00130 class LogService :
00131     public pion::net::WebService
00132 {
00133 public:
00134     // default constructor and destructor
00135     LogService(void);
00136     virtual ~LogService();
00137     
00139     virtual void operator()(pion::net::HTTPRequestPtr& request,
00140                             pion::net::TCPConnectionPtr& tcp_conn);
00141 
00143     inline LogServiceAppender& getLogAppender(void) { return *m_log_appender_ptr; }
00144     
00145 private:
00147     LogServiceAppender *    m_log_appender_ptr;
00148 };
00149 
00150     
00151 }   // end namespace plugins
00152 }   // end namespace pion
00153 
00154 #endif

Generated on Fri Apr 30 14:48:53 2010 for pion-net by  doxygen 1.4.7