artdaq_core  v3_07_02
GenFile_mfPlugin.cc
1 
2 #include "tracemf.h"
3 #define TRACE_NAME "GenFileOutput"
4 
5 #include <boost/date_time/posix_time/posix_time.hpp>
6 #include <fstream>
7 #include "cetlib/PluginTypeDeducer.h"
8 #include "cetlib/ProvideMakePluginMacros.h"
9 #include "cetlib/ostream_handle.h"
10 #include "fhiclcpp/ParameterSet.h"
11 #include "fhiclcpp/types/ConfigurationTable.h"
12 #include "messagefacility/MessageService/ELdestination.h"
13 #include "messagefacility/Utilities/ELseverityLevel.h"
14 #include "messagefacility/Utilities/exception.h"
15 
16 namespace mfplugins {
17 using mf::ErrorObj;
18 using mf::service::ELdestination;
19 
24 class ELGenFileOutput : public ELdestination
25 {
26 public:
30  struct Config
31  {
33  fhicl::TableFragment<ELdestination::Config> elDestConfig;
35  fhicl::Atom<bool> append = fhicl::Atom<bool>{
36  fhicl::Name{"append"}, fhicl::Comment{"Whether to append to the file or recreate it"}, true};
38  fhicl::Atom<std::string> baseDir = fhicl::Atom<std::string>{
39  fhicl::Name{"directory"}, fhicl::Comment{"The directory into which files will be saved"}, "/tmp"};
41  fhicl::Atom<std::string> sep = fhicl::Atom<std::string>{
42  fhicl::Name{"seperator"}, fhicl::Comment{"Separator to use after optional replacement parameters"}, "-"};
44  fhicl::Atom<std::string> timePattern = fhicl::Atom<std::string>{
45  fhicl::Name{"timestamp_pattern"}, fhicl::Comment{"Pattern to use for %t strftime replacement"}, "%Y%m%d%H%M%S"};
60  fhicl::Atom<std::string> filePattern = fhicl::Atom<std::string>{fhicl::Name{"pattern"}, fhicl::Comment{"Pattern to use for file naming.\n"
61  " Supported parameters are:\n"
62  " %%: Print a % sign\n"
63  " %N: Print the executable name, as retrieved from /proc/<pid>/exe\n"
64  " %?N: Print the executable name only if it does not already appear in the parsed format. "
65  "Format is parsed left-to-right.\n"
66  " These options add a seperator AFTER if they are filled and if they are not the last token in "
67  "the file pattern, before the last '.' character.\n"
68  " %H: Print the hostname, without any domain specifiers (i.e. work.fnal.gov will become work)\n"
69  " %?H: Print the hostname only if it does not already appear in the parsed format.\n"
70  " %p: Print the PID of the application configuring MessageFacility\n"
71  " %t: Print the timestamp using the format specified by timestamp_pattern\n"
72  " %T: Print the timestamp in ISO format"},
73  "%N-%?H%t-%p.log"};
74  };
76  using Parameters = fhicl::WrappedTable<Config>;
77 
78 public:
83  explicit ELGenFileOutput(Parameters const& pset);
84 
90  void routePayload(const std::ostringstream& o, const ErrorObj& e) override;
91 
95  void flush() override;
96 
97 private:
98  std::unique_ptr<cet::ostream_handle> output_;
99 };
100 
101 // END DECLARATION
102 //======================================================================
103 // BEGIN IMPLEMENTATION
104 
105 //======================================================================
106 // ELGenFileOutput c'tor
107 //======================================================================
108 
110  : ELdestination(pset().elDestConfig())
111 {
112  bool append = pset().append();
113  std::string baseDir = pset().baseDir();
114  std::string sep = pset().sep();
115  std::string timePattern = pset().timePattern();
116  std::string filePattern = pset().filePattern();
117 
118  auto pid = getpid();
119  std::string exeString;
120  std::string hostString;
121  std::string timeBuffISO; // Using boost::posix_time::to_iso_string (%T)
122  std::string timeBuff; // Using timestamp_pattern (%t)
123 
124  // Determine image name
125  if (filePattern.find("%N") != std::string::npos || filePattern.find("%?N") != std::string::npos)
126  {
127  // get process name from '/proc/pid/exe'
128  std::string exe;
129  std::ostringstream pid_ostr;
130  pid_ostr << "/proc/" << pid << "/exe";
131  exe = std::string(realpath(pid_ostr.str().c_str(), nullptr));
132 
133  size_t end = exe.find('\0');
134  size_t start = exe.find_last_of('/', end);
135  exeString = exe.substr(start + 1, end - start - 1);
136  }
137 
138  // Get Host name
139  if (filePattern.find("%H") != std::string::npos || filePattern.find("%?H") != std::string::npos)
140  {
141  char hostname[256];
142  if (gethostname(&hostname[0], 256) == 0)
143  {
144  std::string tmpString(hostname);
145  hostString = tmpString;
146  size_t pos = hostString.find('.');
147  if (pos != std::string::npos && pos > 2)
148  {
149  hostString = hostString.substr(0, pos);
150  }
151  }
152  }
153  if (filePattern.find("%T") != std::string::npos)
154  {
155  timeBuffISO = boost::posix_time::to_iso_string(boost::posix_time::second_clock::universal_time());
156  }
157  if (filePattern.find("%t") != std::string::npos)
158  {
159  time_t rawtime;
160  struct tm* timeinfo;
161  char timeBuffC[256];
162  time(&rawtime);
163  timeinfo = localtime(&rawtime);
164  strftime(timeBuffC, 256, timePattern.c_str(), timeinfo);
165  timeBuff = std::string(timeBuffC);
166  }
167 
168  size_t pos = 0;
169  TLOG(TLVL_DEBUG) << "filePattern is: " << filePattern;
170  while (filePattern.find('%', pos) != std::string::npos)
171  {
172  pos = filePattern.find('%', pos) + 1;
173  TLOG(5, 1) << "Found % at " << (pos - 1) << ", next char: " << filePattern[pos] << ".";
174  switch (filePattern[pos])
175  {
176  case '%': // "%%"
177  filePattern = filePattern.replace(pos - 1, 2, "%");
178  pos--;
179  break;
180  case '?':
181  {
182  char next = filePattern[pos + 1];
183  switch (next)
184  {
185  case 'N':
186  if (filePattern.find(exeString) != std::string::npos)
187  {
188  filePattern = filePattern.erase(pos - 1, 3);
189  }
190  else
191  {
192  std::string repString = exeString;
193  // Only append separator if we're not at the end of the pattern
194  if (!(pos + 1 == filePattern.size() - 1 || pos + 2 == filePattern.find_last_of('.')))
195  {
196  repString += sep;
197  }
198  filePattern = filePattern.replace(pos - 1, 3, repString);
199  }
200  break;
201  case 'H':
202  if (filePattern.find(hostString) != std::string::npos)
203  {
204  filePattern = filePattern.erase(pos - 1, 3);
205  }
206  else
207  {
208  std::string repString = hostString;
209  // Only append separator if we're not at the end of the pattern
210  if (!(pos + 1 == filePattern.size() - 1 || pos + 2 == filePattern.find_last_of('.')))
211  {
212  repString += sep;
213  }
214  filePattern = filePattern.replace(pos - 1, 3, repString);
215  }
216  break;
217  default:
218  pos += 3;
219  break;
220  }
221  pos -= 3;
222  }
223  break;
224  case 'N':
225  filePattern = filePattern.replace(pos - 1, 2, exeString);
226  pos -= 2;
227  break;
228  case 'H':
229  filePattern = filePattern.replace(pos - 1, 2, hostString);
230  pos -= 2;
231  break;
232  case 'p':
233  filePattern = filePattern.replace(pos - 1, 2, std::to_string(pid));
234  pos -= 2;
235  break;
236  case 't':
237  filePattern = filePattern.replace(pos - 1, 2, timeBuff);
238  pos -= 2;
239  break;
240  case 'T':
241  filePattern = filePattern.replace(pos - 1, 2, timeBuffISO);
242  pos -= 2;
243  break;
244  default:
245  break;
246  }
247  TLOG(TLVL_DEBUG + 3) << "filePattern is now: " << filePattern;
248  }
249  std::string fileName = baseDir + "/" + filePattern;
250  TLOG(TLVL_DEBUG) << "fileName is: " << fileName;
251 
252  output_ = std::make_unique<cet::ostream_handle>(fileName.c_str(), append ? std::ios::app : std::ios::trunc);
253 }
254 
255 //======================================================================
256 // Message router ( overriddes ELdestination::routePayload )
257 //======================================================================
258 void ELGenFileOutput::routePayload(const std::ostringstream& oss, const ErrorObj& /*msg*/)
259 {
260  *output_ << oss.str();
261  flush();
262 }
263 
265 {
266  output_->flush();
267 }
268 } // end namespace mfplugins
269 
270 //======================================================================
271 //
272 // makePlugin function
273 //
274 //======================================================================
275 
276 extern "C" {
277 MAKE_PLUGIN_START(auto, std::string const&, fhicl::ParameterSet const& pset)
278 {
279  return std::make_unique<mfplugins::ELGenFileOutput>(pset);
280 }
281 MAKE_PLUGIN_END
282 }
283 
284 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
fhicl::Atom< std::string > baseDir
&quot;directory&quot; (Default: &quot;/tmp&quot;): The directory into which files will be saved
void routePayload(const std::ostringstream &o, const ErrorObj &e) override
Serialize a MessageFacility message to the output stream.
fhicl::TableFragment< ELdestination::Config > elDestConfig
ELDestination common configuration parameters.
Message Facility destination which generates the output file name based on some combination of PID...
fhicl::Atom< std::string > sep
&quot;seperator&quot; (Default: &quot;-&quot;): Separator to use after optional replacement parameters ...
Parameters used to configure GenFileOutput.
ELGenFileOutput(Parameters const &pset)
ELGenFileOutput Constructor.
void flush() override
Flush any text in the ostream buffer to disk.
fhicl::Atom< bool > append
&quot;append&quot; (Default: true"): Whether to append to the file or recreate it
fhicl::Atom< std::string > filePattern
&quot;pattern&quot; (Default: &quot;%N-%?H%t-%p.log&quot;): Pattern to use for file naming.
fhicl::Atom< std::string > timePattern
&quot;timestamp_pattern&quot; (Default: &quot;%Y%m%d%H%M%S&quot;): Pattern to use for t strftime replacement ...
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation.