artdaq_utilities  v1_05_07b
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 
92  void sendMetric_(const std::string& name, const std::string& value, const std::string& unit) override
93  {
94  if (!inhibit_)
95  {
96  switch (outputLevel_)
97  {
98  case 0:
99  mf::LogInfo(facility_) << name << ": " << value << " " << unit << "." << std::endl;
100  break;
101  case 1:
102  mf::LogDebug(facility_) << name << ": " << value << " " << unit << "." << std::endl;
103  break;
104  case 2:
105  mf::LogWarning(facility_) << name << ": " << value << " " << unit << "." << std::endl;
106  break;
107  case 3:
108  mf::LogError(facility_) << name << ": " << value << " " << unit << "." << std::endl;
109  break;
110  }
111  }
112  }
113 
120  void sendMetric_(const std::string& name, const int& value, const std::string& unit) override
121  {
122  sendMetric_(name, std::to_string(value), unit);
123  }
124 
131  void sendMetric_(const std::string& name, const double& value, const std::string& unit) override
132  {
133  sendMetric_(name, std::to_string(value), unit);
134  }
135 
142  void sendMetric_(const std::string& name, const float& value, const std::string& unit) override
143  {
144  sendMetric_(name, std::to_string(value), unit);
145  }
146 
153  void sendMetric_(const std::string& name, const uint64_t& value, const std::string& unit) override
154  {
155  sendMetric_(name, std::to_string(value), unit);
156  }
157 
161  void startMetrics_() override {}
165  void stopMetrics_() override {}
166 };
167 } //End namespace artdaq
168 
169 DEFINE_ARTDAQ_METRIC(artdaq::MsgFacilityMetric)
void sendMetric_(const std::string &name, const uint64_t &value, const std::string &unit) override
Send a metric to MessageFacility. All metrics are converted to strings.
The MetricPlugin class defines the interface that MetricManager uses to send metric data to the vario...
Definition: MetricPlugin.hh:37
void startMetrics()
Perform startup actions. Simply calls the virtual startMetrics_ function.
void sendMetric_(const std::string &name, const int &value, const std::string &unit) 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 sendMetric_(const std::string &name, const float &value, const std::string &unit) override
Send a metric to MessageFacility. All metrics are converted to strings.
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) override
Send a metric to MessageFacilty. Format is: &quot;name: value unit.&quot;.
void startMetrics_() override
Perform startup actions. No-Op.
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()
void sendMetric_(const std::string &name, const double &value, const std::string &unit) override
Send a metric to MessageFacility. All metrics are converted to strings.
bool inhibit_
Flag to indicate that the MetricPlugin is being stopped, and any metric back-ends which do not have a...