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