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