1 #include "artdaq-core/Utilities/configureMessageFacility.hh"
2 #include "messagefacility/MessageLogger/MessageLogger.h"
3 #if CANVAS_HEX_VERSION >= 0x20002 // art v2_07_03 means a new versions of fhicl, boost, etc
4 # include "fhiclcpp/ParameterSet.h"
5 # include <boost/lexical_cast.hpp>
7 #include "fhiclcpp/make_ParameterSet.h"
8 #include "cetlib_except/exception.h"
9 #include <boost/filesystem.hpp>
13 #define TRACE_NAME "configureMessageFacility"
16 namespace BFS = boost::filesystem;
20 std::string logPathProblem =
"";
21 std::string logfileName =
"";
22 char* logRootString = getenv(
"ARTDAQ_LOG_ROOT");
23 char* logFhiclCode = getenv(
"ARTDAQ_LOG_FHICL");
24 char* artdaqMfextensionsDir = getenv(
"ARTDAQ_MFEXTENSIONS_DIR");
25 char* useMFExtensionsS = getenv(
"ARTDAQ_MFEXTENSIONS_ENABLED");
26 bool useMFExtensions =
false;
27 if (useMFExtensionsS !=
nullptr && !(strncmp(useMFExtensionsS,
"0", 1) == 0)) {
28 useMFExtensions =
true;
31 std::string logfileDir =
"";
32 if (logRootString !=
nullptr)
34 if (!BFS::exists(logRootString))
36 logPathProblem =
"Log file root directory ";
37 logPathProblem.append(logRootString);
38 logPathProblem.append(
" does not exist!");
39 throw cet::exception(
"ConfigureMessageFacility") << logPathProblem;
43 logfileDir = logRootString;
44 logfileDir.append(
"/");
45 logfileDir.append(progname);
49 if (!BFS::exists(logfileDir))
50 BFS::create_directory(logfileDir);
56 timeinfo = localtime(&rawtime);
57 strftime(timeBuff, 256,
"%Y%m%d%H%M%S", timeinfo);
60 std::string hostString =
"";
61 if (gethostname(&hostname[0], 256) == 0)
63 std::string tmpString(hostname);
64 hostString = tmpString;
65 size_t pos = hostString.find(
".");
66 if (pos != std::string::npos && pos > 2)
68 hostString = hostString.substr(0, pos);
72 logfileName.append(logfileDir);
73 logfileName.append(
"/");
74 logfileName.append(progname);
75 logfileName.append(
"-");
76 if (hostString.size() > 0 && logfileName.find(hostString) == std::string::npos) {
77 logfileName.append(hostString);
78 logfileName.append(
"-");
80 logfileName.append(timeBuff);
81 logfileName.append(
"-");
82 logfileName.append(boost::lexical_cast<std::string>(getpid()));
83 logfileName.append(
".log");
87 std::ostringstream ss;
88 ss <<
"debugModules:[\"*\"] "
89 <<
" destinations : { ";
93 std::string outputLevel =
"\"INFO\" ";
94 if (printDebug) outputLevel =
"\"DEBUG\" ";
95 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
98 <<
" type : \"ANSI\" threshold : " << outputLevel
99 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
100 <<
" noTimeStamps : true "
102 <<
" format: { timestamp: none } "
104 <<
" bell_on_error: true "
109 ss <<
" console : { "
110 <<
" type : \"cout\" threshold :" << outputLevel
111 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
112 <<
" noTimeStamps : true "
114 <<
" format: { timestamp: none } "
120 if (logfileDir.size())
123 ss <<
" type: \"GenFile\" threshold: \"DEBUG\" seperator: \"-\"";
124 ss <<
" pattern: \"" << progname <<
"-%?H%t-%p.log" <<
"\"";
125 ss <<
" timestamp_pattern: \"%Y%m%d%H%M%S\"";
126 ss <<
" directory: \"" << logfileDir <<
"\"";
127 ss <<
" append : false";
130 #if 0 // ELF 01/17/2018 Removed because it violates the "every EVB art process must have identical configuration" rule
131 else if (logfileName.length() > 0)
134 <<
" type : \"file\" threshold : \"DEBUG\" "
135 <<
" filename : \"" << logfileName <<
"\" "
136 <<
" append : false "
141 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
144 <<
" type : \"TRACE\" threshold : \"DEBUG\" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF"
148 if (logFhiclCode !=
nullptr)
150 std::ifstream logfhicl(logFhiclCode);
152 if (logfhicl.is_open())
154 std::stringstream fhiclstream;
155 fhiclstream << logfhicl.rdbuf();
156 ss << fhiclstream.str();
160 throw cet::exception(
"configureMessageFacility") <<
"Unable to open requested fhicl file \"" << logFhiclCode <<
"\".";
166 std::string pstr(ss.str());
169 fhicl::ParameterSet tmp_pset;
170 fhicl::make_ParameterSet(pstr, tmp_pset);
171 return tmp_pset.to_string();
193 std::vector<std::string> names = trace_pset.get_names();
194 std::vector<std::string> trace_envs = {
195 "TRACE_LIMIT_MS",
"TRACE_MODE",
"TRACE_NAMLVLSET" };
196 std::unordered_map<std::string, bool> envs_set_to_unset;
197 for (
auto env : trace_envs) envs_set_to_unset[env] =
false;
199 for (
auto name : names) {
200 if (name ==
"TRACE_NUMENTS" || name ==
"TRACE_ARGSMAX"
201 || name ==
"TRACE_MSGMAX" || name ==
"TRACE_FILE")
203 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
205 else if (name ==
"TRACE_LIMIT_MS") {
206 if (!getenv(name.c_str())) {
207 envs_set_to_unset[name] =
true;
208 std::vector<uint32_t> limit = trace_pset.get<std::vector<uint32_t>>(name);
210 std::string limits = std::to_string(limit[0]) +
"," + std::to_string(limit[1]) +
"," + std::to_string(limit[2]);
211 setenv(name.c_str(), limits.c_str(), 0);
214 else if (name ==
"TRACE_MODE") {
215 if (!getenv(name.c_str())) {
216 envs_set_to_unset[name] =
true;
217 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
220 else if (name ==
"TRACE_NAMLVLSET") {
221 if (!getenv(name.c_str())) {
222 envs_set_to_unset[name] =
true;
223 std::stringstream lvlsbldr;
224 fhicl::ParameterSet lvls_pset = trace_pset.get<fhicl::ParameterSet>(name);
225 std::vector<std::string> tnames = lvls_pset.get_names();
226 for (
auto tname : tnames) {
228 std::vector<uint64_t> msks = lvls_pset.get<std::vector<uint64_t>>(tname);
229 for (
auto msk : msks) {
230 lvlsbldr <<
" 0x" << std::hex << (
unsigned long long)msk;
234 setenv(name.c_str(), lvlsbldr.str().c_str(), 0);
238 TRACE_CNTL(
"namlvlset");
239 for (
auto env : trace_envs)
if (envs_set_to_unset[env]) unsetenv(env.c_str());
245 fhicl::ParameterSet pset;
246 fhicl::make_ParameterSet(pstr, pset);
248 fhicl::ParameterSet trace_pset;
249 if (!pset.get_if_present<fhicl::ParameterSet>(
"TRACE", trace_pset)) {
250 fhicl::ParameterSet trace_dflt_pset;
251 fhicl::make_ParameterSet(
"TRACE:{TRACE_MSGMAX:0 TRACE_LIMIT_MS:[10,500,1500]}", trace_dflt_pset);
252 pset.put<fhicl::ParameterSet>(
"TRACE", trace_dflt_pset.get<fhicl::ParameterSet>(
"TRACE"));
253 trace_pset = pset.get<fhicl::ParameterSet>(
"TRACE");
256 pstr = pset.to_string();
259 #if CANVAS_HEX_VERSION >= 0x30300 // art v2_11_00
260 mf::StartMessageFacility(pset, progname);
262 #elif CANVAS_HEX_VERSION >= 0x20002 // art v2_07_03 means a new versions of fhicl, boost, etc
263 mf::StartMessageFacility(pset);
265 mf::SetApplicationName(progname);
267 mf::setEnabledState(
"");
269 mf::StartMessageFacility(mf::MessageFacilityService::MultiThread, pset);
271 mf::SetModuleName(progname);
272 mf::SetContext(progname);
274 TLOG(TLVL_TRACE) <<
"Message Facility Config input is: " << pstr;
275 TLOG(TLVL_INFO) <<
"Message Facility Application " << progname <<
" configured with: " << pset.to_string();
280 std::string appName(appType);
283 if (gethostname(&hostname[0], 256) == 0)
285 std::string hostString(hostname);
286 size_t pos = hostString.find(
".");
287 if (pos != std::string::npos && pos > 2)
289 hostString = hostString.substr(0, pos);
292 appName.append(hostString);
296 appName.append(boost::lexical_cast<std::string>(port));
298 mf::SetApplicationName(appName);
void configureTRACE(fhicl::ParameterSet &trace_pset)
Configure TRACE.
std::string generateMessageFacilityConfiguration(char const *progname, bool useConsole=true, bool printDebug=false)
Create the MessageFacility configuration Fhicl string.
void configureMessageFacility(char const *progname, bool useConsole=true, bool printDebug=false)
Configure and start the message facility. Provide the program name so that messages will be appropria...
std::string setMsgFacAppName(const std::string &appType, unsigned short port)
Set the message facility application name using the specified application type and port number...