1 #define TRACE_NAME "GenFileOutput"
3 #include "cetlib/PluginTypeDeducer.h"
4 #include "cetlib/ostream_handle.h"
5 #include "cetlib/ProvideMakePluginMacros.h"
6 #include "fhiclcpp/ParameterSet.h"
8 #include "boost/date_time/posix_time/posix_time.hpp"
10 #include "messagefacility/MessageService/ELdestination.h"
11 # include "messagefacility/Utilities/ELseverityLevel.h"
12 #include "messagefacility/Utilities/exception.h"
19 using mf::service::ELdestination;
20 using mf::ELseverityLevel;
31 fhicl::TableFragment<ELdestination::Config> elDestConfig;
32 fhicl::Atom<bool> append{ fhicl::Name{
"append"},fhicl::Comment {
"Whether to append to the file or recreate it"},
true };
33 fhicl::Atom<std::string> baseDir{ fhicl::Name{
"directory"},fhicl::Comment{
"The directory into which files will be saved"},
"/tmp" };
34 fhicl::Atom<std::string> sep{ fhicl::Name{
"seperator"},fhicl::Comment{
"Separator to use after optional replacement parameters"},
"-" };
35 fhicl::Atom<std::string> timePattern{ fhicl::Name{
"timestamp_pattern"},fhicl::Comment{
"Pattern to use for %t strftime replacement"},
"%Y%m%d%H%M%S" };
36 fhicl::Atom<std::string> filePattern{ fhicl::Name{
"pattern" },fhicl::Comment{
"Pattern to use for file naming.\n"
37 " Supported parameters are:\n"
38 " %%: Print a % sign\n"
39 " %N: Print the executable name, as retrieved from /proc/<pid>/exe\n"
40 " %?N: Print the executable name only if it does not already appear in the parsed format. Format is parsed left-to-right.\n"
41 " These options add a seperator AFTER if they are filled and if they are not the last token in the file pattern, before the last '.' character.\n"
42 " %H: Print the hostname, without any domain specifiers (i.e. work.fnal.gov will become work)\n"
43 " %?H: Print the hostname only if it does not already appear in the parsed format.\n"
44 " %p: Print the PID of the application configuring MessageFacility\n"
45 " %t: Print the timestamp using the format specified by timestamp_pattern\n"
46 " %T: Print the timestamp in ISO format"
47 },
"%N-%?H%t-%p.log" };
51 using Parameters = fhicl::WrappedTable<Config>;
68 virtual void routePayload(
const std::ostringstream& o,
const ErrorObj& e)
override;
73 virtual void flush()
override;
76 std::unique_ptr<cet::ostream_handle> output_;
90 bool append = pset().append();
91 std::string baseDir = pset().baseDir();
92 std::string sep = pset().sep();
93 std::string timePattern = pset().timePattern();
94 std::string filePattern = pset().filePattern();
97 std::string exeString =
"";
98 std::string hostString =
"";
99 std::string timeBuffISO =
"";
100 std::string timeBuff =
"";
104 if (filePattern.find(
"%N") != std::string::npos || filePattern.find(
"%?N") != std::string::npos)
108 std::ostringstream pid_ostr;
109 pid_ostr <<
"/proc/" << pid <<
"/exe";
110 exe = std::string(realpath(pid_ostr.str().c_str(), NULL));
112 size_t end = exe.find(
'\0');
113 size_t start = exe.find_last_of(
'/', end);
114 exeString = exe.substr(start + 1, end - start - 1);
118 if (filePattern.find(
"%H") != std::string::npos || filePattern.find(
"%?H") != std::string::npos)
121 if (gethostname(&hostname[0], 256) == 0)
123 std::string tmpString(hostname);
124 hostString = tmpString;
125 size_t pos = hostString.find(
".");
126 if (pos != std::string::npos && pos > 2)
128 hostString = hostString.substr(0, pos);
132 if (filePattern.find(
"%T") != std::string::npos)
134 timeBuffISO = boost::posix_time::to_iso_string(boost::posix_time::second_clock::universal_time());
136 if (filePattern.find(
"%t") != std::string::npos)
143 timeinfo = localtime(&rawtime);
144 strftime(timeBuffC, 256, timePattern.c_str(), timeinfo);
145 timeBuff = std::string(timeBuffC);
149 TLOG(TLVL_DEBUG) <<
"filePattern is: " << filePattern;
150 while (filePattern.find(
"%", pos) != std::string::npos)
152 pos = filePattern.find(
"%", pos) + 1;
153 TLOG(5,1) <<
"Found % at " << (pos - 1) <<
", next char: " << filePattern[pos] <<
".";
154 switch (filePattern[pos])
157 filePattern = filePattern.replace(pos - 1, 2,
"%");
162 char next = filePattern[pos + 1];
166 if (filePattern.find(exeString) != std::string::npos)
168 filePattern = filePattern.erase(pos - 1, 3);
172 std::string repString = exeString;
174 if (!(pos + 1 == filePattern.size() - 1 || pos + 2 == filePattern.find_last_of(
'.')))
178 filePattern = filePattern.replace(pos - 1, 3, repString);
182 if (filePattern.find(hostString) != std::string::npos)
184 filePattern = filePattern.erase(pos - 1, 3);
188 std::string repString = hostString;
190 if (!(pos + 1 == filePattern.size() - 1 || pos + 2 == filePattern.find_last_of(
'.')))
194 filePattern = filePattern.replace(pos - 1, 3, repString);
202 filePattern = filePattern.replace(pos - 1, 2, exeString);
206 filePattern = filePattern.replace(pos - 1, 2, hostString);
210 filePattern = filePattern.replace(pos - 1, 2, std::to_string(pid));
214 filePattern = filePattern.replace(pos - 1, 2, timeBuff);
218 filePattern = filePattern.replace(pos - 1, 2, timeBuffISO);
222 TLOG(6) <<
"filePattern is now: " << filePattern;
224 std::string fileName = baseDir +
"/" + filePattern;
225 TLOG(TLVL_DEBUG) <<
"fileName is: " << fileName;
227 output_ = std::make_unique<cet::ostream_handle>(fileName.c_str(), append ? std::ios::app : std::ios::trunc);
236 *output_ << oss.str();
254 MAKE_PLUGIN_START(
auto, std::string
const&, fhicl::ParameterSet
const& pset)
256 return std::make_unique<mfplugins::ELGenFileOutput>(pset);
260 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
virtual void routePayload(const std::ostringstream &o, const ErrorObj &e) override
Serialize a MessageFacility message to the output stream.
Message Facility destination which generates the output file name based on some combination of PID...
ELGenFileOutput(Parameters const &pset)
ELGenFileOutput Constructor.
virtual void flush() override
Flush any text in the ostream buffer to disk.
virtual ~ELGenFileOutput()
Default virtual destructor.