artdaq_mfextensions  v1_05_00
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::ErrorObj;
14 using mf::service::ELdestination;
15 
19 class ELMultiFileOutput : public ELdestination
20 {
21 public:
25  struct Config
26  {
28  fhicl::TableFragment<ELdestination::Config> elDestConfig;
30  fhicl::Atom<std::string> baseDir = fhicl::Atom<std::string>{
31  fhicl::Name{"base_directory"}, fhicl::Comment{"Directory where log files will be created"}, "/tmp"};
33  fhicl::Atom<bool> append =
34  fhicl::Atom<bool>{fhicl::Name{"append"}, fhicl::Comment{"Append to existing log files"}, true};
36  fhicl::Atom<bool> useHostname = fhicl::Atom<bool>{
37  fhicl::Name{"use_hostname"}, fhicl::Comment{"Use the hostname when generating log file names"}, true};
39  fhicl::Atom<bool> useApplication =
40  fhicl::Atom<bool>{fhicl::Name{"use_application"},
41  fhicl::Comment{"Use the application field when generating log file names"}, true};
43  fhicl::Atom<bool> useCategory = fhicl::Atom<bool>{
44  fhicl::Name{"use_category"}, fhicl::Comment{"Use the category field when generating log file names"}, false};
46  fhicl::Atom<bool> useModule = fhicl::Atom<bool>{
47  fhicl::Name{"use_module"}, fhicl::Comment{"Use the module field when generating log file names"}, false};
48  };
50  using Parameters = fhicl::WrappedTable<Config>;
51 
52 public:
57  explicit ELMultiFileOutput(Parameters const& pset);
58 
62  ~ELMultiFileOutput() override = default;
63 
69  void routePayload(const std::ostringstream& oss, const ErrorObj& msg) override;
70 
74  void flush() override;
75 
76 private:
77  ELMultiFileOutput(ELMultiFileOutput const&) = delete;
79  ELMultiFileOutput& operator=(ELMultiFileOutput const&) = delete;
80  ELMultiFileOutput& operator=(ELMultiFileOutput&&) = delete;
81 
82  std::string baseDir_;
83  bool append_;
84  std::unordered_map<std::string, std::unique_ptr<cet::ostream_handle>> outputs_;
85 
86  bool useHost_;
87  bool useApplication_;
88  bool useCategory_;
89  bool useModule_;
90 };
91 
92 // END DECLARATION
93 //======================================================================
94 // BEGIN IMPLEMENTATION
95 
96 //======================================================================
97 // ELMultiFileOutput c'tor
98 //======================================================================
100  : ELdestination(pset().elDestConfig()), baseDir_(pset().baseDir()), append_(pset().append()), useHost_(pset().useHostname()), useApplication_(pset().useApplication()), useCategory_(pset().useCategory()), useModule_(pset().useModule()) {}
101 
102 //======================================================================
103 // Message router ( overriddes ELdestination::routePayload )
104 //======================================================================
105 void ELMultiFileOutput::routePayload(const std::ostringstream& oss, const ErrorObj& msg)
106 {
107  const auto& xid = msg.xid();
108  std::string fileName = baseDir_ + "/";
109  if (useModule_)
110  {
111  fileName += xid.module() + "-";
112  }
113  if (useCategory_)
114  {
115  fileName += xid.id() + "-";
116  }
117  if (useApplication_)
118  {
119  fileName += xid.application() + "-";
120  }
121  if (useHost_)
122  {
123  fileName += xid.hostname() + "-";
124  }
125  fileName += std::to_string(xid.pid()) + ".log";
126  if (outputs_.count(fileName) == 0)
127  {
128  outputs_[fileName] =
129  std::make_unique<cet::ostream_handle>(fileName.c_str(), append_ ? std::ios::app : std::ios::trunc);
130  }
131  *outputs_[fileName] << oss.str();
132  flush();
133 }
134 
136 {
137  for (auto& output : outputs_)
138  {
139  output.second->flush();
140  }
141 }
142 } // end namespace mfplugins
143 
144 //======================================================================
145 //
146 // makePlugin function
147 //
148 //======================================================================
149 
150 #ifndef EXTERN_C_FUNC_DECLARE_START
151 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
152 #endif
153 
154 EXTERN_C_FUNC_DECLARE_START
155 auto makePlugin(const std::string& /*unused*/, const fhicl::ParameterSet& pset)
156 {
157  return std::make_unique<mfplugins::ELMultiFileOutput>(pset);
158 }
159 }
160 
161 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.
void routePayload(const std::ostringstream &oss, const ErrorObj &msg) override
Serialize a MessageFacility message to the output.
~ELMultiFileOutput() override=default
Default virtual Destructor
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.