LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
syslog_mfPlugin.cc
Go to the documentation of this file.
1 // vim: set sw=2 expandtab :
2 
3 #include "cetlib/PluginTypeDeducer.h"
4 #include "cetlib/ProvideFilePathMacro.h"
5 #include "cetlib/ProvideMakePluginMacros.h"
14 
15 #include <iostream>
16 #include <memory>
17 #include <string>
18 
19 #include <syslog.h>
20 
21 using namespace std;
22 using namespace std::string_literals;
23 
24 namespace mfplugins {
25 
26  using mf::ELseverityLevel;
27  using mf::ErrorObj;
29 
30  class ELsyslog : public ELdestination {
31 
32  public:
33  struct Config {
35  };
36 
38 
39  public:
40  ELsyslog(Parameters const& pset);
41 
42  private:
43  void fillPrefix(ostringstream&, ErrorObj const&) override;
44  void fillUsrMsg(ostringstream&, ErrorObj const&) override;
45  void fillSuffix(ostringstream&, ErrorObj const&) override;
46  void routePayload(ostringstream const&, ErrorObj const&) override;
47  int syslogLevel(ELseverityLevel);
48  };
49 
50  ELsyslog::ELsyslog(Parameters const& pset)
51  : ELdestination{pset().elDestConfig()}
52  {
53  openlog("MF", 0, LOG_LOCAL0);
54  }
55 
56  void
57  ELsyslog::fillPrefix(ostringstream& oss, ErrorObj const& msg)
58  {
59  auto const& xid = msg.xid();
60  oss << format_.timestamp(msg.timestamp()) << '|' << xid.hostname() << '|'
61  << xid.hostaddr() << '|' << xid.severity().getName() << '|' << xid.id()
62  << '|' << xid.application() << '|' << xid.pid() << '|'
63  << mf::GetIteration() << '|' << xid.module() << '|';
64  }
65 
66  void
67  ELsyslog::fillUsrMsg(ostringstream& oss, ErrorObj const& msg)
68  {
69  ostringstream buf;
70  ELdestination::fillUsrMsg(buf, msg);
71  string const& usrMsg =
72  !buf.str().compare(0, 1, "\n") ? buf.str().erase(0, 1) : buf.str();
73  oss << usrMsg;
74  }
75 
76  void
77  ELsyslog::fillSuffix(ostringstream&, ErrorObj const&)
78  {}
79 
80  void
81  ELsyslog::routePayload(ostringstream const& oss, ErrorObj const& msg)
82  {
83  int const severity = syslogLevel(msg.xid().severity());
84  syslog(severity, "%s", oss.str().data());
85  }
86 
87  int
89  {
90  // Following syslog levels not used:
91  // LOG_EMERG ; //0
92  // LOG_ALERT ; //1
93  // LOG_NOTICE ; //5
94  switch (severity.getLevel()) { // Used by:
96  return LOG_CRIT; // LogAbsolute, LogSystem
98  return LOG_ERR; // LogError, LogProblem
100  return LOG_WARNING; // LogPrint, LogWarning
102  return LOG_INFO; // LogInfo, LogVerbatim
104  return LOG_DEBUG; // LogDebug, LogTrace
105  default:
107  << "ELseverityLevel: " << severity
108  << " not currently supported for syslog destination\n";
109  }
110  return -1;
111  }
112 
113 } // namespace mfplugins
114 
115 MAKE_PLUGIN_START(auto, string const&, fhicl::ParameterSet const& pset)
116 {
117  return make_unique<mfplugins::ELsyslog>(pset);
118 }
119 MAKE_PLUGIN_END
120 
121 CET_PROVIDE_FILE_PATH()
122 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
#define LOG_INFO(category)
ELseverityLevel severity() const
Definition: ELextendedID.cc:33
virtual void fillUsrMsg(std::ostringstream &, mf::ErrorObj const &msg)
fhicl::TableFragment< ELdestination::Config > elDestConfig
timeval timestamp() const
Definition: ErrorObj.cc:73
MAKE_PLUGIN_START(auto, string const &, fhicl::ParameterSet const &pset)
STL namespace.
void routePayload(ostringstream const &, ErrorObj const &) override
void fillUsrMsg(ostringstream &, ErrorObj const &) override
void fillPrefix(ostringstream &, ErrorObj const &) override
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 LOG_WARNING(category)
string const & GetIteration()
int syslogLevel(ELseverityLevel)
#define FHICL_PROVIDE_ALLOWED_CONFIGURATION(klass)
#define LOG_DEBUG(id)
MsgFormatSettings format_