7 #define TRACE_NAME "FileMetric"
10 #include "artdaq-utilities/Plugins/MetricMacros.hh"
11 #include "fhiclcpp/ParameterSet.h"
13 #include <sys/types.h>
15 #include <boost/filesystem.hpp>
20 namespace BFS = boost::filesystem;
29 std::string outputFile_;
30 bool file_name_is_absolute_path_;
31 std::string relative_env_var_;
32 bool uniquify_file_name_;
33 std::ofstream outputStream_;
34 std::ios_base::openmode mode_;
35 std::string timeformat_;
38 std::ostream& getTime_(std::ostream& stream)
40 std::time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
42 struct std::tm* ptm = std::localtime(&tt);
43 if (timeformat_.size())
45 return stream << std::put_time(ptm, timeformat_.c_str()) <<
": ";
66 explicit FileMetric(fhicl::ParameterSet
const& config, std::string
const& app_name)
68 , outputFile_(
pset.get<std::string>(
"fileName",
"FileMetric.out"))
69 , file_name_is_absolute_path_(
pset.get<bool>(
"absolute_file_path", true))
70 , relative_env_var_(
pset.get<std::string>(
"relative_directory_env_var",
"ARTDAQ_LOG_ROOT"))
71 , uniquify_file_name_(
pset.get<bool>(
"uniquify", false))
72 , timeformat_(
pset.get<std::string>(
"time_format",
"%c"))
75 std::string modeString =
pset.get<std::string>(
"fileMode",
"append");
77 mode_ = std::ofstream::out | std::ofstream::app;
78 if (modeString ==
"Overwrite" || modeString ==
"Create" || modeString ==
"Write")
80 mode_ = std::ofstream::out | std::ofstream::trunc;
83 if (uniquify_file_name_)
85 std::string unique_id = std::to_string(getpid());
86 if (outputFile_.find(
"%UID%") != std::string::npos)
88 outputFile_ = outputFile_.replace(outputFile_.find(
"%UID%"), 5, unique_id);
92 if (outputFile_.rfind(
".") != std::string::npos)
94 outputFile_ = outputFile_.insert(outputFile_.rfind(
"."),
"_" + unique_id);
98 outputFile_ = outputFile_.append(
"_" + unique_id);
127 void sendMetric_(
const std::string& name,
const std::string& value,
const std::string& unit)
override
131 getTime_(outputStream_) <<
"FileMetric: " << name <<
": " << value <<
" " << unit <<
"." << std::endl;
141 void sendMetric_(
const std::string& name,
const int& value,
const std::string& unit)
override
152 void sendMetric_(
const std::string& name,
const double& value,
const std::string& unit)
override
163 void sendMetric_(
const std::string& name,
const float& value,
const std::string& unit)
override
174 void sendMetric_(
const std::string& name,
const unsigned long int& value,
const std::string& unit)
override
185 getTime_(outputStream_) <<
"FileMetric plugin started." << std::endl;
194 getTime_(outputStream_) <<
"FileMetric plugin has been stopped!" << std::endl;
200 if (!file_name_is_absolute_path_)
202 TLOG(TLVL_DEBUG) <<
"Reading relative directory evironment variable " << relative_env_var_;
203 std::string logPathProblem =
"";
204 std::string logfileName =
"";
205 char* logRootString = getenv(relative_env_var_.c_str());
207 std::string logfileDir =
"";
208 if (logRootString !=
nullptr)
210 if (!BFS::exists(logRootString))
212 TLOG(TLVL_WARNING) <<
"Relative directory environment variable " << relative_env_var_ <<
" points to a non-existant directory! Using /tmp/!";
213 outputFile_ =
"/tmp/" + outputFile_;
214 TLOG(TLVL_INFO) <<
"FileMetric Opening file " << outputFile_;
215 outputStream_.open(outputFile_, mode_);
219 logfileDir = logRootString;
220 logfileDir.append(
"/metrics/");
222 while (outputFile_.find(
'/') != std::string::npos)
224 TLOG(TLVL_DEBUG) <<
"Extracting subdirectories from relative file path " << outputFile_ <<
" (logfileDir = " << logfileDir <<
")";
225 logfileDir.append(outputFile_.substr(0, outputFile_.find(
'/') + 1));
226 outputFile_.erase(0, outputFile_.find(
'/') + 1);
231 TLOG(TLVL_DEBUG) <<
"Creating log file directory " << logfileDir;
232 if (!BFS::exists(logfileDir))
233 BFS::create_directories(logfileDir);
235 logfileName.append(logfileDir);
236 logfileName.append(outputFile_);
238 TLOG(TLVL_INFO) <<
"FileMetric Opening file " << logfileName;
239 outputStream_.open(logfileName, mode_);
244 TLOG(TLVL_WARNING) <<
"Relative directory environment variable " << relative_env_var_ <<
" is null! Using /tmp/!";
245 outputFile_ =
"/tmp/" + outputFile_;
246 TLOG(TLVL_INFO) <<
"FileMetric Opening file " << outputFile_;
247 outputStream_.open(outputFile_, mode_);
252 TLOG(TLVL_INFO) <<
"FileMetric Opening file " << outputFile_;
253 outputStream_.open(outputFile_, mode_);
255 if (outputStream_.is_open())
257 getTime_(outputStream_) <<
"FileMetric plugin file opened." << std::endl;
261 TLOG(TLVL_ERROR) <<
"Error opening metric file " << outputFile_;
267 getTime_(outputStream_) <<
"FileMetric closing file stream." << std::endl;
268 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.
The MetricPlugin class defines the interface that MetricManager uses to send metric data to the vario...
void sendMetric_(const std::string &name, const double &value, const std::string &unit) override
Write metric data to a file.
void startMetrics()
Perform startup actions. Simply calls the virtual startMetrics_ function.
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 stopMetrics()
Perform shutdown actions. Zeroes out all accumulators, and sends zeros for each metric. Calls stopMetrics_() for any plugin-defined shutdown actions.
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_
Flag to indicate that the MetricPlugin is being stopped, and any metric back-ends which do not have a...