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>
15 namespace BFS = boost::filesystem;
19 std::string logPathProblem =
"";
20 std::string logfileName =
"";
21 char* logRootString = getenv(
"ARTDAQ_LOG_ROOT");
22 char* logFhiclCode = getenv(
"ARTDAQ_LOG_FHICL");
23 char* artdaqMfextensionsDir = getenv(
"ARTDAQ_MFEXTENSIONS_DIR");
26 setenv(
"TRACE_LVLS",
"0xf", 0);
27 unsigned long long lvls = strtoull(getenv(
"TRACE_LVLS"), NULL, 0);
32 TRACE_CNTL(
"lvlmskSg", lvls);
33 TRACE(4,
"configureMessageFacility lvlmskSg set to 0x%llx", lvls);
36 std::string logfileDir =
"";
37 if (logRootString !=
nullptr)
39 if (!BFS::exists(logRootString))
41 logPathProblem =
"Log file root directory ";
42 logPathProblem.append(logRootString);
43 logPathProblem.append(
" does not exist!");
44 throw cet::exception(
"ConfigureMessageFacility") << logPathProblem;
48 logfileDir = logRootString;
49 logfileDir.append(
"/");
50 logfileDir.append(progname);
53 if (!BFS::exists(logfileDir))
55 BFS::create_directory(logfileDir);
67 timeinfo = localtime(&rawtime);
68 strftime(timeBuff, 256,
"%Y%m%d%H%M%S", timeinfo);
71 std::string hostString =
"";
72 if (gethostname(&hostname[0], 256) == 0)
74 std::string tmpString(hostname);
75 hostString = tmpString;
76 size_t pos = hostString.find(
".");
77 if (pos != std::string::npos && pos > 2)
79 hostString = hostString.substr(0, pos);
83 logfileName.append(logfileDir);
84 logfileName.append(
"/");
85 logfileName.append(progname);
86 logfileName.append(
"-");
87 logfileName.append(timeBuff);
88 logfileName.append(
"-");
89 if (hostString.size() > 0)
91 logfileName.append(hostString);
92 logfileName.append(
"-");
94 logfileName.append(boost::lexical_cast<std::string>(getpid()));
95 logfileName.append(
".log");
100 std::ostringstream ss;
101 ss <<
"debugModules:[\"*\"] statistics:[\"stats\"] "
102 <<
" destinations : { ";
106 std::string outputLevel =
"\"INFO\" ";
107 if (printDebug) outputLevel =
"\"DEBUG\" ";
108 if (artdaqMfextensionsDir !=
nullptr)
110 ss <<
" console : { "
111 <<
" type : \"ANSI\" threshold : " << outputLevel
112 <<
" noTimeStamps : true "
113 <<
" bell_on_error: true "
118 ss <<
" console : { "
119 <<
" type : \"cout\" threshold :" << outputLevel
120 <<
" noTimeStamps : true "
125 if (artdaqMfextensionsDir !=
nullptr)
128 <<
" type: \"GenFile\" threshold: \"DEBUG\" sep: \"-\" "
129 <<
" file_name_prefix: \"" << progname <<
"\" ";
130 if (logfileDir.size())
132 ss <<
" base_directory: \"" << logfileDir <<
"\"";
134 ss <<
" append : false "
137 else if (logfileName.length() > 0)
140 <<
" type : \"file\" threshold : \"DEBUG\" "
141 <<
" filename : \"" << logfileName <<
"\" "
142 <<
" append : false "
146 if (artdaqMfextensionsDir !=
nullptr)
149 <<
" type : \"TRACE\" threshold : \"DEBUG\" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF"
153 if (logFhiclCode !=
nullptr)
155 std::ifstream logfhicl(logFhiclCode);
157 if (logfhicl.is_open())
159 std::stringstream fhiclstream;
160 fhiclstream << logfhicl.rdbuf();
161 ss << fhiclstream.str();
165 throw cet::exception(
"configureMessageFacility") <<
"Unable to open requested fhicl file \"" << logFhiclCode <<
"\".";
171 std::string pstr(ss.str());
179 fhicl::ParameterSet pset;
180 fhicl::make_ParameterSet(pstr, pset);
182 #if CANVAS_HEX_VERSION >= 0x20002 // art v2_07_03 means a new versions of fhicl, boost, etc
183 mf::StartMessageFacility(pset);
185 mf::SetApplicationName(progname);
187 mf::StartMessageFacility(mf::MessageFacilityService::MultiThread, pset);
189 mf::SetModuleName(progname);
190 mf::SetContext(progname);
196 std::string appName(appType);
199 if (gethostname(&hostname[0], 256) == 0)
201 std::string hostString(hostname);
202 size_t pos = hostString.find(
".");
203 if (pos != std::string::npos && pos > 2)
205 hostString = hostString.substr(0, pos);
208 appName.append(hostString);
212 appName.append(boost::lexical_cast<std::string>(port));
214 mf::SetApplicationName(appName);
void 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)
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...