SyslogAppender.cpp
Go to the documentation of this file.
1 /*
2  * SyslogAppender.cpp
3  *
4  * Copyright 2000, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
5  * Copyright 2000, Bastiaan Bakker. All rights reserved.
6  *
7  * See the COPYING file for the terms of usage and distribution.
8  */
9 
10 #include "PortabilityImpl.hh"
11 #ifdef LOG4CPP_HAVE_SYSLOG
12 
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
18 #include <log4cpp/FactoryParams.hh>
19 #include <memory>
20 
21 namespace log4cpp {
22 
24  static int priorities[8] = { LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
26  LOG_DEBUG };
27  int result;
28 
29  priority++;
30  priority /= 100;
31 
32  if (priority < 0) {
33  result = LOG_EMERG;
34  } else if (priority > 7) {
35  result = LOG_DEBUG;
36  } else {
37  result = priorities[priority];
38  }
39 
40  return result;
41  }
42 
43 
44  SyslogAppender::SyslogAppender(const std::string& name,
45  const std::string& syslogName,
46  int facility) :
47  LayoutAppender(name),
48  _syslogName(syslogName),
49  _facility(facility)
50  {
51  open();
52  }
53 
55  close();
56  }
57 
58  void SyslogAppender::open() {
59  openlog(_syslogName.c_str(), 0, _facility);
60  }
61 
62  void SyslogAppender::close() {
63  ::closelog();
64  }
65 
66  void SyslogAppender::_append(const LoggingEvent& event) {
67  std::string message(_getLayout().format(event));
68  int priority = toSyslogPriority(event.priority);
69  ::syslog(priority | _facility, "%s", message.c_str());
70  }
71 
72  bool SyslogAppender::reopen() {
73  close();
74  open();
75  return true;
76  }
77 
78  std::auto_ptr<Appender> create_syslog_appender(const FactoryParams& params)
79  {
80  std::string name, syslog_name;
81  int facility = 0;
82  params.get_for("syslog appender").required("name", name)("syslog_name", syslog_name)
83  .optional("facility", facility);
84  return std::auto_ptr<Appender>(new SyslogAppender(name, syslog_name, facility));
85  }
86 }
87 
88 #endif // LOG4CPP_HAVE_SYSLOG
static int toSyslogPriority(Priority::Value priority)
system is unusable
error conditions
informational
SyslogAppender(const std::string &name, const std::string &syslogName, int facility=LOG_USER)
debug-level messages
warning conditions
std::auto_ptr< Appender > create_syslog_appender(const FactoryParams &)
virtual bool reopen()
normal but significant condition
critical conditions
action must be taken immediately
virtual void _append(const LoggingEvent &event)
const std::string _syslogName
virtual void close()
LayoutAppender(const std::string &name)


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Jun 23 2019 19:10:00