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