1 #include "cetlib/PluginTypeDeducer.h"
2 #include "cetlib/ostream_handle.h"
3 #include "fhiclcpp/ParameterSet.h"
5 #include "cetlib/compiler_macros.h"
6 #include "messagefacility/MessageService/ELdestination.h"
7 #include "messagefacility/Utilities/ELseverityLevel.h"
8 #include "messagefacility/Utilities/exception.h"
13 using mf::ELseverityLevel;
15 using mf::service::ELdestination;
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};
35 using Parameters = fhicl::WrappedTable<Config>;
54 virtual void routePayload(
const std::ostringstream& oss,
const ErrorObj& msg)
override;
59 virtual void flush()
override;
64 std::unordered_map<std::string, std::unique_ptr<cet::ostream_handle>> outputs_;
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()) {}
92 const auto& xid = msg.xid();
93 std::string fileName = baseDir_ +
"/";
95 fileName += xid.module() +
"-";
98 fileName += xid.id() +
"-";
100 if (useApplication_) {
101 fileName += xid.application() +
"-";
104 fileName += xid.hostname() +
"-";
106 fileName += std::to_string(xid.pid()) +
".log";
107 if (outputs_.count(fileName) == 0) {
109 std::make_unique<cet::ostream_handle>(fileName.c_str(), append_ ? std::ios::app : std::ios::trunc);
111 *outputs_[fileName] << oss.str();
116 for (
auto i = outputs_.begin(); i != outputs_.end(); ++i) {
117 (*i).second->flush();
128 #ifndef EXTERN_C_FUNC_DECLARE_START
129 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
132 EXTERN_C_FUNC_DECLARE_START
133 auto makePlugin(
const std::string&,
const fhicl::ParameterSet& pset) {
134 return std::make_unique<mfplugins::ELMultiFileOutput>(pset);
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.