pion  5.0.6
LogService.hpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_LOGSERVICE_HEADER__
11 #define __PION_LOGSERVICE_HEADER__
12 
13 #include <boost/thread/mutex.hpp>
14 #include <boost/scoped_ptr.hpp>
15 #include <pion/logger.hpp>
16 #include <pion/http/plugin_service.hpp>
17 #include <pion/http/response_writer.hpp>
18 #include <string>
19 #include <list>
20 
21 #if defined(PION_USE_LOG4CXX)
22  #include <log4cxx/appenderskeleton.h>
23  // version 0.10.x introduces a new data type that is declared in a
24  // pool.h header file. If we're using 0.9.x, just declare the type
25  // as an int since it is not being used at all
26  #ifndef _LOG4CXX_HELPERS_POOL_H
27  namespace log4cxx {
28  namespace helpers {
29  typedef int Pool;
30  }
31  }
32  #endif
33 #endif
34 
35 
36 namespace pion { // begin namespace pion
37 namespace plugins { // begin namespace plugins
38 
39 
44 #ifdef PION_HAS_LOG_APPENDER
45  : public log_appender
46 #endif
47 {
48 public:
49  // default constructor and destructor
50  LogServiceAppender(void);
51  virtual ~LogServiceAppender();
52 
54  inline void setMaxEvents(unsigned int n) { m_max_events = n; }
55 
57  void addLogString(const std::string& log_string);
58 
60  void writeLogEvents(const pion::http::response_writer_ptr& writer);
61 
62 private:
64  static const unsigned int DEFAULT_MAX_EVENTS;
65 
67  unsigned int m_max_events;
68 
70  unsigned int m_num_events;
71 
73  std::list<std::string> m_log_events;
74 
76  boost::mutex m_log_mutex;
77 
78 #if defined(PION_USE_LOG4CXX)
79  public:
80  // member functions inherited from the Appender interface class
81  virtual void close() {}
82  virtual bool requiresLayout() const { return false; }
83  protected:
85  virtual void append(const log4cxx::spi::LoggingEventPtr& event);
86  // version 0.10.x adds a second "pool" argument -> just ignore it
87  virtual void append(const log4cxx::spi::LoggingEventPtr& event,
88  log4cxx::helpers::Pool& pool)
89  {
90  append(event);
91  }
92 #elif defined(PION_USE_LOG4CPLUS)
93  public:
94  // member functions inherited from the Appender interface class
95  virtual void close() {}
96  protected:
97  virtual void append(const log4cplus::spi::InternalLoggingEvent& event);
98  private:
100  log4cplus::LogLevelManager m_log_level_manager;
101 #elif defined(PION_USE_LOG4CPP)
102  public:
103  // member functions inherited from the AppenderSkeleton class
104  virtual void close() {}
105  virtual bool requiresLayout() const { return true; }
106  virtual void setLayout(log4cpp::Layout* layout) { m_layout_ptr.reset(layout); }
107  protected:
109  virtual void _append(const log4cpp::LoggingEvent& event);
110  private:
112  boost::scoped_ptr<log4cpp::Layout> m_layout_ptr;
113 #endif
114 
115 };
116 
117 
121 class LogService :
123 {
124 public:
125  // default constructor and destructor
126  LogService(void);
127  virtual ~LogService();
128 
130  virtual void operator()(const pion::http::request_ptr& http_request_ptr,
131  const pion::tcp::connection_ptr& tcp_conn);
132 
135 #ifdef PION_HAS_LOG_APPENDER
136  return dynamic_cast<LogServiceAppender&>(*m_log_appender_ptr);
137 #else
138  return *m_log_appender_ptr;
139 #endif
140  }
141 
142 private:
144 #ifdef PION_HAS_LOG_APPENDER
145  log_appender_ptr m_log_appender_ptr;
146 #else
147  LogServiceAppender * m_log_appender_ptr;
148 #endif
149 };
150 
151 
152 } // end namespace plugins
153 } // end namespace pion
154 
155 #endif
void setMaxEvents(unsigned int n)
sets the maximum number of log events cached in memory
Definition: LogService.hpp:54
LogServiceAppender & getLogAppender(void)
returns the log appender used by LogService
Definition: LogService.hpp:134