00001
00002
00003
00004
00005
00006
00007 #include "artdaq-utilities/Plugins/MetricMacros.hh"
00008 #include "fhiclcpp/ParameterSet.h"
00009
00010 #include <fstream>
00011 #include <ctime>
00012 #include <string>
00013 #include <sys/types.h>
00014 #include <unistd.h>
00015
00016 namespace artdaq
00017 {
00021 class FileMetric : public MetricPlugin
00022 {
00023 private:
00024 std::string outputFile_;
00025 bool uniquify_file_name_;
00026 std::ofstream outputStream_;
00027 std::ios_base::openmode mode_;
00028 bool stopped_;
00029 public:
00042 explicit FileMetric(fhicl::ParameterSet const& config, std::string const& app_name) : MetricPlugin(config, app_name)
00043 , outputFile_(pset.get<std::string>("fileName", "FileMetric.out"))
00044 , uniquify_file_name_(pset.get<bool>("uniquify", false))
00045 , stopped_(true)
00046 {
00047 std::string modeString = pset.get<std::string>("fileMode", "append");
00048
00049 mode_ = std::ofstream::out | std::ofstream::app;
00050 if (modeString == "Overwrite" || modeString == "Create" || modeString == "Write")
00051 {
00052 mode_ = std::ofstream::out | std::ofstream::trunc;
00053 }
00054
00055 if (uniquify_file_name_)
00056 {
00057 std::string unique_id = std::to_string(getpid());
00058 if (outputFile_.find("%UID%") != std::string::npos)
00059 {
00060 outputFile_ = outputFile_.replace(outputFile_.find("%UID%"), 5, unique_id);
00061 }
00062 else
00063 {
00064 if (outputFile_.rfind(".") != std::string::npos)
00065 {
00066 outputFile_ = outputFile_.insert(outputFile_.rfind("."), "_" + unique_id);
00067 }
00068 else
00069 {
00070 outputFile_ = outputFile_.append("_" + unique_id);
00071 }
00072 }
00073 }
00074 openFile_();
00075 startMetrics();
00076 }
00077
00081 virtual ~FileMetric()
00082 {
00083 stopMetrics();
00084 closeFile_();
00085 }
00086
00091 std::string getLibName() const override { return "file"; }
00092
00099 void sendMetric_(const std::string& name, const std::string& value, const std::string& unit) override
00100 {
00101 if (!stopped_ && !inhibit_)
00102 {
00103 const std::time_t result = std::time(0);
00104 outputStream_ << std::ctime(&result) << "FileMetric: " << name << ": " << value << " " << unit << "." << std::endl;
00105 }
00106 }
00107
00114 void sendMetric_(const std::string& name, const int& value, const std::string& unit) override
00115 {
00116 sendMetric_(name, std::to_string(value), unit);
00117 }
00118
00125 void sendMetric_(const std::string& name, const double& value, const std::string& unit) override
00126 {
00127 sendMetric_(name, std::to_string(value), unit);
00128 }
00129
00136 void sendMetric_(const std::string& name, const float& value, const std::string& unit) override
00137 {
00138 sendMetric_(name, std::to_string(value), unit);
00139 }
00140
00147 void sendMetric_(const std::string& name, const unsigned long int& value, const std::string& unit) override
00148 {
00149 sendMetric_(name, std::to_string(value), unit);
00150 }
00151
00155 void startMetrics_() override
00156 {
00157 stopped_ = false;
00158 const std::time_t result = std::time(0);
00159 outputStream_ << std::ctime(&result) << "FileMetric plugin started." << std::endl;
00160 }
00161
00165 void stopMetrics_() override
00166 {
00167 stopped_ = true;
00168 const std::time_t result = std::time(0);
00169 outputStream_ << std::ctime(&result) << "FileMetric plugin has been stopped!" << std::endl;
00170 }
00171
00172 private:
00173 void openFile_()
00174 {
00175 outputStream_.open(outputFile_.c_str(), mode_);
00176 const std::time_t result = std::time(0);
00177 outputStream_ << std::ctime(&result) << "FileMetric plugin file opened." << std::endl;
00178 }
00179
00180 void closeFile_()
00181 {
00182 const std::time_t result = std::time(0);
00183 outputStream_ << std::ctime(&result) << "FileMetric closing file stream." << std::endl;
00184 outputStream_.close();
00185 }
00186 };
00187 }
00188
00189 DEFINE_ARTDAQ_METRIC(artdaq::FileMetric)