1 #include "artdaq-core/Utilities/configureMessageFacility.hh"
2 #include "messagefacility/MessageLogger/MessageLogger.h"
5 #include <boost/filesystem.hpp>
6 #include <boost/lexical_cast.hpp>
9 #include "cetlib_except/exception.h"
10 #include "fhiclcpp/ParameterSet.h"
11 #include "fhiclcpp/make_ParameterSet.h"
12 #define TRACE_NAME "configureMessageFacility"
15 namespace BFS = boost::filesystem;
19 std::string logPathProblem =
"";
20 std::string logfileName =
"";
21 char* logRootString = getenv(
"ARTDAQ_LOG_ROOT");
22 char* logFhiclCode = getenv(
"ARTDAQ_LOG_FHICL");
23 char* artdaqMfextensionsDir = getenv(
"ARTDAQ_MFEXTENSIONS_DIR");
24 char* useMFExtensionsS = getenv(
"ARTDAQ_MFEXTENSIONS_ENABLED");
25 bool useMFExtensions =
false;
26 if (useMFExtensionsS !=
nullptr && !(strncmp(useMFExtensionsS,
"0", 1) == 0))
28 useMFExtensions =
true;
31 char* printTimestampsToConsoleS = getenv(
"ARTDAQ_LOG_TIMESTAMPS_TO_CONSOLE");
32 bool printTimestampsToConsole =
true;
33 if (printTimestampsToConsoleS !=
nullptr && strncmp(printTimestampsToConsoleS,
"0", 1) == 0)
35 printTimestampsToConsole =
false;
38 std::string logfileDir =
"";
39 if (logRootString !=
nullptr)
41 if (!BFS::exists(logRootString))
43 logPathProblem =
"Log file root directory ";
44 logPathProblem.append(logRootString);
45 logPathProblem.append(
" does not exist!");
46 throw cet::exception(
"ConfigureMessageFacility") << logPathProblem;
50 logfileDir = logRootString;
51 logfileDir.append(
"/");
52 logfileDir.append(progname);
56 if (!BFS::exists(logfileDir))
57 BFS::create_directory(logfileDir);
63 timeinfo = localtime(&rawtime);
64 strftime(timeBuff, 256,
"%Y%m%d%H%M%S", timeinfo);
67 std::string hostString =
"";
68 if (gethostname(&hostname[0], 256) == 0)
70 std::string tmpString(hostname);
71 hostString = tmpString;
72 size_t pos = hostString.find(
".");
73 if (pos != std::string::npos && pos > 2)
75 hostString = hostString.substr(0, pos);
79 logfileName.append(logfileDir);
80 logfileName.append(
"/");
81 logfileName.append(progname);
82 logfileName.append(
"-");
83 if (hostString.size() > 0 && logfileName.find(hostString) == std::string::npos)
85 logfileName.append(hostString);
86 logfileName.append(
"-");
88 logfileName.append(timeBuff);
89 logfileName.append(
"-");
90 logfileName.append(boost::lexical_cast<std::string>(getpid()));
91 logfileName.append(
".log");
95 std::ostringstream ss;
96 ss <<
"debugModules:[\"*\"] "
97 <<
" destinations : { ";
101 std::string outputLevel =
"\"INFO\" ";
102 if (printDebug) outputLevel =
"\"DEBUG\" ";
103 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
105 ss <<
" console : { "
106 <<
" type : \"ANSI\" threshold : " << outputLevel;
107 if (!printTimestampsToConsole)
109 ss <<
" format: { timestamp: none } ";
111 ss <<
" bell_on_error: true ";
116 ss <<
" console : { "
117 <<
" type : \"cout\" threshold :" << outputLevel;
118 if (!printTimestampsToConsole)
120 ss <<
" format: { timestamp: none } ";
126 if (logfileDir.size())
129 ss <<
" type: \"GenFile\" threshold: \"DEBUG\" seperator: \"-\"";
130 ss <<
" pattern: \"" << progname <<
"-%?H%t-%p.log"
132 ss <<
" timestamp_pattern: \"%Y%m%d%H%M%S\"";
133 ss <<
" directory: \"" << logfileDir <<
"\"";
134 ss <<
" append : false";
137 #if 0 // ELF 01/17/2018 Removed because it violates the "every EVB art process must have identical configuration" rule
138 else if (logfileName.length() > 0)
141 <<
" type : \"file\" threshold : \"DEBUG\" "
142 <<
" filename : \"" << logfileName <<
"\" "
143 <<
" append : false "
148 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
151 <<
" type : \"TRACE\" threshold : \"DEBUG\" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF"
155 if (logFhiclCode !=
nullptr)
157 std::ifstream logfhicl(logFhiclCode);
159 if (logfhicl.is_open())
161 std::stringstream fhiclstream;
162 fhiclstream << logfhicl.rdbuf();
163 ss << fhiclstream.str();
167 throw cet::exception(
"configureMessageFacility") <<
"Unable to open requested fhicl file \"" << logFhiclCode <<
"\".";
173 std::string pstr(ss.str());
176 fhicl::ParameterSet tmp_pset;
177 fhicl::make_ParameterSet(pstr, tmp_pset);
178 return tmp_pset.to_string();
200 std::vector<std::string> names = trace_pset.get_names();
201 std::vector<std::string> trace_envs = {
202 "TRACE_LIMIT_MS",
"TRACE_MODE",
"TRACE_NAMLVLSET"};
203 std::unordered_map<std::string, bool> envs_set_to_unset;
204 for (
auto env : trace_envs) envs_set_to_unset[env] =
false;
206 for (
auto name : names)
208 if (name ==
"TRACE_NUMENTS" || name ==
"TRACE_ARGSMAX" || name ==
"TRACE_MSGMAX" || name ==
"TRACE_FILE")
210 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
212 else if (name ==
"TRACE_LIMIT_MS")
214 if (!getenv(name.c_str()))
216 envs_set_to_unset[name] =
true;
217 std::vector<uint32_t> limit = trace_pset.get<std::vector<uint32_t>>(name);
219 std::string limits = std::to_string(limit[0]) +
"," + std::to_string(limit[1]) +
"," + std::to_string(limit[2]);
220 setenv(name.c_str(), limits.c_str(), 0);
223 else if (name ==
"TRACE_MODE")
225 if (!getenv(name.c_str()))
227 envs_set_to_unset[name] =
true;
228 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
231 else if (name ==
"TRACE_NAMLVLSET")
233 if (!getenv(name.c_str()))
235 envs_set_to_unset[name] =
true;
236 std::stringstream lvlsbldr;
237 fhicl::ParameterSet lvls_pset = trace_pset.get<fhicl::ParameterSet>(name);
238 std::vector<std::string> tnames = lvls_pset.get_names();
239 for (
auto tname : tnames)
242 std::vector<uint64_t> msks = lvls_pset.get<std::vector<uint64_t>>(tname);
243 for (
auto msk : msks)
245 lvlsbldr <<
" 0x" << std::hex << (
unsigned long long)msk;
249 setenv(name.c_str(), lvlsbldr.str().c_str(), 0);
253 TRACE_CNTL(
"namlvlset");
254 for (
auto env : trace_envs)
255 if (envs_set_to_unset[env]) unsetenv(env.c_str());
261 fhicl::ParameterSet pset;
262 fhicl::make_ParameterSet(pstr, pset);
264 fhicl::ParameterSet trace_pset;
265 if (!pset.get_if_present<fhicl::ParameterSet>(
"TRACE", trace_pset))
267 fhicl::ParameterSet trace_dflt_pset;
268 fhicl::make_ParameterSet(
"TRACE:{TRACE_MSGMAX:0 TRACE_LIMIT_MS:[10,500,1500]}", trace_dflt_pset);
269 pset.put<fhicl::ParameterSet>(
"TRACE", trace_dflt_pset.get<fhicl::ParameterSet>(
"TRACE"));
270 trace_pset = pset.get<fhicl::ParameterSet>(
"TRACE");
273 pstr = pset.to_string();
276 #if CANVAS_HEX_VERSION >= 0x30300 // art v2_11_00
277 mf::StartMessageFacility(pset, progname);
280 mf::StartMessageFacility(pset);
282 mf::SetApplicationName(progname);
284 mf::setEnabledState(
"");
286 TLOG(TLVL_TRACE) <<
"Message Facility Config input is: " << pstr;
287 TLOG(TLVL_INFO) <<
"Message Facility Application " << progname <<
" configured with: " << pset.to_string();
292 std::string appName(appType);
295 if (gethostname(&hostname[0], 256) == 0)
297 std::string hostString(hostname);
298 size_t pos = hostString.find(
".");
299 if (pos != std::string::npos && pos > 2)
301 hostString = hostString.substr(0, pos);
304 appName.append(hostString);
308 appName.append(boost::lexical_cast<std::string>(port));
310 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...