artdaq_mfextensions  v1_02_02
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 #if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
10 # include "messagefacility/MessageService/ELcontextSupplier.h"
11 # include "messagefacility/MessageLogger/MessageDrop.h"
12 #endif
13 #include "messagefacility/Utilities/exception.h"
14 
15 #define TRACE_NAME "MessageFacility"
16 #include "trace.h"
17 
18 
19 namespace mfplugins
20 {
21  using mf::service::ELdestination;
22  using mf::ELseverityLevel;
23 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
24  using mf::service::ELcontextSupplier;
25 # endif
26  using mf::ErrorObj;
27 
31  class ELTRACE : public ELdestination
32  {
33 
34 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
35  struct Config
36  {
37  fhicl::TableFragment<ELdestination::Config> elDestConfig;
38  fhicl::Atom<size_t> lvls{ fhicl::Name{ "lvls" },fhicl::Comment{ "TRACE level mask for Slow output" },0 };
39  fhicl::Atom<size_t> lvlm{ fhicl::Name{ "lvlm" },fhicl::Comment{ "TRACE level mask for Memory output" },0 };
40  };
41  using Parameters = fhicl::WrappedTable<Config>;
42 #endif
43 
44  public:
45 
46 #if MESSAGEFACILITY_HEX_VERSION < 0x20103 // v2_01_03 is s58, pre v2_01_03 is s50
47  ELTRACE(const fhicl::ParameterSet& pset);
48 #else
49  ELTRACE(Parameters const& pset);
50 #endif
51 
52  virtual void fillPrefix(std::ostringstream&, const ErrorObj&
53 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
54  , const ELcontextSupplier&
55 # endif
56  ) 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&
63 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
64  , const ELcontextSupplier&
65 # endif
66  ) override;
67 
68  };
69 
70  // END DECLARATION
71  //======================================================================
72  // BEGIN IMPLEMENTATION
73 
74 
75  //======================================================================
76  // ELTRACE c'tor
77  //======================================================================
78 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
79  ELTRACE::ELTRACE(const fhicl::ParameterSet& pset)
80  : ELdestination(pset)
81  {
82  size_t msk;
83 
84  if (pset.get_if_present<size_t>("lvls",msk))
85  TRACE_CNTL("lvlmskS",msk); // the S mask for TRACE_NAME
86 
87  if (pset.get_if_present<size_t>("lvlm",msk))
88  TRACE_CNTL("lvlmskM",msk); // the M mask for TRACE_NAME
89 
90  TRACE(3, "ELTRACE MessageLogger destination plugin initialized.");
91  }
92 #else
93  ELTRACE::ELTRACE(Parameters const& pset)
94  : ELdestination(pset().elDestConfig())
95  {
96  size_t msk;
97 
98  if (pset().lvls() != 0) {
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  TRACE(3, "ELTRACE MessageLogger destination plugin initialized.");
109  }
110 
111 #endif
112 
113  //======================================================================
114  // Message prefix filler ( overriddes ELdestination::fillPrefix )
115  //======================================================================
116  void ELTRACE::fillPrefix(std::ostringstream& oss, const ErrorObj& msg
117 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
118  , ELcontextSupplier const&
119 # endif
120  )
121  {
122  const auto& xid = msg.xid();
123 
124 # if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
125  oss << xid.application() << ", "; // application
126  oss << xid.id() << ": "; // category
127 # else
128  oss << xid.application << ", "; // application
129  oss << xid.id << ": "; // category
130 # endif
131  // oss << mf::MessageDrop::instance()->runEvent + ELstring(" "); // run/event no
132  // oss << xid.module+ELstring(": "); // module name
133  }
134 
135  //======================================================================
136  // Message filler ( overriddes ELdestination::fillUsrMsg )
137  //======================================================================
138  void ELTRACE::fillUsrMsg(std::ostringstream& oss, const ErrorObj& msg)
139  {
140  std::ostringstream tmposs;
141  ELdestination::fillUsrMsg(tmposs, msg);
142 
143  // remove leading "\n" if present
144  const std::string& usrMsg = !tmposs.str().compare(0, 1, "\n") ? tmposs.str().erase(0, 1) : tmposs.str();
145 
146  oss << usrMsg;
147  }
148 
149  //======================================================================
150  // Message router ( overriddes ELdestination::routePayload )
151  //======================================================================
152  void ELTRACE::routePayload(const std::ostringstream& oss, const ErrorObj& msg
153 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
154  , ELcontextSupplier const&
155 # endif
156  )
157  {
158  const auto& xid = msg.xid();
159  auto message = oss.str();
160 
161 # if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
162  auto level = xid.severity().getLevel();
163 # else
164  auto level = xid.severity.getLevel();
165 # endif
166  auto lvlNum = 0;
167 
168  switch (level)
169  {
170 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
171  case mf::ELseverityLevel::ELsev_incidental:
172 # endif
173  case mf::ELseverityLevel::ELsev_success:
174  case mf::ELseverityLevel::ELsev_zeroSeverity:
175  case mf::ELseverityLevel::ELsev_unspecified:
176  lvlNum = 3;
177  break;
178 
179  case mf::ELseverityLevel::ELsev_info:
180  lvlNum = 2;
181  break;
182 
183  case mf::ELseverityLevel::ELsev_warning:
184 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
185  case mf::ELseverityLevel::ELsev_warning2:
186 # endif
187  lvlNum = 1;
188  break;
189  }
190  TRACE(lvlNum, message); // this is the TRACE -- direct the message to memory and/or stdout
191  }
192  } // end namespace mfplugins
193 
194  //======================================================================
195  //
196  // makePlugin function
197  //
198  //======================================================================
199 
200  extern "C"
201  {
202  auto makePlugin(const std::string&,
203  const fhicl::ParameterSet& pset)
204  {
205  return std::make_unique<mfplugins::ELTRACE>(pset);
206  }
207  }
208 
209  DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
Message Facility destination which logs messages to a TRACE buffer