Line data Source code
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/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 :
29 : /// <summary>
30 : /// Message Facility destination which logs messages to a TRACE buffer
31 : /// </summary>
32 : class ELTRACE : public ELdestination
33 : {
34 : public:
35 : /**
36 : * \brief Configuration Parameters for ELTRACE
37 : */
38 : struct Config
39 : {
40 : /// ELDestination common parameters
41 : fhicl::TableFragment<ELdestination::Config> elDestConfig;
42 : /// "lvls" (Default: 0): TRACE level mask for Slow output
43 : fhicl::Atom<size_t> lvls =
44 : fhicl::Atom<size_t>{fhicl::Name{"lvls"}, fhicl::Comment{"TRACE level mask for Slow output"}, 0};
45 : /// "lvlm" (Default: 0): TRACE level mask for Memory output
46 : fhicl::Atom<size_t> lvlm =
47 : fhicl::Atom<size_t>{fhicl::Name{"lvlm"}, fhicl::Comment{"TRACE level mask for Memory output"}, 0};
48 : };
49 : /// Used for ParameterSet validation
50 : using Parameters = fhicl::WrappedTable<Config>;
51 :
52 : public:
53 : /// <summary>
54 : /// ELTRACE Constructor
55 : /// </summary>
56 : /// <param name="pset">ParameterSet used to configure ELTRACE</param>
57 : ELTRACE(Parameters const& pset);
58 :
59 : /**
60 : * \brief Fill the "Prefix" portion of the message
61 : * \param o Output stringstream
62 : * \param msg MessageFacility object containing header information
63 : */
64 : void fillPrefix(std::ostringstream& o, const ErrorObj& msg) override;
65 :
66 : /**
67 : * \brief Fill the "User Message" portion of the message
68 : * \param o Output stringstream
69 : * \param msg MessageFacility object containing header information
70 : */
71 : void fillUsrMsg(std::ostringstream& o, const ErrorObj& msg) override;
72 :
73 : /**
74 : * \brief Fill the "Suffix" portion of the message (Unused)
75 : */
76 0 : void fillSuffix(std::ostringstream& /*unused*/, const ErrorObj& /*msg*/) override {}
77 :
78 : /**
79 : * \brief Serialize a MessageFacility message to the output
80 : * \param o Stringstream object containing message data
81 : * \param msg MessageFacility object containing header information
82 : */
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 : //======================================================================
93 0 : ELTRACE::ELTRACE(Parameters const& pset)
94 0 : : ELdestination(pset().elDestConfig())
95 : {
96 : size_t msk;
97 :
98 0 : if (pset().lvls() != 0)
99 : {
100 0 : msk = pset().lvls();
101 0 : TRACE_CNTL("lvlmskS", msk); // the S mask for TRACE_NAME
102 : }
103 0 : if (pset().lvlm() != 0)
104 : {
105 0 : msk = pset().lvlm();
106 0 : TRACE_CNTL("lvlmskM", msk); // the M mask for TRACE_NAME
107 : }
108 :
109 0 : TLOG(TLVL_INFO) << "ELTRACE MessageLogger destination plugin initialized.";
110 0 : }
111 :
112 : //======================================================================
113 : // Message prefix filler ( overriddes ELdestination::fillPrefix )
114 : //======================================================================
115 0 : void ELTRACE::fillPrefix(std::ostringstream& oss, const ErrorObj& msg)
116 : {
117 0 : const auto& xid = msg.xid();
118 :
119 0 : oss << xid.application() << ", "; // application
120 0 : oss << xid.id() << ": "; // category
121 : // oss << mf::MessageDrop::instance()->runEvent + ELstring(" "); // run/event no
122 : // oss << xid.module+ELstring(": "); // module name
123 0 : }
124 :
125 : //======================================================================
126 : // Message filler ( overriddes ELdestination::fillUsrMsg )
127 : //======================================================================
128 0 : void ELTRACE::fillUsrMsg(std::ostringstream& oss, const ErrorObj& msg)
129 : {
130 0 : std::ostringstream tmposs;
131 0 : ELdestination::fillUsrMsg(tmposs, msg);
132 :
133 : // remove leading "\n" if present
134 0 : const std::string& usrMsg = tmposs.str().compare(0, 1, "\n") == 0 ? tmposs.str().erase(0, 1) : tmposs.str();
135 :
136 0 : oss << usrMsg;
137 0 : }
138 :
139 : //======================================================================
140 : // Message router ( overriddes ELdestination::routePayload )
141 : //======================================================================
142 0 : void ELTRACE::routePayload(const std::ostringstream& oss, const ErrorObj& msg)
143 : {
144 0 : const auto& xid = msg.xid();
145 0 : auto message = oss.str();
146 :
147 0 : auto level = xid.severity().getLevel();
148 : int lvlNum;
149 :
150 0 : switch (level)
151 : {
152 0 : case mf::ELseverityLevel::ELsev_success:
153 : case mf::ELseverityLevel::ELsev_zeroSeverity:
154 : case mf::ELseverityLevel::ELsev_unspecified:
155 0 : lvlNum = 3;
156 0 : break;
157 :
158 0 : case mf::ELseverityLevel::ELsev_info:
159 0 : lvlNum = 2;
160 0 : break;
161 :
162 0 : case mf::ELseverityLevel::ELsev_warning:
163 0 : lvlNum = 1;
164 0 : break;
165 0 : default:
166 0 : lvlNum = 0;
167 : }
168 0 : TRACE(lvlNum, message); // NOLINT this is the TRACE -- direct the message to memory and/or stdout
169 0 : }
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 0 : auto makePlugin(const std::string& /*unused*/, const fhicl::ParameterSet& pset)
184 : {
185 0 : return std::make_unique<mfplugins::ELTRACE>(pset);
186 : }
187 : }
188 :
189 0 : DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
|