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