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