10 #include "LogService.hpp" 12 #if defined(PION_USE_LOG4CXX) 13 #include <log4cxx/spi/loggingevent.h> 14 #include <boost/lexical_cast.hpp> 15 #elif defined(PION_USE_LOG4CPLUS) 16 #include <log4cplus/spi/loggingevent.h> 17 #include <boost/lexical_cast.hpp> 18 #elif defined(PION_USE_LOG4CPP) 19 #include <log4cpp/BasicLayout.hh> 22 #include <pion/net/HTTPResponseWriter.hpp> 33 const unsigned int LogServiceAppender::DEFAULT_MAX_EVENTS = 25;
38 #if defined(PION_USE_LOG4CPP) 39 LogServiceAppender::LogServiceAppender(
void)
40 : log4cpp::AppenderSkeleton(
"LogServiceAppender"),
41 m_max_events(DEFAULT_MAX_EVENTS), m_num_events(0),
42 m_layout_ptr(new log4cpp::BasicLayout())
45 LogServiceAppender::LogServiceAppender(
void)
46 : m_max_events(DEFAULT_MAX_EVENTS), m_num_events(0)
51 #if defined(PION_USE_LOG4CXX) 52 void LogServiceAppender::append(
const log4cxx::spi::LoggingEventPtr& event)
55 std::string formatted_string(boost::lexical_cast<std::string>(event->getTimeStamp()));
56 formatted_string +=
' ';
57 formatted_string +=
event->getLevel()->toString();
58 formatted_string +=
' ';
59 formatted_string +=
event->getLoggerName();
60 formatted_string +=
" - ";
61 formatted_string +=
event->getRenderedMessage();
62 formatted_string +=
'\n';
63 addLogString(formatted_string);
65 #elif defined(PION_USE_LOG4CPLUS) 66 void LogServiceAppender::append(
const log4cplus::spi::InternalLoggingEvent& event)
69 std::string formatted_string(boost::lexical_cast<std::string>(event.getTimestamp().sec()));
70 formatted_string +=
' ';
71 formatted_string += m_log_level_manager.toString(event.getLogLevel());
72 formatted_string +=
' ';
73 formatted_string +=
event.getLoggerName();
74 formatted_string +=
" - ";
75 formatted_string +=
event.getMessage();
76 formatted_string +=
'\n';
77 addLogString(formatted_string);
79 #elif defined(PION_USE_LOG4CPP) 80 void LogServiceAppender::_append(
const log4cpp::LoggingEvent& event)
82 std::string formatted_string(m_layout_ptr->format(event));
83 addLogString(formatted_string);
89 boost::mutex::scoped_lock log_lock(m_log_mutex);
90 m_log_events.push_back(log_string);
92 while (m_num_events > m_max_events) {
93 m_log_events.erase(m_log_events.begin());
100 #if defined(PION_USE_LOG4CXX) || defined(PION_USE_LOG4CPLUS) || defined(PION_USE_LOG4CPP) 101 boost::mutex::scoped_lock log_lock(m_log_mutex);
102 for (std::list<std::string>::const_iterator i = m_log_events.begin();
103 i != m_log_events.end(); ++i)
107 #elif defined(PION_DISABLE_LOGGING) 108 writer <<
"Logging is disabled." << HTTPTypes::STRING_CRLF;
110 writer <<
"Using ostream logging." << HTTPTypes::STRING_CRLF;
117 LogService::LogService(
void)
120 #if defined(PION_USE_LOG4CXX) 121 m_log_appender_ptr->setName(
"LogServiceAppender");
122 log4cxx::Logger::getRootLogger()->addAppender(m_log_appender_ptr);
123 #elif defined(PION_USE_LOG4CPLUS) 124 m_log_appender_ptr->setName(
"LogServiceAppender");
125 log4cplus::Logger::getRoot().addAppender(m_log_appender_ptr);
126 #elif defined(PION_USE_LOG4CPP) 127 log4cpp::Category::getRoot().addAppender(m_log_appender_ptr);
131 LogService::~LogService()
133 #if defined(PION_USE_LOG4CXX) 135 log4cxx::Logger::getRootLogger()->removeAppender(m_log_appender_ptr);
136 #elif defined(PION_USE_LOG4CPLUS) 138 log4cplus::Logger::getRoot().removeAppender(
"LogServiceAppender");
139 #elif defined(PION_USE_LOG4CPP) 141 log4cpp::Category::getRoot().removeAppender(m_log_appender_ptr);
143 delete m_log_appender_ptr;
151 HTTPResponseWriterPtr writer(HTTPResponseWriter::create(tcp_conn, *request,
152 boost::bind(&TCPConnection::finish, tcp_conn)));
153 writer->getResponse().setContentType(HTTPTypes::CONTENT_TYPE_TEXT);
154 getLogAppender().writeLogEvents(writer);
virtual void operator()(pion::net::HTTPRequestPtr &request, pion::net::TCPConnectionPtr &tcp_conn)
handles a new HTTP request
the following enables use of the lock-free cache
void writeLogEvents(pion::net::HTTPResponseWriterPtr &writer)
writes the events cached in memory to a response stream
void addLogString(const std::string &log_string)
adds a formatted log message to the memory cache