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