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());
189 std::vector<std::string> names = trace_pset.get_names();
190 std::vector<std::string> trace_envs = {
191 "TRACE_LIMIT_MS",
"TRACE_MODE",
"TRACE_NAMLVLSET" };
192 std::unordered_map<std::string, bool> envs_set_to_unset;
193 for (
auto env : trace_envs) envs_set_to_unset[env] =
false;
195 for (
auto name : names) {
196 if (name ==
"TRACE_NUMENTS" || name ==
"TRACE_ARGSMAX"
197 || name ==
"TRACE_MSGMAX" || name ==
"TRACE_FILE")
199 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
201 else if (name ==
"TRACE_LIMIT_MS") {
202 if (!getenv(name.c_str())) {
203 envs_set_to_unset[name] =
true;
204 std::vector<uint32_t> limit = trace_pset.get<std::vector<uint32_t>>(name);
206 std::string limits = std::to_string(limit[0]) +
"," + std::to_string(limit[1]) +
"," + std::to_string(limit[2]);
207 setenv(name.c_str(), limits.c_str(), 0);
210 else if (name ==
"TRACE_MODE") {
211 if (!getenv(name.c_str())) {
212 envs_set_to_unset[name] =
true;
213 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
216 else if (name ==
"TRACE_NAMLVLSET") {
217 if (!getenv(name.c_str())) {
218 envs_set_to_unset[name] =
true;
219 std::stringstream lvlsbldr;
220 fhicl::ParameterSet lvls_pset = trace_pset.get<fhicl::ParameterSet>(name);
221 std::vector<std::string> tnames = lvls_pset.get_names();
222 for (
auto tname : tnames) {
224 std::vector<uint64_t> msks = lvls_pset.get<std::vector<uint64_t>>(tname);
225 for (
auto msk : msks) {
226 lvlsbldr <<
" 0x" << std::hex << (
unsigned long long)msk;
230 setenv(name.c_str(), lvlsbldr.str().c_str(), 0);
234 TRACE_CNTL(
"namlvlset");
235 for (
auto env : trace_envs)
if (envs_set_to_unset[env]) unsetenv(env.c_str());
241 fhicl::ParameterSet pset;
242 fhicl::make_ParameterSet(pstr, pset);
244 fhicl::ParameterSet trace_pset;
245 if (!pset.get_if_present<fhicl::ParameterSet>(
"TRACE", trace_pset)) {
246 fhicl::ParameterSet trace_dflt_pset;
247 fhicl::make_ParameterSet(
"TRACE:{TRACE_MSGMAX:0 TRACE_LIMIT_MS:[10,500,1500]}", trace_dflt_pset);
248 pset.put<fhicl::ParameterSet>(
"TRACE", trace_dflt_pset.get<fhicl::ParameterSet>(
"TRACE"));
249 trace_pset = pset.get<fhicl::ParameterSet>(
"TRACE");
252 pstr = pset.to_string();
255 #if CANVAS_HEX_VERSION >= 0x20002 // art v2_07_03 means a new versions of fhicl, boost, etc
256 mf::StartMessageFacility(pset);
258 mf::SetApplicationName(progname);
260 mf::setEnabledState(
"");
262 mf::StartMessageFacility(mf::MessageFacilityService::MultiThread, pset);
264 mf::SetModuleName(progname);
265 mf::SetContext(progname);
267 TLOG(4) <<
"Message Facility Config input is: " << pstr;
268 TLOG_INFO(
"configureMessageFacility") <<
"Message Facility Application " << progname <<
" configured with: " << pset.to_string();
273 std::string appName(appType);
276 if (gethostname(&hostname[0], 256) == 0)
278 std::string hostString(hostname);
279 size_t pos = hostString.find(
".");
280 if (pos != std::string::npos && pos > 2)
282 hostString = hostString.substr(0, pos);
285 appName.append(hostString);
289 appName.append(boost::lexical_cast<std::string>(port));
291 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...