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