artdaq_mfextensions  v1_05_00
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 {
33 public:
37  struct Config
38  {
40  fhicl::TableFragment<ELdestination::Config> elDestConfig;
42  fhicl::Atom<size_t> lvls =
43  fhicl::Atom<size_t>{fhicl::Name{"lvls"}, fhicl::Comment{"TRACE level mask for Slow output"}, 0};
45  fhicl::Atom<size_t> lvlm =
46  fhicl::Atom<size_t>{fhicl::Name{"lvlm"}, fhicl::Comment{"TRACE level mask for Memory output"}, 0};
47  };
49  using Parameters = fhicl::WrappedTable<Config>;
50 
51 public:
56  ELTRACE(Parameters const& pset);
57 
63  void fillPrefix(std::ostringstream& o, const ErrorObj& msg) override;
64 
70  void fillUsrMsg(std::ostringstream& o, const ErrorObj& msg) override;
71 
75  void fillSuffix(std::ostringstream& /*unused*/, const ErrorObj& /*msg*/) override {}
76 
82  void routePayload(const std::ostringstream& o, const ErrorObj& msg) override;
83 };
84 
85 // END DECLARATION
86 //======================================================================
87 // BEGIN IMPLEMENTATION
88 
89 //======================================================================
90 // ELTRACE c'tor
91 //======================================================================
93  : ELdestination(pset().elDestConfig())
94 {
95  size_t msk;
96 
97  if (pset().lvls() != 0)
98  {
99  msk = pset().lvls();
100  TRACE_CNTL("lvlmskS", msk); // the S mask for TRACE_NAME
101  }
102  if (pset().lvlm() != 0)
103  {
104  msk = pset().lvlm();
105  TRACE_CNTL("lvlmskM", msk); // the M mask for TRACE_NAME
106  }
107 
108  TLOG(TLVL_INFO) << "ELTRACE MessageLogger destination plugin initialized.";
109 }
110 
111 //======================================================================
112 // Message prefix filler ( overriddes ELdestination::fillPrefix )
113 //======================================================================
114 void ELTRACE::fillPrefix(std::ostringstream& oss, const ErrorObj& msg)
115 {
116  const auto& xid = msg.xid();
117 
118  oss << xid.application() << ", "; // application
119  oss << xid.id() << ": "; // category
120  // oss << mf::MessageDrop::instance()->runEvent + ELstring(" "); // run/event no
121  // oss << xid.module+ELstring(": "); // module name
122 }
123 
124 //======================================================================
125 // Message filler ( overriddes ELdestination::fillUsrMsg )
126 //======================================================================
127 void ELTRACE::fillUsrMsg(std::ostringstream& oss, const ErrorObj& msg)
128 {
129  std::ostringstream tmposs;
130  ELdestination::fillUsrMsg(tmposs, msg);
131 
132  // remove leading "\n" if present
133  const std::string& usrMsg = tmposs.str().compare(0, 1, "\n") == 0 ? tmposs.str().erase(0, 1) : tmposs.str();
134 
135  oss << usrMsg;
136 }
137 
138 //======================================================================
139 // Message router ( overriddes ELdestination::routePayload )
140 //======================================================================
141 void ELTRACE::routePayload(const std::ostringstream& oss, const ErrorObj& msg)
142 {
143  const auto& xid = msg.xid();
144  auto message = oss.str();
145 
146  auto level = xid.severity().getLevel();
147  int lvlNum;
148 
149  switch (level)
150  {
151  case mf::ELseverityLevel::ELsev_success:
152  case mf::ELseverityLevel::ELsev_zeroSeverity:
153  case mf::ELseverityLevel::ELsev_unspecified:
154  lvlNum = 3;
155  break;
156 
157  case mf::ELseverityLevel::ELsev_info:
158  lvlNum = 2;
159  break;
160 
161  case mf::ELseverityLevel::ELsev_warning:
162  lvlNum = 1;
163  break;
164  default:
165  lvlNum = 0;
166  }
167  TRACE(lvlNum, message); // NOLINT this is the TRACE -- direct the message to memory and/or stdout
168 }
169 } // end namespace mfplugins
170 
171 //======================================================================
172 //
173 // makePlugin function
174 //
175 //======================================================================
176 
177 #ifndef EXTERN_C_FUNC_DECLARE_START
178 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
179 #endif
180 
181 EXTERN_C_FUNC_DECLARE_START
182 auto makePlugin(const std::string& /*unused*/, const fhicl::ParameterSet& pset)
183 {
184  return std::make_unique<mfplugins::ELTRACE>(pset);
185 }
186 }
187 
188 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
void fillPrefix(std::ostringstream &o, const ErrorObj &msg) override
Fill the &quot;Prefix&quot; portion of the message.
void fillSuffix(std::ostringstream &, const ErrorObj &) override
Fill the &quot;Suffix&quot; portion of the message (Unused)
Message Facility destination which logs messages to a TRACE buffer
fhicl::TableFragment< ELdestination::Config > elDestConfig
ELDestination common parameters.
ELTRACE(Parameters const &pset)
ELTRACE Constructor
Configuration Parameters for ELTRACE.
fhicl::Atom< size_t > lvlm
&quot;lvlm&quot; (Default: 0): TRACE level mask for Memory output
void fillUsrMsg(std::ostringstream &o, const ErrorObj &msg) override
Fill the &quot;User Message&quot; portion of the message.
fhicl::Atom< size_t > lvls
&quot;lvls&quot; (Default: 0): TRACE level mask for Slow output
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation.
void routePayload(const std::ostringstream &o, const ErrorObj &msg) override
Serialize a MessageFacility message to the output.