artdaq_utilities  v1_07_02_01
msgFacility_metric.cc
1 // msgFacility_metric.cc: Message Facility Metric Plugin
2 // Author: Eric Flumerfelt
3 // Last Modified: 09/29/2015
4 //
5 // An implementation of the MetricPlugin for Message Facility
6 
7 #include "TRACE/tracemf.h" // order matters -- trace.h (no "mf") is nested from MetricMacros.hh
8 #define TRACE_NAME (app_name_ + "_msgfacility_metric").c_str()
9 
10 #include "artdaq-utilities/Plugins/MetricMacros.hh"
11 #include "fhiclcpp/ParameterSet.h"
12 #include "messagefacility/MessageLogger/MessageLogger.h"
13 
14 #include <algorithm>
15 #include <iostream>
16 #include <string>
17 
18 namespace artdaq {
22 class MsgFacilityMetric final : public MetricPlugin
23 {
24 private:
25  std::string facility_;
26  int outputLevel_;
27 
28  MsgFacilityMetric(const MsgFacilityMetric&) = delete;
30  MsgFacilityMetric& operator=(const MsgFacilityMetric&) = delete;
31  MsgFacilityMetric& operator=(MsgFacilityMetric&&) = delete;
32 
33 public:
48  explicit MsgFacilityMetric(fhicl::ParameterSet const& config, std::string const& app_name, std::string const& metric_name)
49  : MetricPlugin(config, app_name, metric_name)
50  , facility_(config.get<std::string>("output_message_category_name", "ARTDAQ Metric"))
51  , outputLevel_(0)
52  {
53  try
54  {
55  outputLevel_ = config.get<int>("output_message_severity", 0);
56  }
57  catch (const cet::exception&)
58  {
59  auto levelString = config.get<std::string>("output_message_severity", "Info");
60  if (levelString == "Info" || levelString == "info" || levelString == "LogInfo")
61  {
62  outputLevel_ = 0;
63  }
64  else if (levelString == "Debug" || levelString == "debug" || levelString == "LogDebug")
65  {
66  outputLevel_ = 1;
67  }
68  else if (levelString == "Warning" || levelString == "warning" || levelString == "LogWarning" || levelString == "Warn" || levelString == "warn")
69  {
70  outputLevel_ = 2;
71  }
72  else if (levelString == "Error" || levelString == "error" || levelString == "LogError")
73  {
74  outputLevel_ = 3;
75  }
76  }
77  startMetrics();
78  }
79 
83  ~MsgFacilityMetric() override { stopMetrics(); }
88  std::string getLibName() const override { return "msgFacility"; }
89 
98  void sendMetric_(const std::string& name, const std::string& value, const std::string& unit, const std::chrono::system_clock::time_point&) override
99  {
100  if (!inhibit_)
101  {
102  switch (outputLevel_)
103  {
104  case 0:
105  mf::LogInfo(facility_) << name << ": " << value << " " << unit << "." << std::endl;
106  break;
107  case 1:
108  mf::LogDebug(facility_) << name << ": " << value << " " << unit << "." << std::endl;
109  break;
110  case 2:
111  mf::LogWarning(facility_) << name << ": " << value << " " << unit << "." << std::endl;
112  break;
113  case 3:
114  mf::LogError(facility_) << name << ": " << value << " " << unit << "." << std::endl;
115  break;
116  }
117  }
118  }
119 
127  void sendMetric_(const std::string& name, const int& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
128  {
129  sendMetric_(name, std::to_string(value), unit, time);
130  }
131 
139  void sendMetric_(const std::string& name, const double& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
140  {
141  sendMetric_(name, std::to_string(value), unit, time);
142  }
143 
151  void sendMetric_(const std::string& name, const float& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
152  {
153  sendMetric_(name, std::to_string(value), unit, time);
154  }
155 
163  void sendMetric_(const std::string& name, const uint64_t& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
164  {
165  sendMetric_(name, std::to_string(value), unit, time);
166  }
167 
171  void startMetrics_() override {}
175  void stopMetrics_() override {}
176 };
177 } //End namespace artdaq
178 
179 DEFINE_ARTDAQ_METRIC(artdaq::MsgFacilityMetric)
The MetricPlugin class defines the interface that MetricManager uses to send metric data to the vario...
Definition: MetricPlugin.hh:41
void startMetrics()
Perform startup actions. Simply calls the virtual startMetrics_ function.
void sendMetric_(const std::string &name, const float &value, const std::string &unit, const std::chrono::system_clock::time_point &time) override
Send a metric to MessageFacility. All metrics are converted to strings.
std::string getLibName() const override
Return the library name of the MetricPlugin.
MsgFacilityMetric(fhicl::ParameterSet const &config, std::string const &app_name, std::string const &metric_name)
MsgFacilityMetric Constructor.
void stopMetrics()
Perform shutdown actions. Zeroes out all accumulators, and sends zeros for each metric. Calls stopMetrics_() for any plugin-defined shutdown actions.
void stopMetrics_() override
Perform shutdown actions. No-Op.
void sendMetric_(const std::string &name, const std::string &value, const std::string &unit, const std::chrono::system_clock::time_point &) override
Send a metric to MessageFacilty. Format is: &quot;name: value unit.&quot;.
void startMetrics_() override
Perform startup actions. No-Op.
void sendMetric_(const std::string &name, const double &value, const std::string &unit, const std::chrono::system_clock::time_point &time) override
Send a metric to MessageFacility. All metrics are converted to strings.
void sendMetric_(const std::string &name, const uint64_t &value, const std::string &unit, const std::chrono::system_clock::time_point &time) override
Send a metric to MessageFacility. All metrics are converted to strings.
void sendMetric_(const std::string &name, const int &value, const std::string &unit, const std::chrono::system_clock::time_point &time) override
Send a metric to MessageFacility. All metrics are converted to strings.
A MetricPlugin class which sends metric data to MessageFacility.
~MsgFacilityMetric() override
MsgFacilityMetric Destructor. Calls stopMetrics()
bool inhibit_
Flag to indicate that the MetricPlugin is being stopped, and any metric back-ends which do not have a...