$treeview $search $mathjax $extrastylesheet
artdaq_mfextensions
v1_03_03a
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include "cetlib/PluginTypeDeducer.h" 00002 #include "cetlib/ostream_handle.h" 00003 #include "fhiclcpp/ParameterSet.h" 00004 00005 #include "cetlib/compiler_macros.h" 00006 #include "messagefacility/MessageService/ELdestination.h" 00007 #include "messagefacility/Utilities/ELseverityLevel.h" 00008 #include "messagefacility/Utilities/exception.h" 00009 00010 #include <fstream> 00011 00012 namespace mfplugins { 00013 using mf::ELseverityLevel; 00014 using mf::ErrorObj; 00015 using mf::service::ELdestination; 00016 00020 class ELMultiFileOutput : public ELdestination { 00021 struct Config { 00022 fhicl::TableFragment<ELdestination::Config> elDestConfig; 00023 fhicl::Atom<std::string> baseDir{fhicl::Name{"base_directory"}, 00024 fhicl::Comment{"Directory where log files will be created"}, "/tmp"}; 00025 fhicl::Atom<bool> append{fhicl::Name{"append"}, fhicl::Comment{"Append to existing log files"}, true}; 00026 fhicl::Atom<bool> useHostname{fhicl::Name{"use_hostname"}, 00027 fhicl::Comment{"Use the hostname when generating log file names"}, true}; 00028 fhicl::Atom<bool> useApplication{fhicl::Name{"use_application"}, 00029 fhicl::Comment{"Use the application field when generating log file names"}, true}; 00030 fhicl::Atom<bool> useCategory{fhicl::Name{"use_category"}, 00031 fhicl::Comment{"Use the category field when generating log file names"}, false}; 00032 fhicl::Atom<bool> useModule{fhicl::Name{"use_module"}, 00033 fhicl::Comment{"Use the module field when generating log file names"}, false}; 00034 }; 00035 using Parameters = fhicl::WrappedTable<Config>; 00036 00037 public: 00042 ELMultiFileOutput(Parameters const& pset); 00043 00047 virtual ~ELMultiFileOutput() {} 00048 00054 virtual void routePayload(const std::ostringstream& oss, const ErrorObj& msg) override; 00055 00059 virtual void flush() override; 00060 00061 private: 00062 std::string baseDir_; 00063 bool append_; 00064 std::unordered_map<std::string, std::unique_ptr<cet::ostream_handle>> outputs_; 00065 00066 bool useHost_; 00067 bool useApplication_; 00068 bool useCategory_; 00069 bool useModule_; 00070 }; 00071 00072 // END DECLARATION 00073 //====================================================================== 00074 // BEGIN IMPLEMENTATION 00075 00076 //====================================================================== 00077 // ELMultiFileOutput c'tor 00078 //====================================================================== 00079 ELMultiFileOutput::ELMultiFileOutput(Parameters const& pset) 00080 : ELdestination(pset().elDestConfig()), 00081 baseDir_(pset().baseDir()), 00082 append_(pset().append()), 00083 useHost_(pset().useHostname()), 00084 useApplication_(pset().useApplication()), 00085 useCategory_(pset().useCategory()), 00086 useModule_(pset().useModule()) {} 00087 00088 //====================================================================== 00089 // Message router ( overriddes ELdestination::routePayload ) 00090 //====================================================================== 00091 void ELMultiFileOutput::routePayload(const std::ostringstream& oss, const ErrorObj& msg) { 00092 const auto& xid = msg.xid(); 00093 std::string fileName = baseDir_ + "/"; 00094 if (useModule_) { 00095 fileName += xid.module() + "-"; 00096 } 00097 if (useCategory_) { 00098 fileName += xid.id() + "-"; 00099 } 00100 if (useApplication_) { 00101 fileName += xid.application() + "-"; 00102 } 00103 if (useHost_) { 00104 fileName += xid.hostname() + "-"; 00105 } 00106 fileName += std::to_string(xid.pid()) + ".log"; 00107 if (outputs_.count(fileName) == 0) { 00108 outputs_[fileName] = 00109 std::make_unique<cet::ostream_handle>(fileName.c_str(), append_ ? std::ios::app : std::ios::trunc); 00110 } 00111 *outputs_[fileName] << oss.str(); 00112 flush(); 00113 } 00114 00115 void ELMultiFileOutput::flush() { 00116 for (auto i = outputs_.begin(); i != outputs_.end(); ++i) { 00117 (*i).second->flush(); 00118 } 00119 } 00120 } // end namespace mfplugins 00121 00122 //====================================================================== 00123 // 00124 // makePlugin function 00125 // 00126 //====================================================================== 00127 00128 #ifndef EXTERN_C_FUNC_DECLARE_START 00129 #define EXTERN_C_FUNC_DECLARE_START extern "C" { 00130 #endif 00131 00132 EXTERN_C_FUNC_DECLARE_START 00133 auto makePlugin(const std::string&, const fhicl::ParameterSet& pset) { 00134 return std::make_unique<mfplugins::ELMultiFileOutput>(pset); 00135 } 00136 } 00137 00138 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)