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 std::string timeformat_;
00029 bool stopped_;
00030
00031 std::string getTime_()
00032 {
00033 static std::mutex timeMutex;
00034 std::unique_lock<std::mutex> lk(timeMutex);
00035 const std::time_t result = std::time(0);
00036 auto resultTm = localtime(&result);
00037 std::string timeString;
00038 timeString.reserve(30);
00039 strftime(&timeString[0], 30, timeformat_.c_str(), resultTm);
00040
00041 return timeString;
00042 }
00043 public:
00057 explicit FileMetric(fhicl::ParameterSet const& config, std::string const& app_name) : MetricPlugin(config, app_name)
00058 , outputFile_(pset.get<std::string>("fileName", "FileMetric.out"))
00059 , uniquify_file_name_(pset.get<bool>("uniquify", false))
00060 , timeformat_(pset.get<std::string>("time_format", "%c"))
00061 , stopped_(true)
00062 {
00063 std::string modeString = pset.get<std::string>("fileMode", "append");
00064
00065 mode_ = std::ofstream::out | std::ofstream::app;
00066 if (modeString == "Overwrite" || modeString == "Create" || modeString == "Write")
00067 {
00068 mode_ = std::ofstream::out | std::ofstream::trunc;
00069 }
00070
00071 if (uniquify_file_name_)
00072 {
00073 std::string unique_id = std::to_string(getpid());
00074 if (outputFile_.find("%UID%") != std::string::npos)
00075 {
00076 outputFile_ = outputFile_.replace(outputFile_.find("%UID%"), 5, unique_id);
00077 }
00078 else
00079 {
00080 if (outputFile_.rfind(".") != std::string::npos)
00081 {
00082 outputFile_ = outputFile_.insert(outputFile_.rfind("."), "_" + unique_id);
00083 }
00084 else
00085 {
00086 outputFile_ = outputFile_.append("_" + unique_id);
00087 }
00088 }
00089 }
00090 openFile_();
00091 startMetrics();
00092 }
00093
00097 virtual ~FileMetric()
00098 {
00099 stopMetrics();
00100 closeFile_();
00101 }
00102
00107 std::string getLibName() const override { return "file"; }
00108
00115 void sendMetric_(const std::string& name, const std::string& value, const std::string& unit) override
00116 {
00117 if (!stopped_ && !inhibit_)
00118 {
00119 outputStream_ << getTime_() << "FileMetric: " << name << ": " << value << " " << unit << "." << std::endl;
00120 }
00121 }
00122
00129 void sendMetric_(const std::string& name, const int& value, const std::string& unit) override
00130 {
00131 sendMetric_(name, std::to_string(value), unit);
00132 }
00133
00140 void sendMetric_(const std::string& name, const double& value, const std::string& unit) override
00141 {
00142 sendMetric_(name, std::to_string(value), unit);
00143 }
00144
00151 void sendMetric_(const std::string& name, const float& value, const std::string& unit) override
00152 {
00153 sendMetric_(name, std::to_string(value), unit);
00154 }
00155
00162 void sendMetric_(const std::string& name, const unsigned long int& value, const std::string& unit) override
00163 {
00164 sendMetric_(name, std::to_string(value), unit);
00165 }
00166
00170 void startMetrics_() override
00171 {
00172 stopped_ = false;
00173 outputStream_ << getTime_() << "FileMetric plugin started." << std::endl;
00174 }
00175
00179 void stopMetrics_() override
00180 {
00181 stopped_ = true;
00182 outputStream_ << getTime_() << "FileMetric plugin has been stopped!" << std::endl;
00183 }
00184
00185 private:
00186 void openFile_()
00187 {
00188 outputStream_.open(outputFile_.c_str(), mode_);
00189 outputStream_ << getTime_() << "FileMetric plugin file opened." << std::endl;
00190 }
00191
00192 void closeFile_()
00193 {
00194 outputStream_ << getTime_() << "FileMetric closing file stream." << std::endl;
00195 outputStream_.close();
00196 }
00197 };
00198 }
00199
00200 DEFINE_ARTDAQ_METRIC(artdaq::FileMetric)