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 "artdaq-core/Utilities/ExceptionHandler.hh"
10 #include "cetlib_except/exception.h"
11 #include "fhiclcpp/ParameterSet.h"
12 #include "fhiclcpp/make_ParameterSet.h"
13 #define TRACE_NAME "configureMessageFacility"
16 namespace BFS = boost::filesystem;
20 std::string logPathProblem;
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;
49 logfileDir = logRootString;
50 logfileDir.append(
"/");
51 logfileDir.append(progname);
55 if (!BFS::exists(logfileDir))
57 BFS::create_directory(logfileDir);
58 BFS::permissions(logfileDir, BFS::add_perms | BFS::owner_all | BFS::group_all | BFS::others_read);
62 std::ostringstream ss;
64 ss <<
" destinations : { ";
68 std::string outputLevel =
"\"INFO\" ";
71 outputLevel =
"\"DEBUG\" ";
73 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
76 <<
" type : \"ANSI\" threshold : " << outputLevel;
77 if (!printTimestampsToConsole)
79 ss <<
" format: { timestamp: none } ";
81 ss <<
" bell_on_error: true ";
87 <<
" type : \"cout\" threshold :" << outputLevel;
88 if (!printTimestampsToConsole)
90 ss <<
" format: { timestamp: none } ";
96 if (!logfileDir.empty())
99 ss << R
"( type: "GenFile" threshold: "DEBUG" seperator: "-")";
100 ss << " pattern: \"" << progname << fileExtraName <<
"-%?H%t-%p.log"
102 ss <<
" timestamp_pattern: \"%Y%m%d%H%M%S\"";
103 ss <<
" directory: \"" << logfileDir <<
"\"";
104 ss <<
" append : false";
108 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
111 << R
"( type : "TRACE" threshold : "DEBUG" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF)"
115 if (logFhiclCode !=
nullptr)
117 std::ifstream logfhicl(logFhiclCode);
119 if (logfhicl.is_open())
121 std::stringstream fhiclstream;
122 fhiclstream << logfhicl.rdbuf();
123 ss << fhiclstream.str();
127 throw cet::exception(
"configureMessageFacility") <<
"Unable to open requested fhicl file \"" << logFhiclCode <<
"\".";
133 std::string pstr(ss.str());
136 fhicl::ParameterSet tmp_pset;
139 fhicl::make_ParameterSet(pstr, tmp_pset);
141 catch (cet::exception
const& ex)
143 ExceptionHandler(ExceptionHandlerRethrow::yes, std::string(
"Exception occurred while processing fhicl ParameterSet string ") + pstr +
":");
145 return tmp_pset.to_string();
167 std::vector<std::string> names = trace_pset.get_names();
168 std::vector<std::string> trace_envs = {
169 "TRACE_LIMIT_MS",
"TRACE_MODE",
"TRACE_NAMLVLSET"};
170 std::unordered_map<std::string, bool> envs_set_to_unset;
171 for (
const auto& env : trace_envs)
173 envs_set_to_unset[env] =
false;
176 for (
const auto& name : names)
178 if (name ==
"TRACE_NUMENTS" || name ==
"TRACE_ARGSMAX" || name ==
"TRACE_MSGMAX" || name ==
"TRACE_FILE")
181 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
184 else if (name ==
"TRACE_LIMIT_MS")
186 if (getenv(name.c_str()) ==
nullptr)
188 envs_set_to_unset[name] =
true;
189 auto limit = trace_pset.get<std::vector<uint32_t>>(name);
191 std::string limits = std::to_string(limit[0]) +
"," + std::to_string(limit[1]) +
"," + std::to_string(limit[2]);
192 setenv(name.c_str(), limits.c_str(), 0);
195 else if (name ==
"TRACE_MODE")
197 if (getenv(name.c_str()) ==
nullptr)
199 envs_set_to_unset[name] =
true;
200 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
203 else if (name ==
"TRACE_NAMLVLSET")
205 if (getenv(name.c_str()) ==
nullptr)
207 envs_set_to_unset[name] =
true;
208 std::stringstream lvlsbldr;
209 auto lvls_pset = trace_pset.get<fhicl::ParameterSet>(name);
210 std::vector<std::string> tnames = lvls_pset.get_names();
211 for (
const auto& tname : tnames)
214 auto msks = lvls_pset.get<std::vector<double>>(tname);
215 for (
auto msk : msks)
217 lvlsbldr <<
" 0x" << std::hex << static_cast<unsigned long long>(msk);
221 setenv(name.c_str(), lvlsbldr.str().c_str(), 0);
225 TRACE_CNTL(
"namlvlset");
226 for (
const auto& env : trace_envs)
228 if (envs_set_to_unset[env])
230 unsetenv(env.c_str());
238 fhicl::ParameterSet pset;
241 fhicl::make_ParameterSet(pstr, pset);
243 catch (cet::exception
const&)
245 ExceptionHandler(ExceptionHandlerRethrow::yes, std::string(
"Exception occurred while processing fhicl ParameterSet string ") + pstr +
":");
248 fhicl::ParameterSet trace_pset;
249 if (!pset.get_if_present<fhicl::ParameterSet>(
"TRACE", trace_pset))
251 fhicl::ParameterSet trace_dflt_pset;
252 fhicl::make_ParameterSet(
"TRACE:{TRACE_MSGMAX:0 TRACE_LIMIT_MS:[10,500,1500]}", trace_dflt_pset);
253 pset.put<fhicl::ParameterSet>(
"TRACE", trace_dflt_pset.get<fhicl::ParameterSet>(
"TRACE"));
254 trace_pset = pset.get<fhicl::ParameterSet>(
"TRACE");
257 pstr = pset.to_string();
260 mf::StartMessageFacility(pset, progname);
262 TLOG(TLVL_TRACE) <<
"Message Facility Config input is: " << pstr;
263 TLOG(TLVL_INFO) <<
"Message Facility Application " << progname <<
" configured with: " << pset.to_string();
268 std::string appName(appType);
271 if (gethostname(&hostname[0], 256) == 0)
273 std::string hostString(hostname);
274 size_t pos = hostString.find(
'.');
275 if (pos != std::string::npos && pos > 2)
277 hostString = hostString.substr(0, pos);
280 appName.append(hostString);
284 appName.append(boost::lexical_cast<std::string>(port));
286 mf::SetApplicationName(appName);
void configureTRACE(fhicl::ParameterSet &trace_pset)
Configure TRACE.
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...
std::string generateMessageFacilityConfiguration(char const *progname, bool useConsole=true, bool printDebug=false, char const *fileExtraName="")
Create the MessageFacility configuration Fhicl string.
void ExceptionHandler(ExceptionHandlerRethrow decision, const std::string &optional_message)
The ExceptionHandler class prints out all available information about an excection, then optionally re-throws.