artdaq_mfextensions  v1_03_05
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:
33 
37  struct Config {
39  fhicl::TableFragment<ELdestination::Config> elDestConfig;
41  fhicl::Atom<size_t> lvls =
42  fhicl::Atom<size_t>{fhicl::Name{"lvls"}, fhicl::Comment{"TRACE level mask for Slow output"}, 0};
44  fhicl::Atom<size_t> lvlm =
45  fhicl::Atom<size_t>{fhicl::Name{"lvlm"}, fhicl::Comment{"TRACE level mask for Memory output"}, 0};
46  };
48  using Parameters = fhicl::WrappedTable<Config>;
49 
50  public:
55  ELTRACE(Parameters const& pset);
56 
62  virtual void fillPrefix(std::ostringstream& o, const ErrorObj& e) override;
63 
69  virtual void fillUsrMsg(std::ostringstream& o, const ErrorObj& e) override;
70 
74  virtual void fillSuffix(std::ostringstream&, const ErrorObj&) override {}
75 
81  virtual void routePayload(const std::ostringstream& o, const ErrorObj& e) override;
82 };
83 
84 // END DECLARATION
85 //======================================================================
86 // BEGIN IMPLEMENTATION
87 
88 //======================================================================
89 // ELTRACE c'tor
90 //======================================================================
91 ELTRACE::ELTRACE(Parameters const& pset) : ELdestination(pset().elDestConfig()) {
92  size_t msk;
93 
94  if (pset().lvls() != 0) {
95  msk = pset().lvls();
96  TRACE_CNTL("lvlmskS", msk); // the S mask for TRACE_NAME
97  }
98  if (pset().lvlm() != 0) {
99  msk = pset().lvlm();
100  TRACE_CNTL("lvlmskM", msk); // the M mask for TRACE_NAME
101  }
102 
103  TLOG(TLVL_INFO) << "ELTRACE MessageLogger destination plugin initialized.";
104 }
105 
106 //======================================================================
107 // Message prefix filler ( overriddes ELdestination::fillPrefix )
108 //======================================================================
109 void ELTRACE::fillPrefix(std::ostringstream& oss, const ErrorObj& msg) {
110  const auto& xid = msg.xid();
111 
112  oss << xid.application() << ", "; // application
113  oss << xid.id() << ": "; // category
114  // oss << mf::MessageDrop::instance()->runEvent + ELstring(" "); // run/event no
115  // oss << xid.module+ELstring(": "); // module name
116 }
117 
118 //======================================================================
119 // Message filler ( overriddes ELdestination::fillUsrMsg )
120 //======================================================================
121 void ELTRACE::fillUsrMsg(std::ostringstream& oss, const ErrorObj& msg) {
122  std::ostringstream tmposs;
123  ELdestination::fillUsrMsg(tmposs, msg);
124 
125  // remove leading "\n" if present
126  const std::string& usrMsg = !tmposs.str().compare(0, 1, "\n") ? tmposs.str().erase(0, 1) : tmposs.str();
127 
128  oss << usrMsg;
129 }
130 
131 //======================================================================
132 // Message router ( overriddes ELdestination::routePayload )
133 //======================================================================
134 void ELTRACE::routePayload(const std::ostringstream& oss, const ErrorObj& msg) {
135  const auto& xid = msg.xid();
136  auto message = oss.str();
137 
138  auto level = xid.severity().getLevel();
139  auto lvlNum = 0;
140 
141  switch (level) {
142  case mf::ELseverityLevel::ELsev_success:
143  case mf::ELseverityLevel::ELsev_zeroSeverity:
144  case mf::ELseverityLevel::ELsev_unspecified:
145  lvlNum = 3;
146  break;
147 
148  case mf::ELseverityLevel::ELsev_info:
149  lvlNum = 2;
150  break;
151 
152  case mf::ELseverityLevel::ELsev_warning:
153  lvlNum = 1;
154  break;
155  }
156  TRACE(lvlNum, message); // this is the TRACE -- direct the message to memory and/or stdout
157 }
158 } // end namespace mfplugins
159 
160 //======================================================================
161 //
162 // makePlugin function
163 //
164 //======================================================================
165 
166 #ifndef EXTERN_C_FUNC_DECLARE_START
167 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
168 #endif
169 
170 EXTERN_C_FUNC_DECLARE_START
171 auto makePlugin(const std::string&, const fhicl::ParameterSet& pset) {
172  return std::make_unique<mfplugins::ELTRACE>(pset);
173 }
174 }
175 
176 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.