00001 #include "cetlib/PluginTypeDeducer.h"
00002 #include "cetlib/ostream_handle.h"
00003 #include "fhiclcpp/ParameterSet.h"
00004
00005 #include "boost/date_time/posix_time/posix_time.hpp"
00006
00007 #include "messagefacility/MessageService/ELdestination.h"
00008 # include "messagefacility/Utilities/ELseverityLevel.h"
00009 #if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
00010 # include "messagefacility/MessageService/ELcontextSupplier.h"
00011 # include "messagefacility/MessageLogger/MessageDrop.h"
00012 #endif
00013 #include "messagefacility/Utilities/exception.h"
00014
00015 #include <fstream>
00016 #include "trace.h"
00017
00018 namespace mfplugins
00019 {
00020 using mf::service::ELdestination;
00021 using mf::ELseverityLevel;
00022 using mf::ErrorObj;
00023 #if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
00024 using mf::service::ELcontextSupplier;
00025 #endif
00026
00031 class ELGenFileOutput : public ELdestination
00032 {
00033 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
00034 struct Config
00035 {
00036 fhicl::TableFragment<ELdestination::Config> elDestConfig;
00037 fhicl::Atom<bool> append{ fhicl::Name{"append"},fhicl::Comment {"Whether to append to the file or recreate it"},true };
00038 fhicl::Atom<std::string> baseDir{ fhicl::Name{"directory"},fhicl::Comment{"The directory into which files will be saved"},"/tmp" };
00039 fhicl::Atom<std::string> sep{ fhicl::Name{"separator"},fhicl::Comment{"Separator to use after optional replacement parameters"}, "-" };
00040 fhicl::Atom<std::string> timePattern{ fhicl::Name{"timestamp_pattern"},fhicl::Comment{"Pattern to use for %t strftime replacement"},"%Y%m%d%H%M%S" };
00041 fhicl::Atom<std::string> filePattern{ fhicl::Name{ "pattern" },fhicl::Comment{ "Pattern to use for file naming.\n"
00042 " Supported parameters are:\n"
00043 " %%: Print a % sign\n"
00044 " %N: Print the executable name, as retrieved from /proc/<pid>/exe\n"
00045 " %?N: Print the executable name only if it does not already appear in the parsed format. Format is parsed left-to-right.\n"
00046 " These options add a separator AFTER if they are filled and if they are not the last token in the file pattern, before the last '.' character.\n"
00047 " %H: Print the hostname, without any domain specifiers (i.e. work.fnal.gov will become work)\n"
00048 " %?H: Print the hostname only if it does not already appear in the parsed format.\n"
00049 " %p: Print the PID of the application configuring MessageFacility\n"
00050 " %t: Print the timestamp using the format specified by timestamp_pattern\n"
00051 " %T: Print the timestamp in ISO format"
00052 },"%N-%?H%t-%p.log" };
00053
00054
00055 };
00056 using Parameters = fhicl::WrappedTable<Config>;
00057 #endif
00058
00059 public:
00060 #if MESSAGEFACILITY_HEX_VERSION < 0x20103 // v2_01_03 is s58, pre v2_01_03 is s50
00061 ELGenFileOutput(const fhicl::ParameterSet& pset);
00062 #else
00063 ELGenFileOutput(Parameters const& pset);
00064 #endif
00065
00066 virtual ~ELGenFileOutput() {}
00067
00068 virtual void routePayload(const std::ostringstream&, const ErrorObj&
00069 # if MESSAGEFACILITY_HEX_VERSION < 0x20002
00070 , const ELcontextSupplier&
00071 #endif
00072 ) override;
00073
00074 virtual void flush(
00075 # if MESSAGEFACILITY_HEX_VERSION < 0x20002
00076 const ELcontextSupplier&
00077 #endif
00078 ) override;
00079
00080 private:
00081 std::unique_ptr<cet::ostream_handle> output_;
00082 };
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
00093 ELGenFileOutput::ELGenFileOutput(const fhicl::ParameterSet& pset)
00094 : ELdestination(pset)
00095 {
00096 bool append = pset.get<bool>("append", true);
00097 std::string baseDir = pset.get<std::string>("directory", "/tmp");
00098 std::string sep = pset.get<std::string>("separator", "-");
00099 std::string timePattern = pset.get<std::string>("timestamp_pattern", "%Y%m%d%H%M%S");
00100 std::string filePattern = pset.get<std::string>("pattern", "%N-%?H%t-%p.log");
00101 #else
00102 ELGenFileOutput::ELGenFileOutput(Parameters const& pset) : ELdestination(pset().elDestConfig())
00103 {
00104 bool append = pset().append();
00105 std::string baseDir = pset().baseDir();
00106 std::string sep = pset().sep();
00107 std::string timePattern = pset().timePattern();
00108 std::string filePattern = pset().filePattern();
00109 #endif
00110
00111 auto pid = getpid();
00112 std::string exeString;
00113 std::string hostString;
00114 std::string timeBuffISO;
00115 std::string timeBuff;
00116
00117
00118
00119 if (filePattern.find("%N") != std::string::npos || filePattern.find("%?N") != std::string::npos)
00120 {
00121
00122 std::string exe;
00123 std::ostringstream pid_ostr;
00124 pid_ostr << "/proc/" << pid << "/exe";
00125 exe = std::string(realpath(pid_ostr.str().c_str(), NULL));
00126
00127 size_t end = exe.find('\0');
00128 size_t start = exe.find_last_of('/', end);
00129 exeString = exe.substr(start + 1, end - start - 1);
00130 }
00131
00132
00133 if (filePattern.find("%H") != std::string::npos || filePattern.find("%?H") != std::string::npos)
00134 {
00135 char hostname[256];
00136 std::string hostString = "";
00137 if (gethostname(&hostname[0], 256) == 0)
00138 {
00139 std::string tmpString(hostname);
00140 hostString = tmpString;
00141 size_t pos = hostString.find(".");
00142 if (pos != std::string::npos && pos > 2)
00143 {
00144 hostString = hostString.substr(0, pos);
00145 }
00146 }
00147 }
00148 if (filePattern.find("%T") != std::string::npos)
00149 {
00150 timeBuffISO = boost::posix_time::to_iso_string(boost::posix_time::second_clock::universal_time());
00151 }
00152 if (filePattern.find("%t") != std::string::npos)
00153 {
00154
00155 time_t rawtime;
00156 struct tm* timeinfo;
00157 char timeBuffC[256];
00158 time(&rawtime);
00159 timeinfo = localtime(&rawtime);
00160 strftime(timeBuffC, 256, timePattern.c_str(), timeinfo);
00161 timeBuff = std::string(timeBuffC);
00162 }
00163
00164 size_t pos = 0;
00165 TRACE(3, "GenFile: filePattern is: " + filePattern);
00166 while (filePattern.find("%", pos) != std::string::npos)
00167 {
00168 pos = filePattern.find("%", pos) + 1;
00169 switch (filePattern[pos])
00170 {
00171 case '%':
00172 filePattern = filePattern.replace(pos - 1, 2, "%");
00173 break;
00174 case '?':
00175 {
00176 char next = filePattern[pos + 1];
00177 switch (next) {
00178 case 'N':
00179 if (filePattern.find(exeString) != std::string::npos) {
00180 filePattern = filePattern.erase(pos - 1, 3);
00181 }
00182 else {
00183 std::string repString = exeString;
00184
00185 if (!(pos + 1 == filePattern.size() - 1 || pos + 2 == filePattern.find_last_of('.'))) {
00186 repString += sep;
00187 }
00188 filePattern = filePattern.replace(pos - 1, 3, repString);
00189 }
00190 break;
00191 case 'H':
00192 if (filePattern.find(hostString) != std::string::npos) {
00193 filePattern = filePattern.erase(pos - 1, 3);
00194 }
00195 else {
00196 std::string repString = hostString;
00197
00198 if (!(pos + 1 == filePattern.size() - 1 || pos + 2 == filePattern.find_last_of('.'))) {
00199 repString += sep;
00200 filePattern = filePattern.replace(pos - 1, 3, repString);
00201 }
00202 break;
00203 }
00204 }
00205 }
00206 break;
00207 case 'N':
00208 filePattern = filePattern.replace(pos - 1, 2, exeString);
00209 break;
00210 case 'H':
00211 filePattern = filePattern.replace(pos - 1, 2, hostString);
00212 break;
00213 case 'p':
00214 filePattern = filePattern.replace(pos - 1, 2, std::to_string(pid));
00215 break;
00216 case 't':
00217 filePattern = filePattern.replace(pos - 1, 2, timeBuff);
00218 break;
00219 case 'T':
00220 filePattern = filePattern.replace(pos - 1, 2, timeBuffISO);
00221 break;
00222 }
00223 }
00224 std::string fileName = baseDir + "/" + filePattern;
00225 TRACE(3, "GenFile: fileName is: " + fileName);
00226
00227 output_ = std::make_unique<cet::ostream_handle>(fileName.c_str(), append ? std::ios::app : std::ios::trunc);
00228 }
00229
00230
00231
00232
00233 void ELGenFileOutput::routePayload(const std::ostringstream& oss, const ErrorObj&
00234 # if MESSAGEFACILITY_HEX_VERSION < 0x20002
00235 , ELcontextSupplier const& sup
00236 #endif
00237 )
00238 {
00239 *output_ << oss.str();
00240 flush(
00241 # if MESSAGEFACILITY_HEX_VERSION < 0x20002
00242 sup
00243 #endif
00244 );
00245 }
00246
00247 void ELGenFileOutput::flush(
00248 # if MESSAGEFACILITY_HEX_VERSION < 0x20002
00249 ELcontextSupplier const&
00250 #endif
00251 )
00252 {
00253 output_->flush();
00254 }
00255 }
00256
00257
00258
00259
00260
00261
00262
00263 extern "C"
00264 {
00265 auto makePlugin(const std::string&,
00266 const fhicl::ParameterSet& pset)
00267 {
00268 return std::make_unique<mfplugins::ELGenFileOutput>(pset);
00269 }
00270 }
00271
00272 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)