artdaq_utilities  v1_02_02
 All Classes
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 <iostream>
12 #include <string>
13 #include <algorithm>
14 
15 namespace artdaq
16 {
18  {
19  private:
20  std::string facility_;
21  int outputLevel_;
22  public:
23  MsgFacilityMetric(fhicl::ParameterSet config)
24  : MetricPlugin(config)
25  , facility_(config.get<std::string>("output_message_application_name", "ARTDAQ Metric"))
26  , outputLevel_(0)
27  {
28  try
29  {
30  outputLevel_ = config.get<int>("output_message_severity", 0);
31  }
32  catch (cet::exception)
33  {
34  std::string levelString = config.get<std::string>("output_message_severity", "Info");
35  if (levelString == "Info" || levelString == "info" || levelString == "LogInfo")
36  {
37  outputLevel_ = 0;
38  }
39  else if (levelString == "Debug" || levelString == "debug" || levelString == "LogDebug")
40  {
41  outputLevel_ = 1;
42  }
43  else if (levelString == "Warning" || levelString == "warning" || levelString == "LogWarning" || levelString == "Warn" || levelString == "warn")
44  {
45  outputLevel_ = 2;
46  }
47  else if (levelString == "Error" || levelString == "error" || levelString == "LogError")
48  {
49  outputLevel_ = 3;
50  }
51  }
52  startMetrics();
53  }
54 
55  ~MsgFacilityMetric() { stopMetrics(); }
56  virtual std::string getLibName() const { return "msgFacility"; }
57 
58  virtual void sendMetric_(const std::string& name, const std::string& value, const std::string& unit)
59  {
60  if (!inhibit_)
61  {
62  switch (outputLevel_)
63  {
64  case 0:
65  mf::LogInfo(facility_) << name << ": " << value << " " << unit << "." << std::endl;
66  break;
67  case 1:
68  mf::LogDebug(facility_) << name << ": " << value << " " << unit << "." << std::endl;
69  break;
70  case 2:
71  mf::LogWarning(facility_) << name << ": " << value << " " << unit << "." << std::endl;
72  break;
73  case 3:
74  mf::LogError(facility_) << name << ": " << value << " " << unit << "." << std::endl;
75  break;
76  }
77  }
78  }
79 
80  virtual void sendMetric_(const std::string& name, const int& value, const std::string& unit)
81  {
82  sendMetric(name, std::to_string(value), unit);
83  }
84 
85  virtual void sendMetric_(const std::string& name, const double& value, const std::string& unit)
86  {
87  sendMetric(name, std::to_string(value), unit);
88  }
89 
90  virtual void sendMetric_(const std::string& name, const float& value, const std::string& unit)
91  {
92  sendMetric(name, std::to_string(value), unit);
93  }
94 
95  virtual void sendMetric_(const std::string& name, const unsigned long int& value, const std::string& unit)
96  {
97  sendMetric(name, std::to_string(value), unit);
98  }
99 
100  virtual void startMetrics_() {}
101  virtual void stopMetrics_() {}
102  };
103 } //End namespace artdaq
104 
105 DEFINE_ARTDAQ_METRIC(artdaq::MsgFacilityMetric)