artdaq_utilities  v1_06_03
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 "artdaq-utilities/Plugins/MetricMacros.hh"
8 #include "fhiclcpp/ParameterSet.h"
9 #include "messagefacility/MessageLogger/MessageLogger.h"
10 
11 #include <algorithm>
12 #include <iostream>
13 #include <string>
14 
15 namespace artdaq {
19 class MsgFacilityMetric final : public MetricPlugin
20 {
21 private:
22  std::string facility_;
23  int outputLevel_;
24 
25  MsgFacilityMetric(const MsgFacilityMetric&) = delete;
27  MsgFacilityMetric& operator=(const MsgFacilityMetric&) = delete;
28  MsgFacilityMetric& operator=(MsgFacilityMetric&&) = delete;
29 
30 public:
44  explicit MsgFacilityMetric(fhicl::ParameterSet const& config, std::string const& app_name)
45  : MetricPlugin(config, app_name)
46  , facility_(config.get<std::string>("output_message_category_name", "ARTDAQ Metric"))
47  , outputLevel_(0)
48  {
49  try
50  {
51  outputLevel_ = config.get<int>("output_message_severity", 0);
52  }
53  catch (const cet::exception&)
54  {
55  auto levelString = config.get<std::string>("output_message_severity", "Info");
56  if (levelString == "Info" || levelString == "info" || levelString == "LogInfo")
57  {
58  outputLevel_ = 0;
59  }
60  else if (levelString == "Debug" || levelString == "debug" || levelString == "LogDebug")
61  {
62  outputLevel_ = 1;
63  }
64  else if (levelString == "Warning" || levelString == "warning" || levelString == "LogWarning" || levelString == "Warn" || levelString == "warn")
65  {
66  outputLevel_ = 2;
67  }
68  else if (levelString == "Error" || levelString == "error" || levelString == "LogError")
69  {
70  outputLevel_ = 3;
71  }
72  }
73  startMetrics();
74  }
75 
79  ~MsgFacilityMetric() override { stopMetrics(); }
84  std::string getLibName() const override { return "msgFacility"; }
85 
94  void sendMetric_(const std::string& name, const std::string& value, const std::string& unit, const std::chrono::system_clock::time_point&) override
95  {
96  if (!inhibit_)
97  {
98  switch (outputLevel_)
99  {
100  case 0:
101  mf::LogInfo(facility_) << name << ": " << value << " " << unit << "." << std::endl;
102  break;
103  case 1:
104  mf::LogDebug(facility_) << name << ": " << value << " " << unit << "." << std::endl;
105  break;
106  case 2:
107  mf::LogWarning(facility_) << name << ": " << value << " " << unit << "." << std::endl;
108  break;
109  case 3:
110  mf::LogError(facility_) << name << ": " << value << " " << unit << "." << std::endl;
111  break;
112  }
113  }
114  }
115 
123  void sendMetric_(const std::string& name, const int& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
124  {
125  sendMetric_(name, std::to_string(value), unit, time);
126  }
127 
135  void sendMetric_(const std::string& name, const double& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
136  {
137  sendMetric_(name, std::to_string(value), unit, time);
138  }
139 
147  void sendMetric_(const std::string& name, const float& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
148  {
149  sendMetric_(name, std::to_string(value), unit, time);
150  }
151 
159  void sendMetric_(const std::string& name, const uint64_t& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
160  {
161  sendMetric_(name, std::to_string(value), unit, time);
162  }
163 
167  void startMetrics_() override {}
171  void stopMetrics_() override {}
172 };
173 } //End namespace artdaq
174 
175 DEFINE_ARTDAQ_METRIC(artdaq::MsgFacilityMetric)
The MetricPlugin class defines the interface that MetricManager uses to send metric data to the vario...
Definition: MetricPlugin.hh:38
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.
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.
MsgFacilityMetric(fhicl::ParameterSet const &config, std::string const &app_name)
MsgFacilityMetric Constructor.
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...