syslog_mfPlugin.cc
Go to the documentation of this file.
1 // vim: set sw=2 expandtab :
2 
4 
12 
13 #include <iostream>
14 #include <memory>
15 #include <string>
16 
17 #include <syslog.h>
18 
19 using namespace std;
20 using namespace std::string_literals;
21 
22 namespace mfplugins {
23 
24  using mf::ELseverityLevel;
25  using mf::ErrorObj;
27 
28  class ELsyslog : public ELdestination {
29 
30  public:
31  struct Config {
33  };
34 
36 
37  public:
38  ELsyslog(Parameters const& pset);
39 
40  private:
41  void fillPrefix(ostringstream&, ErrorObj const&) override;
42  void fillUsrMsg(ostringstream&, ErrorObj const&) override;
43  void fillSuffix(ostringstream&, ErrorObj const&) override;
44  void routePayload(ostringstream const&, ErrorObj const&) override;
45  int syslogLevel(ELseverityLevel);
46  };
47 
48  ELsyslog::ELsyslog(Parameters const& pset)
49  : ELdestination{pset().elDestConfig()}
50  {
51  openlog("MF", 0, LOG_LOCAL0);
52  }
53 
54  void
55  ELsyslog::fillPrefix(ostringstream& oss, ErrorObj const& msg)
56  {
57  auto const& xid = msg.xid();
58  oss << format_.timestamp(msg.timestamp()) << '|' << xid.hostname() << '|'
59  << xid.hostaddr() << '|' << xid.severity().getName() << '|' << xid.id()
60  << '|' << xid.application() << '|' << xid.pid() << '|'
61  << mf::GetIteration() << '|' << xid.module() << '|';
62  }
63 
64  void
65  ELsyslog::fillUsrMsg(ostringstream& oss, ErrorObj const& msg)
66  {
67  ostringstream buf;
68  ELdestination::fillUsrMsg(buf, msg);
69  string const& usrMsg =
70  !buf.str().compare(0, 1, "\n") ? buf.str().erase(0, 1) : buf.str();
71  oss << usrMsg;
72  }
73 
74  void
75  ELsyslog::fillSuffix(ostringstream&, ErrorObj const&)
76  {}
77 
78  void
79  ELsyslog::routePayload(ostringstream const& oss, ErrorObj const& msg)
80  {
81  int const severity = syslogLevel(msg.xid().severity());
82  syslog(severity, "%s", oss.str().data());
83  }
84 
85  int
87  {
88  // Following syslog levels not used:
89  // LOG_EMERG ; //0
90  // LOG_ALERT ; //1
91  // LOG_NOTICE ; //5
92  switch (severity.getLevel()) { // Used by:
94  return LOG_CRIT; // LogAbsolute, LogSystem
96  return LOG_ERR; // LogError, LogProblem
98  return LOG_WARNING; // LogPrint, LogWarning
100  return LOG_INFO; // LogInfo, LogVerbatim
102  return LOG_DEBUG; // LogDebug, LogTrace
103  default:
105  << "ELseverityLevel: " << severity
106  << " not currently supported for syslog destination\n";
107  }
108  return -1;
109  }
110 
111 } // namespace mfplugins
112 
114 {
115  return make_unique<mfplugins::ELsyslog>(pset);
116 }
118 
#define LOG_DEBUG(stream)
Definition: Messenger.h:147
ELseverityLevel severity() const
Definition: ELextendedID.cc:33
virtual void fillUsrMsg(std::ostringstream &, mf::ErrorObj const &msg)
fhicl::TableFragment< ELdestination::Config > elDestConfig
void msg(const char *fmt,...)
Definition: message.cpp:107
timeval timestamp() const
Definition: ErrorObj.cc:73
STL namespace.
void routePayload(ostringstream const &, ErrorObj const &) override
void fillUsrMsg(ostringstream &, ErrorObj const &) override
void fillPrefix(ostringstream &, ErrorObj const &) override
#define LOG_CRIT(stream)
Definition: Messenger.h:122
void fillSuffix(ostringstream &, ErrorObj const &) override
cet::coded_exception< errors::error, detail::translate > Exception
Definition: exception.h:16
ELextendedID const & xid() const
Definition: ErrorObj.cc:61
#define MAKE_PLUGIN_END
MAKE_MFPLUGIN_START(, pset)
string const & GetIteration()
int syslogLevel(ELseverityLevel)
#define FHICL_PROVIDE_ALLOWED_CONFIGURATION(klass)
MsgFormatSettings format_
#define LOG_INFO(stream)
Definition: Messenger.h:142