artdaq_mfextensions  v1_03_03
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  struct Config {
22  fhicl::TableFragment<ELdestination::Config> elDestConfig;
23  fhicl::Atom<std::string> baseDir{fhicl::Name{"base_directory"},
24  fhicl::Comment{"Directory where log files will be created"}, "/tmp"};
25  fhicl::Atom<bool> append{fhicl::Name{"append"}, fhicl::Comment{"Append to existing log files"}, true};
26  fhicl::Atom<bool> useHostname{fhicl::Name{"use_hostname"},
27  fhicl::Comment{"Use the hostname when generating log file names"}, true};
28  fhicl::Atom<bool> useApplication{fhicl::Name{"use_application"},
29  fhicl::Comment{"Use the application field when generating log file names"}, true};
30  fhicl::Atom<bool> useCategory{fhicl::Name{"use_category"},
31  fhicl::Comment{"Use the category field when generating log file names"}, false};
32  fhicl::Atom<bool> useModule{fhicl::Name{"use_module"},
33  fhicl::Comment{"Use the module field when generating log file names"}, false};
34  };
35  using Parameters = fhicl::WrappedTable<Config>;
36 
37  public:
42  ELMultiFileOutput(Parameters const& pset);
43 
47  virtual ~ELMultiFileOutput() {}
48 
54  virtual void routePayload(const std::ostringstream& oss, const ErrorObj& msg) override;
55 
59  virtual void flush() override;
60 
61  private:
62  std::string baseDir_;
63  bool append_;
64  std::unordered_map<std::string, std::unique_ptr<cet::ostream_handle>> outputs_;
65 
66  bool useHost_;
67  bool useApplication_;
68  bool useCategory_;
69  bool useModule_;
70 };
71 
72 // END DECLARATION
73 //======================================================================
74 // BEGIN IMPLEMENTATION
75 
76 //======================================================================
77 // ELMultiFileOutput c'tor
78 //======================================================================
79 ELMultiFileOutput::ELMultiFileOutput(Parameters const& pset)
80  : ELdestination(pset().elDestConfig()),
81  baseDir_(pset().baseDir()),
82  append_(pset().append()),
83  useHost_(pset().useHostname()),
84  useApplication_(pset().useApplication()),
85  useCategory_(pset().useCategory()),
86  useModule_(pset().useModule()) {}
87 
88 //======================================================================
89 // Message router ( overriddes ELdestination::routePayload )
90 //======================================================================
91 void ELMultiFileOutput::routePayload(const std::ostringstream& oss, const ErrorObj& msg) {
92  const auto& xid = msg.xid();
93  std::string fileName = baseDir_ + "/";
94  if (useModule_) {
95  fileName += xid.module() + "-";
96  }
97  if (useCategory_) {
98  fileName += xid.id() + "-";
99  }
100  if (useApplication_) {
101  fileName += xid.application() + "-";
102  }
103  if (useHost_) {
104  fileName += xid.hostname() + "-";
105  }
106  fileName += std::to_string(xid.pid()) + ".log";
107  if (outputs_.count(fileName) == 0) {
108  outputs_[fileName] =
109  std::make_unique<cet::ostream_handle>(fileName.c_str(), append_ ? std::ios::app : std::ios::trunc);
110  }
111  *outputs_[fileName] << oss.str();
112  flush();
113 }
114 
116  for (auto i = outputs_.begin(); i != outputs_.end(); ++i) {
117  (*i).second->flush();
118  }
119 }
120 } // end namespace mfplugins
121 
122 //======================================================================
123 //
124 // makePlugin function
125 //
126 //======================================================================
127 
128 #ifndef EXTERN_C_FUNC_DECLARE_START
129 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
130 #endif
131 
132 EXTERN_C_FUNC_DECLARE_START
133 auto makePlugin(const std::string&, const fhicl::ParameterSet& pset) {
134  return std::make_unique<mfplugins::ELMultiFileOutput>(pset);
135 }
136 }
137 
138 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
Message Facility Destination which automatically opens files and sorts messages into them based on gi...
ELMultiFileOutput(Parameters const &pset)
ELMultiFileOutput Constructor
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.