artdaq_mfextensions  v1_03_06
TRACE_mfPlugin.cc
1 #include "cetlib/PluginTypeDeducer.h"
2 #include "cetlib/ProvideMakePluginMacros.h"
3 #include "fhiclcpp/ParameterSet.h"
4 
5 #include "cetlib/compiler_macros.h"
6 #include "messagefacility/MessageService/ELdestination.h"
7 #include "messagefacility/Utilities/ELseverityLevel.h"
8 #include "messagefacility/Utilities/exception.h"
9 
10 #define TRACE_NAME "MessageFacility"
11 
12 #if GCC_VERSION >= 701000 || defined(__clang__)
13 #pragma GCC diagnostic push
14 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
15 #endif
16 
17 #include "trace.h"
18 
19 #if GCC_VERSION >= 701000 || defined(__clang__)
20 #pragma GCC diagnostic pop
21 #endif
22 
23 namespace mfplugins {
24 using mf::ELseverityLevel;
25 using mf::ErrorObj;
26 using mf::service::ELdestination;
27 
31 class ELTRACE : public ELdestination {
32  public:
36  struct Config {
38  fhicl::TableFragment<ELdestination::Config> elDestConfig;
40  fhicl::Atom<size_t> lvls =
41  fhicl::Atom<size_t>{fhicl::Name{"lvls"}, fhicl::Comment{"TRACE level mask for Slow output"}, 0};
43  fhicl::Atom<size_t> lvlm =
44  fhicl::Atom<size_t>{fhicl::Name{"lvlm"}, fhicl::Comment{"TRACE level mask for Memory output"}, 0};
45  };
47  using Parameters = fhicl::WrappedTable<Config>;
48 
49  public:
54  ELTRACE(Parameters const& pset);
55 
61  virtual void fillPrefix(std::ostringstream& o, const ErrorObj& e) override;
62 
68  virtual void fillUsrMsg(std::ostringstream& o, const ErrorObj& e) override;
69 
73  virtual void fillSuffix(std::ostringstream&, const ErrorObj&) override {}
74 
80  virtual void routePayload(const std::ostringstream& o, const ErrorObj& e) override;
81 };
82 
83 // END DECLARATION
84 //======================================================================
85 // BEGIN IMPLEMENTATION
86 
87 //======================================================================
88 // ELTRACE c'tor
89 //======================================================================
90 ELTRACE::ELTRACE(Parameters const& pset) : ELdestination(pset().elDestConfig()) {
91  size_t msk;
92 
93  if (pset().lvls() != 0) {
94  msk = pset().lvls();
95  TRACE_CNTL("lvlmskS", msk); // the S mask for TRACE_NAME
96  }
97  if (pset().lvlm() != 0) {
98  msk = pset().lvlm();
99  TRACE_CNTL("lvlmskM", msk); // the M mask for TRACE_NAME
100  }
101 
102  TLOG(TLVL_INFO) << "ELTRACE MessageLogger destination plugin initialized.";
103 }
104 
105 //======================================================================
106 // Message prefix filler ( overriddes ELdestination::fillPrefix )
107 //======================================================================
108 void ELTRACE::fillPrefix(std::ostringstream& oss, const ErrorObj& msg) {
109  const auto& xid = msg.xid();
110 
111  oss << xid.application() << ", "; // application
112  oss << xid.id() << ": "; // category
113  // oss << mf::MessageDrop::instance()->runEvent + ELstring(" "); // run/event no
114  // oss << xid.module+ELstring(": "); // module name
115 }
116 
117 //======================================================================
118 // Message filler ( overriddes ELdestination::fillUsrMsg )
119 //======================================================================
120 void ELTRACE::fillUsrMsg(std::ostringstream& oss, const ErrorObj& msg) {
121  std::ostringstream tmposs;
122  ELdestination::fillUsrMsg(tmposs, msg);
123 
124  // remove leading "\n" if present
125  const std::string& usrMsg = !tmposs.str().compare(0, 1, "\n") ? tmposs.str().erase(0, 1) : tmposs.str();
126 
127  oss << usrMsg;
128 }
129 
130 //======================================================================
131 // Message router ( overriddes ELdestination::routePayload )
132 //======================================================================
133 void ELTRACE::routePayload(const std::ostringstream& oss, const ErrorObj& msg) {
134  const auto& xid = msg.xid();
135  auto message = oss.str();
136 
137  auto level = xid.severity().getLevel();
138  auto lvlNum = 0;
139 
140  switch (level) {
141  case mf::ELseverityLevel::ELsev_success:
142  case mf::ELseverityLevel::ELsev_zeroSeverity:
143  case mf::ELseverityLevel::ELsev_unspecified:
144  lvlNum = 3;
145  break;
146 
147  case mf::ELseverityLevel::ELsev_info:
148  lvlNum = 2;
149  break;
150 
151  case mf::ELseverityLevel::ELsev_warning:
152  lvlNum = 1;
153  break;
154  }
155  TRACE(lvlNum, message); // this is the TRACE -- direct the message to memory and/or stdout
156 }
157 } // end namespace mfplugins
158 
159 //======================================================================
160 //
161 // makePlugin function
162 //
163 //======================================================================
164 
165 #ifndef EXTERN_C_FUNC_DECLARE_START
166 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
167 #endif
168 
169 EXTERN_C_FUNC_DECLARE_START
170 auto makePlugin(const std::string&, const fhicl::ParameterSet& pset) {
171  return std::make_unique<mfplugins::ELTRACE>(pset);
172 }
173 }
174 
175 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
fhicl::TableFragment< ELdestination::Config > elDestConfig
ELDestination common parameters.
virtual void fillPrefix(std::ostringstream &o, const ErrorObj &e) override
Fill the &quot;Prefix&quot; portion of the message.
ELTRACE(Parameters const &pset)
ELTRACE Constructor
virtual void fillSuffix(std::ostringstream &, const ErrorObj &) override
Fill the &quot;Suffix&quot; portion of the message (Unused)
Configuration Parameters for ELTRACE.
fhicl::Atom< size_t > lvlm
&quot;lvlm&quot; (Default: 0): TRACE level mask for Memory output
fhicl::Atom< size_t > lvls
&quot;lvls&quot; (Default: 0): TRACE level mask for Slow output
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation.