SyslogAppender.cpp
Go to the documentation of this file.
00001 /*
00002  * SyslogAppender.cpp
00003  *
00004  * Copyright 2000, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
00005  * Copyright 2000, Bastiaan Bakker. All rights reserved.
00006  *
00007  * See the COPYING file for the terms of usage and distribution.
00008  */
00009 
00010 #include "PortabilityImpl.hh"
00011 #ifdef LOG4CPP_HAVE_SYSLOG
00012 
00013 #include <unistd.h>
00014 #include <sys/types.h>
00015 #include <sys/stat.h>
00016 #include <fcntl.h>
00017 #include <log4cpp/SyslogAppender.hh>
00018 #include <log4cpp/FactoryParams.hh>
00019 #include <memory>
00020 
00021 namespace log4cpp {
00022 
00023     int SyslogAppender::toSyslogPriority(Priority::Value priority) {
00024         static int priorities[8] = { LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
00025                                      LOG_WARNING, LOG_NOTICE, LOG_INFO, 
00026                                      LOG_DEBUG };
00027         int result;
00028 
00029         priority++;
00030         priority /= 100;
00031 
00032         if (priority < 0) {
00033             result = LOG_EMERG;
00034         } else if (priority > 7) {
00035             result = LOG_DEBUG;
00036         } else {
00037             result = priorities[priority];
00038         }
00039 
00040         return result;
00041     }
00042         
00043 
00044     SyslogAppender::SyslogAppender(const std::string& name, 
00045                                    const std::string& syslogName, 
00046                                    int facility) : 
00047         LayoutAppender(name),
00048         _syslogName(syslogName),
00049         _facility(facility) 
00050     {
00051         open();
00052     }
00053     
00054     SyslogAppender::~SyslogAppender() {
00055         close();
00056     }
00057 
00058     void SyslogAppender::open() {
00059         openlog(_syslogName.c_str(), 0, _facility);
00060     }
00061 
00062     void SyslogAppender::close() {
00063         ::closelog();
00064     }
00065 
00066     void SyslogAppender::_append(const LoggingEvent& event) {
00067         std::string message(_getLayout().format(event));
00068         int priority = toSyslogPriority(event.priority);
00069         ::syslog(priority | _facility, "%s", message.c_str());
00070     }
00071 
00072     bool SyslogAppender::reopen() {
00073         close();
00074         open();
00075         return true;
00076     }
00077     
00078     std::auto_ptr<Appender> create_syslog_appender(const FactoryParams& params)
00079     {
00080        std::string name, syslog_name;
00081        int facility = 0;
00082        params.get_for("syslog appender").required("name", name)("syslog_name", syslog_name)
00083                                         .optional("facility", facility);
00084        return std::auto_ptr<Appender>(new SyslogAppender(name, syslog_name, facility));
00085     }
00086 }
00087 
00088 #endif // LOG4CPP_HAVE_SYSLOG


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung.
autogenerated on Thu Jan 2 2014 11:35:02