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 #define TRACE_NAME "configureMessageFacility"
13 #include "TRACE/tracemf.h"
15 namespace BFS = boost::filesystem;
23 fhicl::ParameterSet
make_pset(std::string
const& config_str)
25 return fhicl::ParameterSet::make(config_str);
31 std::string logPathProblem;
32 char* logRootString = getenv(
"ARTDAQ_LOG_ROOT");
33 char* logFhiclCode = getenv(
"ARTDAQ_LOG_FHICL");
34 char* artdaqMfextensionsDir = getenv(
"ARTDAQ_MFEXTENSIONS_DIR");
35 char* useMFExtensionsS = getenv(
"ARTDAQ_MFEXTENSIONS_ENABLED");
36 bool useMFExtensions =
false;
37 if (useMFExtensionsS !=
nullptr && !(strncmp(useMFExtensionsS,
"0", 1) == 0))
39 useMFExtensions =
true;
42 char* printTimestampsToConsoleS = getenv(
"ARTDAQ_LOG_TIMESTAMPS_TO_CONSOLE");
43 bool printTimestampsToConsole =
true;
44 if (printTimestampsToConsoleS !=
nullptr && strncmp(printTimestampsToConsoleS,
"0", 1) == 0)
46 printTimestampsToConsole =
false;
49 std::string logfileDir;
50 if (logRootString !=
nullptr)
52 if (!BFS::exists(logRootString))
54 logPathProblem =
"Log file root directory ";
55 logPathProblem.append(logRootString);
56 logPathProblem.append(
" does not exist!");
57 throw cet::exception(
"ConfigureMessageFacility") << logPathProblem;
60 logfileDir = logRootString;
61 logfileDir.append(
"/");
62 logfileDir.append(progname);
66 if (!BFS::exists(logfileDir))
68 BFS::create_directory(logfileDir);
69 BFS::permissions(logfileDir, BFS::add_perms | BFS::owner_all | BFS::group_all | BFS::others_read);
73 std::ostringstream ss;
75 ss <<
" destinations : { ";
79 std::string outputLevel =
"\"INFO\" ";
82 outputLevel =
"\"DEBUG\" ";
84 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
87 <<
" type : \"ANSI\" threshold : " << outputLevel;
88 if (!printTimestampsToConsole)
90 ss <<
" format: { timestamp: none } ";
92 ss <<
" bell_on_error: true ";
98 <<
" type : \"cout\" threshold :" << outputLevel;
99 if (!printTimestampsToConsole)
101 ss <<
" format: { timestamp: none } ";
107 if (!logfileDir.empty())
110 ss << R
"( type: "GenFile" threshold: "DEBUG" seperator: "-")";
111 ss << " pattern: \"" << progname << fileExtraName <<
"-%?H%t-%p.log"
113 ss <<
" timestamp_pattern: \"%Y%m%d%H%M%S\"";
114 ss <<
" directory: \"" << logfileDir <<
"\"";
115 ss <<
" append : false";
119 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
122 << R
"( type : "TRACE" threshold : "DEBUG" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF)"
126 if (logFhiclCode !=
nullptr)
128 std::ifstream logfhicl(logFhiclCode);
130 if (logfhicl.is_open())
132 std::stringstream fhiclstream;
133 fhiclstream << logfhicl.rdbuf();
134 ss << fhiclstream.str();
138 TLOG(TLVL_ERROR) <<
"Unable to open requested fhicl file ARTDAQ_LOG_FHICL=\"" << logFhiclCode <<
"\".";
139 throw cet::exception(
"ConfigureMessageFacility") <<
"Unable to open requested fhicl file ARTDAQ_LOG_FHICL=\"" << logFhiclCode <<
"\".";
145 std::string pstr(ss.str());
148 fhicl::ParameterSet tmp_pset;
151 tmp_pset = make_pset(pstr);
153 catch (cet::exception
const& ex)
155 ExceptionHandler(ExceptionHandlerRethrow::yes, std::string(
"Exception occurred while processing fhicl ParameterSet string ") + pstr +
":");
157 return tmp_pset.to_string();
179 std::vector<std::string> names = trace_pset.get_names();
180 std::vector<std::string> trace_envs = {
181 "TRACE_LIMIT_MS",
"TRACE_MODE",
"TRACE_NAMLVLSET"};
182 std::unordered_map<std::string, bool> envs_set_to_unset;
183 for (
const auto& env : trace_envs)
185 envs_set_to_unset[env] =
false;
188 for (
const auto& name : names)
190 if (name ==
"TRACE_NUMENTS" || name ==
"TRACE_ARGSMAX" || name ==
"TRACE_MSGMAX" || name ==
"TRACE_FILE")
193 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
196 else if (name ==
"TRACE_LIMIT_MS")
198 if (getenv(name.c_str()) ==
nullptr)
200 envs_set_to_unset[name] =
true;
201 auto limit = trace_pset.get<std::vector<uint32_t>>(name);
203 std::string limits = std::to_string(limit[0]) +
"," + std::to_string(limit[1]) +
"," + std::to_string(limit[2]);
204 setenv(name.c_str(), limits.c_str(), 0);
207 else if (name ==
"TRACE_MODE")
209 if (getenv(name.c_str()) ==
nullptr)
211 envs_set_to_unset[name] =
true;
212 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
215 else if (name ==
"TRACE_NAMLVLSET")
217 if (getenv(name.c_str()) ==
nullptr)
219 envs_set_to_unset[name] =
true;
220 std::stringstream lvlsbldr;
221 auto lvls_pset = trace_pset.get<fhicl::ParameterSet>(name);
222 std::vector<std::string> tnames = lvls_pset.get_names();
223 for (
const auto& tname : tnames)
226 auto msks = lvls_pset.get<std::vector<double>>(tname);
227 for (
auto msk : msks)
229 lvlsbldr <<
" 0x" << std::hex << static_cast<unsigned long long>(msk);
233 setenv(name.c_str(), lvlsbldr.str().c_str(), 0);
237 TRACE_CNTL(
"namlvlset");
238 for (
const auto& env : trace_envs)
240 if (envs_set_to_unset[env])
242 unsetenv(env.c_str());
250 fhicl::ParameterSet pset;
253 pset = make_pset(pstr);
255 catch (cet::exception
const&)
257 ExceptionHandler(ExceptionHandlerRethrow::yes, std::string(
"Exception occurred while processing fhicl ParameterSet string ") + pstr +
":");
260 fhicl::ParameterSet trace_pset;
261 if (!pset.get_if_present<fhicl::ParameterSet>(
"TRACE", trace_pset))
263 auto trace_dflt_pset = make_pset(
"TRACE:{TRACE_MSGMAX:0 TRACE_LIMIT_MS:[10,500,1500]}");
264 pset.put<fhicl::ParameterSet>(
"TRACE", trace_dflt_pset.get<fhicl::ParameterSet>(
"TRACE"));
265 trace_pset = pset.get<fhicl::ParameterSet>(
"TRACE");
268 pstr = pset.to_string();
271 mf::StartMessageFacility(pset, progname);
273 TLOG(TLVL_DEBUG + 33) <<
"Message Facility Config input is: " << pstr;
274 TLOG(TLVL_INFO) <<
"Message Facility Application " << progname <<
" configured with: " << pset.to_string();
279 std::string appName(appType);
282 if (gethostname(&hostname[0], 256) == 0)
284 std::string hostString(hostname);
285 size_t pos = hostString.find(
'.');
286 if (pos != std::string::npos && pos > 2)
288 hostString = hostString.substr(0, pos);
291 appName.append(hostString);
295 appName.append(boost::lexical_cast<std::string>(port));
297 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.