artdaq_utilities  v1_04_10
file_metric.cc
1 // FileMetric.h: File Metric Plugin
2 // Author: Eric Flumerfelt
3 // Last Modified: 11/06/2014
4 //
5 // An implementation of the MetricPlugin for Log Files
6 
7 #include "artdaq-utilities/Plugins/MetricMacros.hh"
8 #include "fhiclcpp/ParameterSet.h"
9 
10 #include <sys/types.h>
11 #include <unistd.h>
12 #include <ctime>
13 #include <fstream>
14 #include <iomanip>
15 #include <string>
16 
17 namespace artdaq {
21 class FileMetric : public MetricPlugin {
22  private:
23  std::string outputFile_;
24  bool uniquify_file_name_;
25  std::ofstream outputStream_;
26  std::ios_base::openmode mode_;
27  std::string timeformat_;
28  bool stopped_;
29 
30  std::ostream& getTime_(std::ostream & stream) {
31  static std::mutex timeMutex;
32  std::unique_lock<std::mutex> lk(timeMutex);
33  using std::chrono::system_clock;
34  std::time_t tt = system_clock::to_time_t(system_clock::now());
35 
36  struct std::tm* ptm = std::localtime(&tt);
37  if (timeformat_.size()) {
38 
39  return stream << std::put_time(ptm, timeformat_.c_str()) << ": ";
40  }
41 
42  return stream;
43  }
44 
45  public:
58  explicit FileMetric(fhicl::ParameterSet const& config, std::string const& app_name)
59  : MetricPlugin(config, app_name),
60  outputFile_(pset.get<std::string>("fileName", "FileMetric.out")),
61  uniquify_file_name_(pset.get<bool>("uniquify", false)),
62  timeformat_(pset.get<std::string>("time_format", "%c")),
63  stopped_(true) {
64  std::string modeString = pset.get<std::string>("fileMode", "append");
65 
66  mode_ = std::ofstream::out | std::ofstream::app;
67  if (modeString == "Overwrite" || modeString == "Create" || modeString == "Write") {
68  mode_ = std::ofstream::out | std::ofstream::trunc;
69  }
70 
71  if (uniquify_file_name_) {
72  std::string unique_id = std::to_string(getpid());
73  if (outputFile_.find("%UID%") != std::string::npos) {
74  outputFile_ = outputFile_.replace(outputFile_.find("%UID%"), 5, unique_id);
75  } else {
76  if (outputFile_.rfind(".") != std::string::npos) {
77  outputFile_ = outputFile_.insert(outputFile_.rfind("."), "_" + unique_id);
78  } else {
79  outputFile_ = outputFile_.append("_" + unique_id);
80  }
81  }
82  }
83  openFile_();
84  startMetrics();
85  }
86 
90  virtual ~FileMetric() {
91  stopMetrics();
92  closeFile_();
93  }
94 
99  std::string getLibName() const override { return "file"; }
100 
107  void sendMetric_(const std::string& name, const std::string& value, const std::string& unit) override {
108  if (!stopped_ && !inhibit_) {
109  getTime_(outputStream_) << "FileMetric: " << name << ": " << value << " " << unit << "." << std::endl;
110  }
111  }
112 
119  void sendMetric_(const std::string& name, const int& value, const std::string& unit) override {
120  sendMetric_(name, std::to_string(value), unit);
121  }
122 
129  void sendMetric_(const std::string& name, const double& value, const std::string& unit) override {
130  sendMetric_(name, std::to_string(value), unit);
131  }
132 
139  void sendMetric_(const std::string& name, const float& value, const std::string& unit) override {
140  sendMetric_(name, std::to_string(value), unit);
141  }
142 
149  void sendMetric_(const std::string& name, const unsigned long int& value, const std::string& unit) override {
150  sendMetric_(name, std::to_string(value), unit);
151  }
152 
156  void startMetrics_() override {
157  stopped_ = false;
158  getTime_(outputStream_) << "FileMetric plugin started." << std::endl;
159  }
160 
164  void stopMetrics_() override {
165  stopped_ = true;
166  getTime_(outputStream_) << "FileMetric plugin has been stopped!" << std::endl;
167  }
168 
169  private:
170  void openFile_() {
171  outputStream_.open(outputFile_.c_str(), mode_);
172  getTime_(outputStream_) << "FileMetric plugin file opened." << std::endl;
173  }
174 
175  void closeFile_() {
176  getTime_(outputStream_) << "FileMetric closing file stream." << std::endl;
177  outputStream_.close();
178  }
179 };
180 } // End namespace artdaq
181 
182 DEFINE_ARTDAQ_METRIC(artdaq::FileMetric)
void sendMetric_(const std::string &name, const std::string &value, const std::string &unit) override
Write metric data to a file.
Definition: file_metric.cc:107
void sendMetric_(const std::string &name, const unsigned long int &value, const std::string &unit) override
Write metric data to a file.
Definition: file_metric.cc:149
void sendMetric_(const std::string &name, const double &value, const std::string &unit) override
Write metric data to a file.
Definition: file_metric.cc:129
fhicl::ParameterSet pset
The ParameterSet used to configure the MetricPlugin.
void sendMetric_(const std::string &name, const int &value, const std::string &unit) override
Write metric data to a file.
Definition: file_metric.cc:119
void stopMetrics_() override
Perform shutdown actions. Writes stop message to output file.
Definition: file_metric.cc:164
void startMetrics_() override
Perform startup actions. Writes start message to output file.
Definition: file_metric.cc:156
void sendMetric_(const std::string &name, const float &value, const std::string &unit) override
Write metric data to a file.
Definition: file_metric.cc:139
FileMetric writes metric data to a file on disk.
Definition: file_metric.cc:21
virtual ~FileMetric()
FileMetric Destructor. Calls stopMetrics and then closes the file.
Definition: file_metric.cc:90
FileMetric(fhicl::ParameterSet const &config, std::string const &app_name)
FileMetric Constructor. Opens the file and starts the metric.
Definition: file_metric.cc:58
std::string getLibName() const override
Get the library name for the File metric.
Definition: file_metric.cc:99
bool inhibit_
Whether to inhibit all metric sending.