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