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;
26 fhicl::ParameterSet
make_pset(std::string
const& config_str)
28 #ifdef SIMPLER_PSET_MAKE
29 return fhicl::ParameterSet::make(config_str);
31 fhicl::ParameterSet tmp_pset;
32 fhicl::make_ParameterSet(config_str, tmp_pset);
40 std::string logPathProblem;
41 char* logRootString = getenv(
"ARTDAQ_LOG_ROOT");
42 char* logFhiclCode = getenv(
"ARTDAQ_LOG_FHICL");
43 char* artdaqMfextensionsDir = getenv(
"ARTDAQ_MFEXTENSIONS_DIR");
44 char* useMFExtensionsS = getenv(
"ARTDAQ_MFEXTENSIONS_ENABLED");
45 bool useMFExtensions =
false;
46 if (useMFExtensionsS !=
nullptr && !(strncmp(useMFExtensionsS,
"0", 1) == 0))
48 useMFExtensions =
true;
51 char* printTimestampsToConsoleS = getenv(
"ARTDAQ_LOG_TIMESTAMPS_TO_CONSOLE");
52 bool printTimestampsToConsole =
true;
53 if (printTimestampsToConsoleS !=
nullptr && strncmp(printTimestampsToConsoleS,
"0", 1) == 0)
55 printTimestampsToConsole =
false;
58 std::string logfileDir;
59 if (logRootString !=
nullptr)
61 if (!BFS::exists(logRootString))
63 logPathProblem =
"Log file root directory ";
64 logPathProblem.append(logRootString);
65 logPathProblem.append(
" does not exist!");
66 throw cet::exception(
"ConfigureMessageFacility") << logPathProblem;
69 logfileDir = logRootString;
70 logfileDir.append(
"/");
71 logfileDir.append(progname);
75 if (!BFS::exists(logfileDir))
77 BFS::create_directory(logfileDir);
78 BFS::permissions(logfileDir, BFS::add_perms | BFS::owner_all | BFS::group_all | BFS::others_read);
82 std::ostringstream ss;
84 ss <<
" destinations : { ";
88 std::string outputLevel =
"\"INFO\" ";
91 outputLevel =
"\"DEBUG\" ";
93 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
96 <<
" type : \"ANSI\" threshold : " << outputLevel;
97 if (!printTimestampsToConsole)
99 ss <<
" format: { timestamp: none } ";
101 ss <<
" bell_on_error: true ";
106 ss <<
" console : { "
107 <<
" type : \"cout\" threshold :" << outputLevel;
108 if (!printTimestampsToConsole)
110 ss <<
" format: { timestamp: none } ";
116 if (!logfileDir.empty())
119 ss << R
"( type: "GenFile" threshold: "DEBUG" seperator: "-")";
120 ss << " pattern: \"" << progname << fileExtraName <<
"-%?H%t-%p.log"
122 ss <<
" timestamp_pattern: \"%Y%m%d%H%M%S\"";
123 ss <<
" directory: \"" << logfileDir <<
"\"";
124 ss <<
" append : false";
128 if (artdaqMfextensionsDir !=
nullptr && useMFExtensions)
131 << R
"( type : "TRACE" threshold : "DEBUG" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF)"
135 if (logFhiclCode !=
nullptr)
137 std::ifstream logfhicl(logFhiclCode);
139 if (logfhicl.is_open())
141 std::stringstream fhiclstream;
142 fhiclstream << logfhicl.rdbuf();
143 ss << fhiclstream.str();
147 TLOG(TLVL_ERROR) <<
"Unable to open requested fhicl file ARTDAQ_LOG_FHICL=\"" << logFhiclCode <<
"\".";
148 throw cet::exception(
"ConfigureMessageFacility") <<
"Unable to open requested fhicl file ARTDAQ_LOG_FHICL=\"" << logFhiclCode <<
"\".";
154 std::string pstr(ss.str());
157 fhicl::ParameterSet tmp_pset;
160 tmp_pset = make_pset(pstr);
162 catch (cet::exception
const& ex)
164 ExceptionHandler(ExceptionHandlerRethrow::yes, std::string(
"Exception occurred while processing fhicl ParameterSet string ") + pstr +
":");
166 return tmp_pset.to_string();
188 std::vector<std::string> names = trace_pset.get_names();
189 std::vector<std::string> trace_envs = {
190 "TRACE_LIMIT_MS",
"TRACE_MODE",
"TRACE_NAMLVLSET"};
191 std::unordered_map<std::string, bool> envs_set_to_unset;
192 for (
const auto& env : trace_envs)
194 envs_set_to_unset[env] =
false;
197 for (
const auto& name : names)
199 if (name ==
"TRACE_NUMENTS" || name ==
"TRACE_ARGSMAX" || name ==
"TRACE_MSGMAX" || name ==
"TRACE_FILE")
202 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
205 else if (name ==
"TRACE_LIMIT_MS")
207 if (getenv(name.c_str()) ==
nullptr)
209 envs_set_to_unset[name] =
true;
210 auto limit = trace_pset.get<std::vector<uint32_t>>(name);
212 std::string limits = std::to_string(limit[0]) +
"," + std::to_string(limit[1]) +
"," + std::to_string(limit[2]);
213 setenv(name.c_str(), limits.c_str(), 0);
216 else if (name ==
"TRACE_MODE")
218 if (getenv(name.c_str()) ==
nullptr)
220 envs_set_to_unset[name] =
true;
221 setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
224 else if (name ==
"TRACE_NAMLVLSET")
226 if (getenv(name.c_str()) ==
nullptr)
228 envs_set_to_unset[name] =
true;
229 std::stringstream lvlsbldr;
230 auto lvls_pset = trace_pset.get<fhicl::ParameterSet>(name);
231 std::vector<std::string> tnames = lvls_pset.get_names();
232 for (
const auto& tname : tnames)
235 auto msks = lvls_pset.get<std::vector<double>>(tname);
236 for (
auto msk : msks)
238 lvlsbldr <<
" 0x" << std::hex << static_cast<unsigned long long>(msk);
242 setenv(name.c_str(), lvlsbldr.str().c_str(), 0);
246 TRACE_CNTL(
"namlvlset");
247 for (
const auto& env : trace_envs)
249 if (envs_set_to_unset[env])
251 unsetenv(env.c_str());
259 fhicl::ParameterSet pset;
262 pset = make_pset(pstr);
264 catch (cet::exception
const&)
266 ExceptionHandler(ExceptionHandlerRethrow::yes, std::string(
"Exception occurred while processing fhicl ParameterSet string ") + pstr +
":");
269 fhicl::ParameterSet trace_pset;
270 if (!pset.get_if_present<fhicl::ParameterSet>(
"TRACE", trace_pset))
272 auto trace_dflt_pset = make_pset(
"TRACE:{TRACE_MSGMAX:0 TRACE_LIMIT_MS:[10,500,1500]}");
273 pset.put<fhicl::ParameterSet>(
"TRACE", trace_dflt_pset.get<fhicl::ParameterSet>(
"TRACE"));
274 trace_pset = pset.get<fhicl::ParameterSet>(
"TRACE");
277 pstr = pset.to_string();
280 mf::StartMessageFacility(pset, progname);
282 TLOG(TLVL_TRACE) <<
"Message Facility Config input is: " << pstr;
283 TLOG(TLVL_INFO) <<
"Message Facility Application " << progname <<
" configured with: " << pset.to_string();
288 std::string appName(appType);
291 if (gethostname(&hostname[0], 256) == 0)
293 std::string hostString(hostname);
294 size_t pos = hostString.find(
'.');
295 if (pos != std::string::npos && pos > 2)
297 hostString = hostString.substr(0, pos);
300 appName.append(hostString);
304 appName.append(boost::lexical_cast<std::string>(port));
306 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.