artdaq_mfextensions  v1_03_06
MultiFile_mfPlugin.cc
1 #include "cetlib/PluginTypeDeducer.h"
2 #include "cetlib/ostream_handle.h"
3 #include "fhiclcpp/ParameterSet.h"
4 
5 #include "cetlib/compiler_macros.h"
6 #include "messagefacility/MessageService/ELdestination.h"
7 #include "messagefacility/Utilities/ELseverityLevel.h"
8 #include "messagefacility/Utilities/exception.h"
9 
10 #include <fstream>
11 
12 namespace mfplugins {
13 using mf::ELseverityLevel;
14 using mf::ErrorObj;
15 using mf::service::ELdestination;
16 
20 class ELMultiFileOutput : public ELdestination {
21  public:
25  struct Config {
27  fhicl::TableFragment<ELdestination::Config> elDestConfig;
29  fhicl::Atom<std::string> baseDir = fhicl::Atom<std::string>{
30  fhicl::Name{"base_directory"}, fhicl::Comment{"Directory where log files will be created"}, "/tmp"};
32  fhicl::Atom<bool> append =
33  fhicl::Atom<bool>{fhicl::Name{"append"}, fhicl::Comment{"Append to existing log files"}, true};
35  fhicl::Atom<bool> useHostname = fhicl::Atom<bool>{
36  fhicl::Name{"use_hostname"}, fhicl::Comment{"Use the hostname when generating log file names"}, true};
38  fhicl::Atom<bool> useApplication =
39  fhicl::Atom<bool>{fhicl::Name{"use_application"},
40  fhicl::Comment{"Use the application field when generating log file names"}, true};
42  fhicl::Atom<bool> useCategory = fhicl::Atom<bool>{
43  fhicl::Name{"use_category"}, fhicl::Comment{"Use the category field when generating log file names"}, false};
45  fhicl::Atom<bool> useModule = fhicl::Atom<bool>{
46  fhicl::Name{"use_module"}, fhicl::Comment{"Use the module field when generating log file names"}, false};
47  };
49  using Parameters = fhicl::WrappedTable<Config>;
50 
51  public:
56  ELMultiFileOutput(Parameters const& pset);
57 
61  virtual ~ELMultiFileOutput() {}
62 
68  virtual void routePayload(const std::ostringstream& oss, const ErrorObj& msg) override;
69 
73  virtual void flush() override;
74 
75  private:
76  std::string baseDir_;
77  bool append_;
78  std::unordered_map<std::string, std::unique_ptr<cet::ostream_handle>> outputs_;
79 
80  bool useHost_;
81  bool useApplication_;
82  bool useCategory_;
83  bool useModule_;
84 };
85 
86 // END DECLARATION
87 //======================================================================
88 // BEGIN IMPLEMENTATION
89 
90 //======================================================================
91 // ELMultiFileOutput c'tor
92 //======================================================================
94  : ELdestination(pset().elDestConfig()),
95  baseDir_(pset().baseDir()),
96  append_(pset().append()),
97  useHost_(pset().useHostname()),
98  useApplication_(pset().useApplication()),
99  useCategory_(pset().useCategory()),
100  useModule_(pset().useModule()) {}
101 
102 //======================================================================
103 // Message router ( overriddes ELdestination::routePayload )
104 //======================================================================
105 void ELMultiFileOutput::routePayload(const std::ostringstream& oss, const ErrorObj& msg) {
106  const auto& xid = msg.xid();
107  std::string fileName = baseDir_ + "/";
108  if (useModule_) {
109  fileName += xid.module() + "-";
110  }
111  if (useCategory_) {
112  fileName += xid.id() + "-";
113  }
114  if (useApplication_) {
115  fileName += xid.application() + "-";
116  }
117  if (useHost_) {
118  fileName += xid.hostname() + "-";
119  }
120  fileName += std::to_string(xid.pid()) + ".log";
121  if (outputs_.count(fileName) == 0) {
122  outputs_[fileName] =
123  std::make_unique<cet::ostream_handle>(fileName.c_str(), append_ ? std::ios::app : std::ios::trunc);
124  }
125  *outputs_[fileName] << oss.str();
126  flush();
127 }
128 
130  for (auto i = outputs_.begin(); i != outputs_.end(); ++i) {
131  (*i).second->flush();
132  }
133 }
134 } // end namespace mfplugins
135 
136 //======================================================================
137 //
138 // makePlugin function
139 //
140 //======================================================================
141 
142 #ifndef EXTERN_C_FUNC_DECLARE_START
143 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
144 #endif
145 
146 EXTERN_C_FUNC_DECLARE_START
147 auto makePlugin(const std::string&, const fhicl::ParameterSet& pset) {
148  return std::make_unique<mfplugins::ELMultiFileOutput>(pset);
149 }
150 }
151 
152 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
fhicl::Atom< bool > append
&quot;append&quot; (Default: true): Append to existing log files
Message Facility Destination which automatically opens files and sorts messages into them based on gi...
fhicl::Atom< std::string > baseDir
&quot;base_directory&quot; (Default: &quot;/tmp&quot;): Directory where log files will be created
fhicl::Atom< bool > useCategory
&quot;use_category&quot; (Default: false): Use the category field when generating log file names ...
ELMultiFileOutput(Parameters const &pset)
ELMultiFileOutput Constructor
fhicl::Atom< bool > useHostname
&quot;use_hostname&quot; (Default: true): Use the hostname when generating log file names
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation.
virtual ~ELMultiFileOutput()
Default virtual Destructor
virtual void routePayload(const std::ostringstream &oss, const ErrorObj &msg) override
Serialize a MessageFacility message to the output.
virtual void flush() override
Flush any text in the ostream buffer to disk.
Configuration parameters for ELMultiFileOutput.
fhicl::Atom< bool > useApplication
&quot;use_application&quot; (Default: true): Use the application field when generating log file names ...
fhicl::Atom< bool > useModule
&quot;use_module&quot; (Default: false): Use the module field when generating log file names ...
fhicl::TableFragment< ELdestination::Config > elDestConfig
ELdestination common config parameters.