artdaq_mfextensions  v1_02_00
TRACE_mfPlugin.cc
1 #include "cetlib/PluginTypeDeducer.h"
2 #include "fhiclcpp/ParameterSet.h"
3 
4 #include "messagefacility/MessageService/ELdestination.h"
5 #include "messagefacility/Utilities/ELseverityLevel.h"
6 #if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
7 # include "messagefacility/MessageService/ELcontextSupplier.h"
8 # include "messagefacility/MessageLogger/MessageDrop.h"
9 #endif
10 #include "messagefacility/Utilities/exception.h"
11 
12 #define TRACE_NAME "MessageFacility"
13 #include "trace.h"
14 
15 
16 namespace mfplugins
17 {
18  using mf::service::ELdestination;
19  using mf::ELseverityLevel;
20 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
21  using mf::service::ELcontextSupplier;
22 # endif
23  using mf::ErrorObj;
24 
28  class ELTRACE : public ELdestination
29  {
30  public:
31 
32  ELTRACE(const fhicl::ParameterSet& pset);
33 
34  virtual void fillPrefix(std::ostringstream&, const ErrorObj&
35 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
36  , const ELcontextSupplier&
37 # endif
38  ) override;
39 
40  virtual void fillUsrMsg(std::ostringstream&, const ErrorObj&) override;
41 
42  virtual void fillSuffix(std::ostringstream&, const ErrorObj&) override {}
43 
44  virtual void routePayload(const std::ostringstream&, const ErrorObj&
45 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
46  , const ELcontextSupplier&
47 # endif
48  ) override;
49 
50  private:
51  int consecutive_success_count_;
52  int error_count_;
53  int next_error_report_;
54  int error_report_backoff_factor_;
55  };
56 
57  // END DECLARATION
58  //======================================================================
59  // BEGIN IMPLEMENTATION
60 
61 
62  //======================================================================
63  // ELTRACE c'tor
64  //======================================================================
65 
66  ELTRACE::ELTRACE(const fhicl::ParameterSet& pset)
67  : ELdestination(pset)
68  , consecutive_success_count_(0)
69  , error_count_(0)
70  , next_error_report_(1)
71  , error_report_backoff_factor_()
72  {
73  size_t msk;
74 
75  if (pset.get_if_present<size_t>("lvls",msk))
76  TRACE_CNTL("lvlmskS",msk); // the S mask for TRACE_NAME
77 
78  if (pset.get_if_present<size_t>("lvlm",msk))
79  TRACE_CNTL("lvlmskM",msk); // the M mask for TRACE_NAME
80 
81  error_report_backoff_factor_ = pset.get<int>("error_report_backoff_factor", 10);
82  TRACE(3, "ELTRACE MessageLogger destination plugin initialized.");
83  }
84 
85  //======================================================================
86  // Message prefix filler ( overriddes ELdestination::fillPrefix )
87  //======================================================================
88  void ELTRACE::fillPrefix(std::ostringstream& oss, const ErrorObj& msg
89 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
90  , ELcontextSupplier const&
91 # endif
92  )
93  {
94  const auto& xid = msg.xid();
95 
96 # if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
97  oss << xid.application() << ", "; // application
98  oss << xid.id() << ": "; // category
99 # else
100  oss << xid.application << ", "; // application
101  oss << xid.id << ": "; // category
102 # endif
103  // oss << mf::MessageDrop::instance()->runEvent + ELstring(" "); // run/event no
104  // oss << xid.module+ELstring(": "); // module name
105  }
106 
107  //======================================================================
108  // Message filler ( overriddes ELdestination::fillUsrMsg )
109  //======================================================================
110  void ELTRACE::fillUsrMsg(std::ostringstream& oss, const ErrorObj& msg)
111  {
112  std::ostringstream tmposs;
113  ELdestination::fillUsrMsg(tmposs, msg);
114 
115  // remove leading "\n" if present
116  const std::string& usrMsg = !tmposs.str().compare(0, 1, "\n") ? tmposs.str().erase(0, 1) : tmposs.str();
117 
118  oss << usrMsg;
119  }
120 
121  //======================================================================
122  // Message router ( overriddes ELdestination::routePayload )
123  //======================================================================
124  void ELTRACE::routePayload(const std::ostringstream& oss, const ErrorObj& msg
125 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
126  , ELcontextSupplier const&
127 # endif
128  )
129  {
130  const auto& xid = msg.xid();
131  auto message = oss.str();
132 
133 # if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
134  auto level = xid.severity().getLevel();
135 # else
136  auto level = xid.severity.getLevel();
137 # endif
138  auto lvlNum = 0;
139 
140  switch (level)
141  {
142 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
143  case mf::ELseverityLevel::ELsev_incidental:
144 # endif
145  case mf::ELseverityLevel::ELsev_success:
146  case mf::ELseverityLevel::ELsev_zeroSeverity:
147  case mf::ELseverityLevel::ELsev_unspecified:
148  lvlNum = 3;
149  break;
150 
151  case mf::ELseverityLevel::ELsev_info:
152  lvlNum = 2;
153  break;
154 
155  case mf::ELseverityLevel::ELsev_warning:
156 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
157  case mf::ELseverityLevel::ELsev_warning2:
158 # endif
159  lvlNum = 1;
160  break;
161  }
162  TRACE(lvlNum, message); // this is the TRACE -- direct the message to memory and/or stdout
163  }
164  } // end namespace mfplugins
165 
166  //======================================================================
167  //
168  // makePlugin function
169  //
170  //======================================================================
171 
172  extern "C"
173  {
174  auto makePlugin(const std::string&,
175  const fhicl::ParameterSet& pset)
176  {
177  return std::make_unique<mfplugins::ELTRACE>(pset);
178  }
179  }
180 
181  DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
Message Facility destination which logs messages to a TRACE buffer