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))
29 useMFExtensions =
true;
32 char* printTimestampsToConsoleS = getenv(
"ARTDAQ_LOG_TIMESTAMPS_TO_CONSOLE");
33 bool printTimestampsToConsole =
true;
34 if (printTimestampsToConsoleS !=
nullptr && strncmp(printTimestampsToConsoleS,
"0", 1) == 0)
36 printTimestampsToConsole =
false;
39 std::string logfileDir =
"";
40 if (logRootString !=
nullptr)
42 if (!BFS::exists(logRootString))
44 logPathProblem =
"Log file root directory ";
45 logPathProblem.append(logRootString);
46 logPathProblem.append(
" does not exist!");
47 throw cet::exception(
"ConfigureMessageFacility") << logPathProblem;
51 logfileDir = logRootString;
52 logfileDir.append(
"/");
53 logfileDir.append(progname);
57 if (!BFS::exists(logfileDir))
58 BFS::create_directory(logfileDir);
64 timeinfo = localtime(&rawtime);
65 strftime(timeBuff, 256,
"%Y%m%d%H%M%S", timeinfo);
68 std::string hostString =
"";
69 if (gethostname(&hostname[0], 256) == 0)
71 std::string tmpString(hostname);
72 hostString = tmpString;
73 size_t pos = hostString.find(
".");
74 if (pos != std::string::npos && pos > 2)
76 hostString = hostString.substr(0, pos);
80 logfileName.append(logfileDir);
81 logfileName.append(
"/");
82 logfileName.append(progname);
83 logfileName.append(
"-");
84 if (hostString.size() > 0 && logfileName.find(hostString) == std::string::npos)
86 logfileName.append(hostString);
87 logfileName.append(
"-");
89 logfileName.append(timeBuff);
90 logfileName.append(
"-");
91 logfileName.append(boost::lexical_cast<std::string>(getpid()));
92 logfileName.append(
".log");
96 std::ostringstream ss;
97 ss <<
"debugModules:[\"*\"] "
98 <<
" destinations : { ";
102 std::string outputLevel =
"\"INFO\" ";
103 if (printDebug) outputLevel =
"\"DEBUG\" ";
104 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
106 ss <<
" console : { "
107 <<
" type : \"ANSI\" threshold : " << outputLevel;
108 if (!printTimestampsToConsole)
110 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
111 ss <<
" noTimeStamps : true ";
113 ss <<
" format: { timestamp: none } ";
116 ss <<
" bell_on_error: true ";
121 ss <<
" console : { "
122 <<
" type : \"cout\" threshold :" << outputLevel;
123 if (!printTimestampsToConsole)
125 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
126 ss <<
" noTimeStamps : true ";
128 ss <<
" format: { timestamp: none } ";
135 if (logfileDir.size())
138 ss <<
" type: \"GenFile\" threshold: \"DEBUG\" seperator: \"-\"";
139 ss <<
" pattern: \"" << progname <<
"-%?H%t-%p.log" <<
"\"";
140 ss <<
" timestamp_pattern: \"%Y%m%d%H%M%S\"";
141 ss <<
" directory: \"" << logfileDir <<
"\"";
142 ss <<
" append : false";
145 #if 0 // ELF 01/17/2018 Removed because it violates the "every EVB art process must have identical configuration" rule
146 else if (logfileName.length() > 0)
149 <<
" type : \"file\" threshold : \"DEBUG\" "
150 <<
" filename : \"" << logfileName <<
"\" "
151 <<
" append : false "
156 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
159 <<
" type : \"TRACE\" threshold : \"DEBUG\" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF"
163 if (logFhiclCode !=
nullptr)
165 std::ifstream logfhicl(logFhiclCode);
167 if (logfhicl.is_open())
169 std::stringstream fhiclstream;
170 fhiclstream << logfhicl.rdbuf();
171 ss << fhiclstream.str();
175 throw cet::exception(
"configureMessageFacility") <<
"Unable to open requested fhicl file \"" << logFhiclCode <<
"\".";
181 std::string pstr(ss.str());
184 fhicl::ParameterSet tmp_pset;
185 fhicl::make_ParameterSet(pstr, tmp_pset);
186 return tmp_pset.to_string();
208 std::vector<std::string> names = trace_pset.get_names();
209 std::vector<std::string> trace_envs = {
210 "TRACE_LIMIT_MS",
"TRACE_MODE",
"TRACE_NAMLVLSET" };
211 std::unordered_map<std::string, bool> envs_set_to_unset;
212 for (
auto env : trace_envs) envs_set_to_unset[env] =
false;
214 for (
auto name : names)
216 if (name ==
"TRACE_NUMENTS" || name ==
"TRACE_ARGSMAX"
217 || name ==
"TRACE_MSGMAX" || name ==
"TRACE_FILE")
219 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
221 else if (name ==
"TRACE_LIMIT_MS")
223 if (!getenv(name.c_str()))
225 envs_set_to_unset[name] =
true;
226 std::vector<uint32_t> limit = trace_pset.get<std::vector<uint32_t>>(name);
228 std::string limits = std::to_string(limit[0]) +
"," + std::to_string(limit[1]) +
"," + std::to_string(limit[2]);
229 setenv(name.c_str(), limits.c_str(), 0);
232 else if (name ==
"TRACE_MODE")
234 if (!getenv(name.c_str()))
236 envs_set_to_unset[name] =
true;
237 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
240 else if (name ==
"TRACE_NAMLVLSET")
242 if (!getenv(name.c_str()))
244 envs_set_to_unset[name] =
true;
245 std::stringstream lvlsbldr;
246 fhicl::ParameterSet lvls_pset = trace_pset.get<fhicl::ParameterSet>(name);
247 std::vector<std::string> tnames = lvls_pset.get_names();
248 for (
auto tname : tnames)
251 std::vector<uint64_t> msks = lvls_pset.get<std::vector<uint64_t>>(tname);
252 for (
auto msk : msks)
254 lvlsbldr <<
" 0x" << std::hex << (
unsigned long long)msk;
258 setenv(name.c_str(), lvlsbldr.str().c_str(), 0);
262 TRACE_CNTL(
"namlvlset");
263 for (
auto env : trace_envs)
if (envs_set_to_unset[env]) unsetenv(env.c_str());
269 fhicl::ParameterSet pset;
270 fhicl::make_ParameterSet(pstr, pset);
272 fhicl::ParameterSet trace_pset;
273 if (!pset.get_if_present<fhicl::ParameterSet>(
"TRACE", trace_pset))
275 fhicl::ParameterSet trace_dflt_pset;
276 fhicl::make_ParameterSet(
"TRACE:{TRACE_MSGMAX:0 TRACE_LIMIT_MS:[10,500,1500]}", trace_dflt_pset);
277 pset.put<fhicl::ParameterSet>(
"TRACE", trace_dflt_pset.get<fhicl::ParameterSet>(
"TRACE"));
278 trace_pset = pset.get<fhicl::ParameterSet>(
"TRACE");
281 pstr = pset.to_string();
284 #if CANVAS_HEX_VERSION >= 0x30300 // art v2_11_00
285 mf::StartMessageFacility(pset, progname);
287 #elif CANVAS_HEX_VERSION >= 0x20002 // art v2_07_03 means a new versions of fhicl, boost, etc
288 mf::StartMessageFacility(pset);
290 mf::SetApplicationName(progname);
292 mf::setEnabledState(
"");
294 mf::StartMessageFacility(mf::MessageFacilityService::MultiThread, pset);
296 mf::SetModuleName(progname);
297 mf::SetContext(progname);
299 TLOG(TLVL_TRACE) <<
"Message Facility Config input is: " << pstr;
300 TLOG(TLVL_INFO) <<
"Message Facility Application " << progname <<
" configured with: " << pset.to_string();
305 std::string appName(appType);
308 if (gethostname(&hostname[0], 256) == 0)
310 std::string hostString(hostname);
311 size_t pos = hostString.find(
".");
312 if (pos != std::string::npos && pos > 2)
314 hostString = hostString.substr(0, pos);
317 appName.append(hostString);
321 appName.append(boost::lexical_cast<std::string>(port));
323 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...