$treeview $search $mathjax $extrastylesheet
artdaq_utilities
v1_04_10
$projectbrief
|
$projectbrief
|
$searchbox |
00001 // FileMetric.h: File Metric Plugin 00002 // Author: Eric Flumerfelt 00003 // Last Modified: 11/06/2014 00004 // 00005 // An implementation of the MetricPlugin for Log Files 00006 00007 #include "artdaq-utilities/Plugins/MetricMacros.hh" 00008 #include "fhiclcpp/ParameterSet.h" 00009 00010 #include <sys/types.h> 00011 #include <unistd.h> 00012 #include <ctime> 00013 #include <fstream> 00014 #include <iomanip> 00015 #include <string> 00016 00017 namespace artdaq { 00021 class FileMetric : public MetricPlugin { 00022 private: 00023 std::string outputFile_; 00024 bool uniquify_file_name_; 00025 std::ofstream outputStream_; 00026 std::ios_base::openmode mode_; 00027 std::string timeformat_; 00028 bool stopped_; 00029 00030 std::ostream& getTime_(std::ostream & stream) { 00031 static std::mutex timeMutex; 00032 std::unique_lock<std::mutex> lk(timeMutex); 00033 using std::chrono::system_clock; 00034 std::time_t tt = system_clock::to_time_t(system_clock::now()); 00035 00036 struct std::tm* ptm = std::localtime(&tt); 00037 if (timeformat_.size()) { 00038 00039 return stream << std::put_time(ptm, timeformat_.c_str()) << ": "; 00040 } 00041 00042 return stream; 00043 } 00044 00045 public: 00058 explicit FileMetric(fhicl::ParameterSet const& config, std::string const& app_name) 00059 : MetricPlugin(config, app_name), 00060 outputFile_(pset.get<std::string>("fileName", "FileMetric.out")), 00061 uniquify_file_name_(pset.get<bool>("uniquify", false)), 00062 timeformat_(pset.get<std::string>("time_format", "%c")), 00063 stopped_(true) { 00064 std::string modeString = pset.get<std::string>("fileMode", "append"); 00065 00066 mode_ = std::ofstream::out | std::ofstream::app; 00067 if (modeString == "Overwrite" || modeString == "Create" || modeString == "Write") { 00068 mode_ = std::ofstream::out | std::ofstream::trunc; 00069 } 00070 00071 if (uniquify_file_name_) { 00072 std::string unique_id = std::to_string(getpid()); 00073 if (outputFile_.find("%UID%") != std::string::npos) { 00074 outputFile_ = outputFile_.replace(outputFile_.find("%UID%"), 5, unique_id); 00075 } else { 00076 if (outputFile_.rfind(".") != std::string::npos) { 00077 outputFile_ = outputFile_.insert(outputFile_.rfind("."), "_" + unique_id); 00078 } else { 00079 outputFile_ = outputFile_.append("_" + unique_id); 00080 } 00081 } 00082 } 00083 openFile_(); 00084 startMetrics(); 00085 } 00086 00090 virtual ~FileMetric() { 00091 stopMetrics(); 00092 closeFile_(); 00093 } 00094 00099 std::string getLibName() const override { return "file"; } 00100 00107 void sendMetric_(const std::string& name, const std::string& value, const std::string& unit) override { 00108 if (!stopped_ && !inhibit_) { 00109 getTime_(outputStream_) << "FileMetric: " << name << ": " << value << " " << unit << "." << std::endl; 00110 } 00111 } 00112 00119 void sendMetric_(const std::string& name, const int& value, const std::string& unit) override { 00120 sendMetric_(name, std::to_string(value), unit); 00121 } 00122 00129 void sendMetric_(const std::string& name, const double& value, const std::string& unit) override { 00130 sendMetric_(name, std::to_string(value), unit); 00131 } 00132 00139 void sendMetric_(const std::string& name, const float& value, const std::string& unit) override { 00140 sendMetric_(name, std::to_string(value), unit); 00141 } 00142 00149 void sendMetric_(const std::string& name, const unsigned long int& value, const std::string& unit) override { 00150 sendMetric_(name, std::to_string(value), unit); 00151 } 00152 00156 void startMetrics_() override { 00157 stopped_ = false; 00158 getTime_(outputStream_) << "FileMetric plugin started." << std::endl; 00159 } 00160 00164 void stopMetrics_() override { 00165 stopped_ = true; 00166 getTime_(outputStream_) << "FileMetric plugin has been stopped!" << std::endl; 00167 } 00168 00169 private: 00170 void openFile_() { 00171 outputStream_.open(outputFile_.c_str(), mode_); 00172 getTime_(outputStream_) << "FileMetric plugin file opened." << std::endl; 00173 } 00174 00175 void closeFile_() { 00176 getTime_(outputStream_) << "FileMetric closing file stream." << std::endl; 00177 outputStream_.close(); 00178 } 00179 }; 00180 } // End namespace artdaq 00181 00182 DEFINE_ARTDAQ_METRIC(artdaq::FileMetric)