artdaq_mfextensions  v1_03_02
TRACE_mfPlugin.cc
1 #include "cetlib/PluginTypeDeducer.h"
2 #include "fhiclcpp/ParameterSet.h"
3 #if MESSAGEFACILITY_HEX_VERSION >= 0x20106 // v2_01_06 => cetlib v3_02_00 => new clang support
4 #include "cetlib/ProvideMakePluginMacros.h"
5 #endif
6 
7 #include "messagefacility/MessageService/ELdestination.h"
8 #include "messagefacility/Utilities/ELseverityLevel.h"
9 #include "messagefacility/Utilities/exception.h"
10 #include "cetlib/compiler_macros.h"
11 
12 #define TRACE_NAME "MessageFacility"
13 
14 #if GCC_VERSION >= 701000 || defined(__clang__)
15 #pragma GCC diagnostic push
16 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
17 #endif
18 
19 #include "trace.h"
20 
21 #if GCC_VERSION >= 701000 || defined(__clang__)
22 #pragma GCC diagnostic pop
23 #endif
24 
25 
26 namespace mfplugins
27 {
28  using mf::service::ELdestination;
29  using mf::ELseverityLevel;
30  using mf::ErrorObj;
31 
35  class ELTRACE : public ELdestination
36  {
37 
38 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
39  struct Config
40  {
41  fhicl::TableFragment<ELdestination::Config> elDestConfig;
42  fhicl::Atom<size_t> lvls{ fhicl::Name{ "lvls" },fhicl::Comment{ "TRACE level mask for Slow output" },0 };
43  fhicl::Atom<size_t> lvlm{ fhicl::Name{ "lvlm" },fhicl::Comment{ "TRACE level mask for Memory output" },0 };
44  };
45  using Parameters = fhicl::WrappedTable<Config>;
46 #endif
47 
48  public:
49 
54 #if MESSAGEFACILITY_HEX_VERSION < 0x20103 // v2_01_03 is s58, pre v2_01_03 is s50
55  ELTRACE(const fhicl::ParameterSet& pset);
56 #else
57  ELTRACE(Parameters const& pset);
58 #endif
59 
65  virtual void fillPrefix(std::ostringstream& o, const ErrorObj& e) override;
66 
72  virtual void fillUsrMsg(std::ostringstream& o, const ErrorObj& e) override;
73 
77  virtual void fillSuffix(std::ostringstream&, const ErrorObj&) override {}
78 
84  virtual void routePayload(const std::ostringstream& o, const ErrorObj& e) override;
85 
86  };
87 
88  // END DECLARATION
89  //======================================================================
90  // BEGIN IMPLEMENTATION
91 
92 
93  //======================================================================
94  // ELTRACE c'tor
95  //======================================================================
96 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
97  ELTRACE::ELTRACE(const fhicl::ParameterSet& pset)
98  : ELdestination(pset)
99  {
100  size_t msk;
101 
102  if (pset.get_if_present<size_t>("lvls", msk))
103  TRACE_CNTL("lvlmskS", msk); // the S mask for TRACE_NAME
104 
105  if (pset.get_if_present<size_t>("lvlm", msk))
106  TRACE_CNTL("lvlmskM", msk); // the M mask for TRACE_NAME
107 
108  TLOG(TLVL_INFO) << "ELTRACE MessageLogger destination plugin initialized.";
109  }
110 #else
111  ELTRACE::ELTRACE(Parameters const& pset)
112  : ELdestination(pset().elDestConfig())
113  {
114  size_t msk;
115 
116  if (pset().lvls() != 0)
117  {
118  msk = pset().lvls();
119  TRACE_CNTL("lvlmskS", msk); // the S mask for TRACE_NAME
120  }
121  if (pset().lvlm() != 0)
122  {
123  msk = pset().lvlm();
124  TRACE_CNTL("lvlmskM", msk); // the M mask for TRACE_NAME
125  }
126 
127  TLOG(TLVL_INFO) << "ELTRACE MessageLogger destination plugin initialized.";
128  }
129 
130 #endif
131 
132  //======================================================================
133  // Message prefix filler ( overriddes ELdestination::fillPrefix )
134  //======================================================================
135  void ELTRACE::fillPrefix(std::ostringstream& oss, const ErrorObj& msg)
136  {
137  const auto& xid = msg.xid();
138 
139  oss << xid.application() << ", "; // application
140  oss << xid.id() << ": "; // category
141  // oss << mf::MessageDrop::instance()->runEvent + ELstring(" "); // run/event no
142  // oss << xid.module+ELstring(": "); // module name
143  }
144 
145  //======================================================================
146  // Message filler ( overriddes ELdestination::fillUsrMsg )
147  //======================================================================
148  void ELTRACE::fillUsrMsg(std::ostringstream& oss, const ErrorObj& msg)
149  {
150  std::ostringstream tmposs;
151  ELdestination::fillUsrMsg(tmposs, msg);
152 
153  // remove leading "\n" if present
154  const std::string& usrMsg = !tmposs.str().compare(0, 1, "\n") ? tmposs.str().erase(0, 1) : tmposs.str();
155 
156  oss << usrMsg;
157  }
158 
159  //======================================================================
160  // Message router ( overriddes ELdestination::routePayload )
161  //======================================================================
162  void ELTRACE::routePayload(const std::ostringstream& oss, const ErrorObj& msg)
163  {
164  const auto& xid = msg.xid();
165  auto message = oss.str();
166 
167  auto level = xid.severity().getLevel();
168  auto lvlNum = 0;
169 
170  switch (level)
171  {
172  case mf::ELseverityLevel::ELsev_success:
173  case mf::ELseverityLevel::ELsev_zeroSeverity:
174  case mf::ELseverityLevel::ELsev_unspecified:
175  lvlNum = 3;
176  break;
177 
178  case mf::ELseverityLevel::ELsev_info:
179  lvlNum = 2;
180  break;
181 
182  case mf::ELseverityLevel::ELsev_warning:
183  lvlNum = 1;
184  break;
185  }
186  TRACE(lvlNum, message); // this is the TRACE -- direct the message to memory and/or stdout
187  }
188 } // end namespace mfplugins
189 
190 //======================================================================
191 //
192 // makePlugin function
193 //
194 //======================================================================
195 
196 #ifndef EXTERN_C_FUNC_DECLARE_START
197 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
198 #endif
199 
200 EXTERN_C_FUNC_DECLARE_START
201 auto makePlugin(const std::string&,
202  const fhicl::ParameterSet& pset)
203 {
204  return std::make_unique<mfplugins::ELTRACE>(pset);
205 }
206  }
207 
208 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
virtual void routePayload(const std::ostringstream &o, const ErrorObj &e) override
Serialize a MessageFacility message to the output.
virtual void fillUsrMsg(std::ostringstream &o, const ErrorObj &e) override
Fill the &quot;User Message&quot; portion of the message.
Message Facility destination which logs messages to a TRACE buffer
ELTRACE(const fhicl::ParameterSet &pset)
ELTRACE Constructor
virtual void fillPrefix(std::ostringstream &o, const ErrorObj &e) override
Fill the &quot;Prefix&quot; portion of the message.
virtual void fillSuffix(std::ostringstream &, const ErrorObj &) override
Fill the &quot;Suffix&quot; portion of the message (Unused)