7 #include "artdaq-utilities/Plugins/MetricMacros.hh"
8 #include "fhiclcpp/ParameterSet.h"
10 #include <sys/types.h>
23 std::string outputFile_;
24 bool uniquify_file_name_;
25 std::ofstream outputStream_;
26 std::ios_base::openmode mode_;
27 std::string timeformat_;
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());
36 struct std::tm* ptm = std::localtime(&tt);
37 if (timeformat_.size()) {
39 return stream << std::put_time(ptm, timeformat_.c_str()) <<
": ";
58 explicit FileMetric(fhicl::ParameterSet
const& config, std::string
const& 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")),
64 std::string modeString =
pset.get<std::string>(
"fileMode",
"append");
66 mode_ = std::ofstream::out | std::ofstream::app;
67 if (modeString ==
"Overwrite" || modeString ==
"Create" || modeString ==
"Write") {
68 mode_ = std::ofstream::out | std::ofstream::trunc;
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);
76 if (outputFile_.rfind(
".") != std::string::npos) {
77 outputFile_ = outputFile_.insert(outputFile_.rfind(
"."),
"_" + unique_id);
79 outputFile_ = outputFile_.append(
"_" + unique_id);
99 std::string
getLibName()
const override {
return "file"; }
107 void sendMetric_(
const std::string& name,
const std::string& value,
const std::string& unit)
override {
109 getTime_(outputStream_) <<
"FileMetric: " << name <<
": " << value <<
" " << unit <<
"." << std::endl;
119 void sendMetric_(
const std::string& name,
const int& value,
const std::string& unit)
override {
129 void sendMetric_(
const std::string& name,
const double& value,
const std::string& unit)
override {
139 void sendMetric_(
const std::string& name,
const float& value,
const std::string& unit)
override {
149 void sendMetric_(
const std::string& name,
const unsigned long int& value,
const std::string& unit)
override {
158 getTime_(outputStream_) <<
"FileMetric plugin started." << std::endl;
166 getTime_(outputStream_) <<
"FileMetric plugin has been stopped!" << std::endl;
171 outputStream_.open(outputFile_.c_str(), mode_);
172 getTime_(outputStream_) <<
"FileMetric plugin file opened." << std::endl;
176 getTime_(outputStream_) <<
"FileMetric closing file stream." << std::endl;
177 outputStream_.close();
void sendMetric_(const std::string &name, const std::string &value, const std::string &unit) override
Write metric data to a file.
void sendMetric_(const std::string &name, const unsigned long int &value, const std::string &unit) override
Write metric data to a file.
void sendMetric_(const std::string &name, const double &value, const std::string &unit) override
Write metric data to a file.
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.
void stopMetrics_() override
Perform shutdown actions. Writes stop message to output file.
void startMetrics_() override
Perform startup actions. Writes start message to output file.
void sendMetric_(const std::string &name, const float &value, const std::string &unit) override
Write metric data to a file.
FileMetric writes metric data to a file on disk.
virtual ~FileMetric()
FileMetric Destructor. Calls stopMetrics and then closes the file.
FileMetric(fhicl::ParameterSet const &config, std::string const &app_name)
FileMetric Constructor. Opens the file and starts the metric.
std::string getLibName() const override
Get the library name for the File metric.
bool inhibit_
Whether to inhibit all metric sending.