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:[\"*\"] "
00102 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
00103 << " statistics:[\"stats\"] "
00104 #endif
00105 << " destinations : { ";
00106
00107 if (useConsole)
00108 {
00109 std::string outputLevel = "\"INFO\" ";
00110 if (printDebug) outputLevel = "\"DEBUG\" ";
00111 if (artdaqMfextensionsDir != nullptr)
00112 {
00113 ss << " console : { "
00114 << " type : \"ANSI\" threshold : " << outputLevel
00115 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
00116 << " noTimeStamps : true "
00117 #else
00118 << " format: { timestamp: none } "
00119 #endif
00120 << " bell_on_error: true "
00121 << " } ";
00122 }
00123 else
00124 {
00125 ss << " console : { "
00126 << " type : \"cout\" threshold :" << outputLevel
00127 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
00128 << " noTimeStamps : true "
00129 #else
00130 << " format: { timestamp: none } "
00131 #endif
00132 << " } ";
00133 }
00134 }
00135
00136 if (artdaqMfextensionsDir != nullptr)
00137 {
00138 ss << " file: { "
00139 << " type: \"GenFile\" threshold: \"DEBUG\" sep: \"-\" "
00140 << " file_name_prefix: \"" << progname << "\" ";
00141 if (logfileDir.size())
00142 {
00143 ss << " base_directory: \"" << logfileDir << "\"";
00144 }
00145 ss << " append : false "
00146 << " } ";
00147 }
00148 else if (logfileName.length() > 0)
00149 {
00150 ss << " file : { "
00151 << " type : \"file\" threshold : \"DEBUG\" "
00152 << " filename : \"" << logfileName << "\" "
00153 << " append : false "
00154 << " } ";
00155 }
00156
00157 if (artdaqMfextensionsDir != nullptr)
00158 {
00159 ss << " trace : { "
00160 << " type : \"TRACE\" threshold : \"DEBUG\" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF"
00161 << " } ";
00162 }
00163
00164 if (logFhiclCode != nullptr)
00165 {
00166 std::ifstream logfhicl(logFhiclCode);
00167
00168 if (logfhicl.is_open())
00169 {
00170 std::stringstream fhiclstream;
00171 fhiclstream << logfhicl.rdbuf();
00172 ss << fhiclstream.str();
00173 }
00174 else
00175 {
00176 throw cet::exception("configureMessageFacility") << "Unable to open requested fhicl file \"" << logFhiclCode << "\".";
00177 }
00178 }
00179
00180 ss << " } ";
00181
00182 std::string pstr(ss.str());
00183 std::cout << "Message Facility Config is: " << pstr << std::endl;
00184 return pstr;
00185 }
00186
00187 void artdaq::configureMessageFacility(char const* progname, bool useConsole, bool printDebug)
00188 {
00189 auto pstr = generateMessageFacilityConfiguration(progname, useConsole, printDebug);
00190 fhicl::ParameterSet pset;
00191 fhicl::make_ParameterSet(pstr, pset);
00192
00193 #if CANVAS_HEX_VERSION >= 0x20002 // art v2_07_03 means a new versions of fhicl, boost, etc
00194 mf::StartMessageFacility(pset);
00195
00196 mf::SetApplicationName(progname);
00197
00198 mf::setEnabledState("");
00199 # else
00200 mf::StartMessageFacility(mf::MessageFacilityService::MultiThread, pset);
00201
00202 mf::SetModuleName(progname);
00203 mf::SetContext(progname);
00204 # endif
00205 }
00206
00207 void artdaq::setMsgFacAppName(const std::string& appType, unsigned short port)
00208 {
00209 std::string appName(appType);
00210
00211 char hostname[256];
00212 if (gethostname(&hostname[0], 256) == 0)
00213 {
00214 std::string hostString(hostname);
00215 size_t pos = hostString.find(".");
00216 if (pos != std::string::npos && pos > 2)
00217 {
00218 hostString = hostString.substr(0, pos);
00219 }
00220 appName.append("-");
00221 appName.append(hostString);
00222 }
00223
00224 appName.append("-");
00225 appName.append(boost::lexical_cast<std::string>(port));
00226
00227 mf::SetApplicationName(appName);
00228 }