00001 #include "artdaq/DAQdata/configureMessageFacility.hh"
00002 #include "messagefacility/MessageLogger/MessageLogger.h"
00003 #include "fhiclcpp/make_ParameterSet.h"
00004 #include <boost/filesystem.hpp>
00005 #include <unistd.h>
00006 #include <fstream>
00007 #include <sstream>
00008
00009 namespace BFS = boost::filesystem;
00010
00011 void artdaq::configureMessageFacility(char const* progname, bool useConsole)
00012 {
00013 std::string logPathProblem = "";
00014 std::string logfileName = "";
00015 char* logRootString = getenv("ARTDAQ_LOG_ROOT");
00016 char* logFhiclCode = getenv("ARTDAQ_LOG_FHICL");
00017 char* artdaqMfextensionsDir = getenv("ARTDAQ_MFEXTENSIONS_DIR");
00018
00019 if (logRootString != nullptr)
00020 {
00021 if (!BFS::exists(logRootString))
00022 {
00023 logPathProblem = "Log file root directory ";
00024 logPathProblem.append(logRootString);
00025 logPathProblem.append(" does not exist!");
00026 }
00027 else
00028 {
00029 std::string logfileDir(logRootString);
00030 logfileDir.append("/");
00031 logfileDir.append(progname);
00032 if (!BFS::exists(logfileDir))
00033 {
00034 logPathProblem = "Log file directory ";
00035 logPathProblem.append(logfileDir);
00036 logPathProblem.append(" does not exist!");
00037 }
00038 else
00039 {
00040 time_t rawtime;
00041 struct tm* timeinfo;
00042 char timeBuff[256];
00043 time(&rawtime);
00044 timeinfo = localtime(&rawtime);
00045 strftime(timeBuff, 256, "%Y%m%d%H%M%S", timeinfo);
00046
00047 char hostname[256];
00048 std::string hostString = "";
00049 if (gethostname(&hostname[0], 256) == 0)
00050 {
00051 std::string tmpString(hostname);
00052 hostString = tmpString;
00053 size_t pos = hostString.find(".");
00054 if (pos != std::string::npos && pos > 2)
00055 {
00056 hostString = hostString.substr(0, pos);
00057 }
00058 }
00059
00060 logfileName.append(logfileDir);
00061 logfileName.append("/");
00062 logfileName.append(progname);
00063 logfileName.append("-");
00064 logfileName.append(timeBuff);
00065 logfileName.append("-");
00066 if (hostString.size() > 0)
00067 {
00068 logfileName.append(hostString);
00069 logfileName.append("-");
00070 }
00071 logfileName.append(boost::lexical_cast<std::string>(getpid()));
00072 logfileName.append(".log");
00073 }
00074 }
00075 }
00076
00077 std::ostringstream ss;
00078 ss << "debugModules:[\"*\"] statistics:[\"stats\"] "
00079 << " destinations : { ";
00080
00081 if (useConsole) {
00082 if (artdaqMfextensionsDir != nullptr)
00083 {
00084 ss << " console : { "
00085 << " type : \"ANSI\" threshold : \"INFO\" "
00086 << " noTimeStamps : true "
00087 << " bell_on_error: true "
00088 << " } ";
00089 }
00090 else
00091 {
00092 ss << " console : { "
00093 << " type : \"cout\" threshold : \"INFO\" "
00094 << " noTimeStamps : true "
00095 << " } ";
00096 }
00097 }
00098
00099 if (logfileName.length() > 0)
00100 {
00101 ss << " file : { "
00102 << " type : \"file\" threshold : \"DEBUG\" "
00103 << " filename : \"" << logfileName << "\" "
00104 << " append : false "
00105 << " } ";
00106 }
00107
00108 if (artdaqMfextensionsDir != nullptr)
00109 {
00110 ss << " trace : { "
00111 << " type : \"TRACE\" threshold : \"DEBUG\" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF"
00112 << " }";
00113 }
00114
00115 if (logFhiclCode != nullptr)
00116 {
00117 std::ifstream logfhicl(logFhiclCode);
00118
00119 if (logfhicl.is_open())
00120 {
00121 std::stringstream fhiclstream;
00122 fhiclstream << logfhicl.rdbuf();
00123 ss << fhiclstream.str();
00124 }
00125 else
00126 {
00127 throw cet::exception("configureMessageFacility") <<
00128 "Unable to open requested fhicl file \"" <<
00129 logFhiclCode << "\".";
00130 }
00131 }
00132
00133 ss << " } ";
00134
00135 fhicl::ParameterSet pset;
00136 std::string pstr(ss.str());
00137
00138 fhicl::make_ParameterSet(pstr, pset);
00139
00140 mf::StartMessageFacility(mf::MessageFacilityService::MultiThread,
00141 pset);
00142
00143 mf::SetModuleName(progname);
00144 mf::SetContext(progname);
00145
00146 if (logPathProblem.size() > 0)
00147 {
00148 mf::LogError(progname) << logPathProblem;
00149 }
00150 }
00151
00152 void artdaq::setMsgFacAppName(const std::string& appType, unsigned short port)
00153 {
00154 std::string appName(appType);
00155
00156 char hostname[256];
00157 if (gethostname(&hostname[0], 256) == 0)
00158 {
00159 std::string hostString(hostname);
00160 size_t pos = hostString.find(".");
00161 if (pos != std::string::npos && pos > 2)
00162 {
00163 hostString = hostString.substr(0, pos);
00164 }
00165 appName.append("-");
00166 appName.append(hostString);
00167 }
00168
00169 appName.append("-");
00170 appName.append(boost::lexical_cast<std::string>(port));
00171
00172 mf::SetApplicationName(appName);
00173 }