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 #ifndef SIMPLER_PSET_MAKE
13 #include "fhiclcpp/make_ParameterSet.h"
15 #define TRACE_NAME "configureMessageFacility"
18 namespace BFS = boost::filesystem;
21 fhicl::ParameterSet make_pset(std::string
const& config_str)
23 #ifdef SIMPLER_PSET_MAKE
24 return fhicl::ParameterSet::make(config_str);
26 fhicl::ParameterSet tmp_pset;
27 fhicl::make_ParameterSet(config_str, tmp_pset);
35 std::string logPathProblem;
36 char* logRootString = getenv(
"ARTDAQ_LOG_ROOT");
37 char* logFhiclCode = getenv(
"ARTDAQ_LOG_FHICL");
38 char* artdaqMfextensionsDir = getenv(
"ARTDAQ_MFEXTENSIONS_DIR");
39 char* useMFExtensionsS = getenv(
"ARTDAQ_MFEXTENSIONS_ENABLED");
40 bool useMFExtensions =
false;
41 if (useMFExtensionsS !=
nullptr && !(strncmp(useMFExtensionsS,
"0", 1) == 0))
43 useMFExtensions =
true;
46 char* printTimestampsToConsoleS = getenv(
"ARTDAQ_LOG_TIMESTAMPS_TO_CONSOLE");
47 bool printTimestampsToConsole =
true;
48 if (printTimestampsToConsoleS !=
nullptr && strncmp(printTimestampsToConsoleS,
"0", 1) == 0)
50 printTimestampsToConsole =
false;
53 std::string logfileDir;
54 if (logRootString !=
nullptr)
56 if (!BFS::exists(logRootString))
58 logPathProblem =
"Log file root directory ";
59 logPathProblem.append(logRootString);
60 logPathProblem.append(
" does not exist!");
61 throw cet::exception(
"ConfigureMessageFacility") << logPathProblem;
64 logfileDir = logRootString;
65 logfileDir.append(
"/");
66 logfileDir.append(progname);
70 if (!BFS::exists(logfileDir))
72 BFS::create_directory(logfileDir);
73 BFS::permissions(logfileDir, BFS::add_perms | BFS::owner_all | BFS::group_all | BFS::others_read);
77 std::ostringstream ss;
79 ss <<
" destinations : { ";
83 std::string outputLevel =
"\"INFO\" ";
86 outputLevel =
"\"DEBUG\" ";
88 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
91 <<
" type : \"ANSI\" threshold : " << outputLevel;
92 if (!printTimestampsToConsole)
94 ss <<
" format: { timestamp: none } ";
96 ss <<
" bell_on_error: true ";
101 ss <<
" console : { "
102 <<
" type : \"cout\" threshold :" << outputLevel;
103 if (!printTimestampsToConsole)
105 ss <<
" format: { timestamp: none } ";
111 if (!logfileDir.empty())
114 ss << R
"( type: "GenFile" threshold: "DEBUG" seperator: "-")";
115 ss << " pattern: \"" << progname << fileExtraName <<
"-%?H%t-%p.log"
117 ss <<
" timestamp_pattern: \"%Y%m%d%H%M%S\"";
118 ss <<
" directory: \"" << logfileDir <<
"\"";
119 ss <<
" append : false";
123 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
126 << R
"( type : "TRACE" threshold : "DEBUG" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF)"
130 if (logFhiclCode !=
nullptr)
132 std::ifstream logfhicl(logFhiclCode);
134 if (logfhicl.is_open())
136 std::stringstream fhiclstream;
137 fhiclstream << logfhicl.rdbuf();
138 ss << fhiclstream.str();
142 throw cet::exception(
"configureMessageFacility") <<
"Unable to open requested fhicl file \"" << logFhiclCode <<
"\".";
148 std::string pstr(ss.str());
151 fhicl::ParameterSet tmp_pset;
154 tmp_pset = make_pset(pstr);
156 catch (cet::exception
const& ex)
158 ExceptionHandler(ExceptionHandlerRethrow::yes, std::string(
"Exception occurred while processing fhicl ParameterSet string ") + pstr +
":");
160 return tmp_pset.to_string();
182 std::vector<std::string> names = trace_pset.get_names();
183 std::vector<std::string> trace_envs = {
184 "TRACE_LIMIT_MS",
"TRACE_MODE",
"TRACE_NAMLVLSET"};
185 std::unordered_map<std::string, bool> envs_set_to_unset;
186 for (
const auto& env : trace_envs)
188 envs_set_to_unset[env] =
false;
191 for (
const auto& name : names)
193 if (name ==
"TRACE_NUMENTS" || name ==
"TRACE_ARGSMAX" || name ==
"TRACE_MSGMAX" || name ==
"TRACE_FILE")
196 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
199 else if (name ==
"TRACE_LIMIT_MS")
201 if (getenv(name.c_str()) ==
nullptr)
203 envs_set_to_unset[name] =
true;
204 auto 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")
212 if (getenv(name.c_str()) ==
nullptr)
214 envs_set_to_unset[name] =
true;
215 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
218 else if (name ==
"TRACE_NAMLVLSET")
220 if (getenv(name.c_str()) ==
nullptr)
222 envs_set_to_unset[name] =
true;
223 std::stringstream lvlsbldr;
224 auto lvls_pset = trace_pset.get<fhicl::ParameterSet>(name);
225 std::vector<std::string> tnames = lvls_pset.get_names();
226 for (
const auto& tname : tnames)
229 auto msks = lvls_pset.get<std::vector<double>>(tname);
230 for (
auto msk : msks)
232 lvlsbldr <<
" 0x" << std::hex << static_cast<unsigned long long>(msk);
236 setenv(name.c_str(), lvlsbldr.str().c_str(), 0);
240 TRACE_CNTL(
"namlvlset");
241 for (
const auto& env : trace_envs)
243 if (envs_set_to_unset[env])
245 unsetenv(env.c_str());
253 fhicl::ParameterSet pset;
256 pset = make_pset(pstr);
258 catch (cet::exception
const&)
260 ExceptionHandler(ExceptionHandlerRethrow::yes, std::string(
"Exception occurred while processing fhicl ParameterSet string ") + pstr +
":");
263 fhicl::ParameterSet trace_pset;
264 if (!pset.get_if_present<fhicl::ParameterSet>(
"TRACE", trace_pset))
266 auto trace_dflt_pset = make_pset(
"TRACE:{TRACE_MSGMAX:0 TRACE_LIMIT_MS:[10,500,1500]}");
267 pset.put<fhicl::ParameterSet>(
"TRACE", trace_dflt_pset.get<fhicl::ParameterSet>(
"TRACE"));
268 trace_pset = pset.get<fhicl::ParameterSet>(
"TRACE");
271 pstr = pset.to_string();
274 mf::StartMessageFacility(pset, progname);
276 TLOG(TLVL_TRACE) <<
"Message Facility Config input is: " << pstr;
277 TLOG(TLVL_INFO) <<
"Message Facility Application " << progname <<
" configured with: " << pset.to_string();
282 std::string appName(appType);
285 if (gethostname(&hostname[0], 256) == 0)
287 std::string hostString(hostname);
288 size_t pos = hostString.find(
'.');
289 if (pos != std::string::npos && pos > 2)
291 hostString = hostString.substr(0, pos);
294 appName.append(hostString);
298 appName.append(boost::lexical_cast<std::string>(port));
300 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.