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:[\"*\"] "
102 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
103 <<
" statistics:[\"stats\"] "
105 <<
" destinations : { ";
109 std::string outputLevel =
"\"INFO\" ";
110 if (printDebug) outputLevel =
"\"DEBUG\" ";
111 if (artdaqMfextensionsDir !=
nullptr)
113 ss <<
" console : { "
114 <<
" type : \"ANSI\" threshold : " << outputLevel
115 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
116 <<
" noTimeStamps : true "
118 <<
" format: { timestamp: none } "
120 <<
" bell_on_error: true "
125 ss <<
" console : { "
126 <<
" type : \"cout\" threshold :" << outputLevel
127 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
128 <<
" noTimeStamps : true "
130 <<
" format: { timestamp: none } "
136 if (artdaqMfextensionsDir !=
nullptr)
139 <<
" type: \"GenFile\" threshold: \"DEBUG\" sep: \"-\" "
140 <<
" file_name_prefix: \"" << progname <<
"\" ";
141 if (logfileDir.size())
143 ss <<
" base_directory: \"" << logfileDir <<
"\"";
145 ss <<
" append : false "
148 else if (logfileName.length() > 0)
151 <<
" type : \"file\" threshold : \"DEBUG\" "
152 <<
" filename : \"" << logfileName <<
"\" "
153 <<
" append : false "
157 if (artdaqMfextensionsDir !=
nullptr)
160 <<
" type : \"TRACE\" threshold : \"DEBUG\" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF"
164 if (logFhiclCode !=
nullptr)
166 std::ifstream logfhicl(logFhiclCode);
168 if (logfhicl.is_open())
170 std::stringstream fhiclstream;
171 fhiclstream << logfhicl.rdbuf();
172 ss << fhiclstream.str();
176 throw cet::exception(
"configureMessageFacility") <<
"Unable to open requested fhicl file \"" << logFhiclCode <<
"\".";
182 std::string pstr(ss.str());
183 std::cout <<
"Message Facility Config is: " << pstr << std::endl;
190 fhicl::ParameterSet pset;
191 fhicl::make_ParameterSet(pstr, pset);
193 #if CANVAS_HEX_VERSION >= 0x20002 // art v2_07_03 means a new versions of fhicl, boost, etc
194 mf::StartMessageFacility(pset);
196 mf::SetApplicationName(progname);
198 mf::setEnabledState(
"");
200 mf::StartMessageFacility(mf::MessageFacilityService::MultiThread, pset);
202 mf::SetModuleName(progname);
203 mf::SetContext(progname);
209 std::string appName(appType);
212 if (gethostname(&hostname[0], 256) == 0)
214 std::string hostString(hostname);
215 size_t pos = hostString.find(
".");
216 if (pos != std::string::npos && pos > 2)
218 hostString = hostString.substr(0, pos);
221 appName.append(hostString);
225 appName.append(boost::lexical_cast<std::string>(port));
227 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...