1 #include "artdaq/DAQdata/configureMessageFacility.hh"
2 #include "messagefacility/MessageLogger/MessageLogger.h"
3 #if ART_HEX_VERSION >= 0x20703 // 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 <boost/filesystem.hpp>
14 namespace BFS = boost::filesystem;
18 std::string logPathProblem =
"";
19 std::string logfileName =
"";
20 char* logRootString = getenv(
"ARTDAQ_LOG_ROOT");
21 char* logFhiclCode = getenv(
"ARTDAQ_LOG_FHICL");
22 char* artdaqMfextensionsDir = getenv(
"ARTDAQ_MFEXTENSIONS_DIR");
25 setenv(
"TRACE_LVLS",
"0xf", 0 );
26 unsigned long long lvls=strtoull( getenv(
"TRACE_LVLS"), NULL, 0 );
31 TRACE_CNTL(
"lvlmskSg", lvls );
32 TRACE( 4,
"configureMessageFacilit lvlmskSg set to 0x%llx", lvls );
35 if (logRootString !=
nullptr)
37 if (!BFS::exists(logRootString))
39 logPathProblem =
"Log file root directory ";
40 logPathProblem.append(logRootString);
41 logPathProblem.append(
" does not exist!");
45 std::string logfileDir(logRootString);
46 logfileDir.append(
"/");
47 logfileDir.append(progname);
48 if (!BFS::exists(logfileDir))
50 logPathProblem =
"Log file directory ";
51 logPathProblem.append(logfileDir);
52 logPathProblem.append(
" does not exist!");
60 timeinfo = localtime(&rawtime);
61 strftime(timeBuff, 256,
"%Y%m%d%H%M%S", timeinfo);
64 std::string hostString =
"";
65 if (gethostname(&hostname[0], 256) == 0)
67 std::string tmpString(hostname);
68 hostString = tmpString;
69 size_t pos = hostString.find(
".");
70 if (pos != std::string::npos && pos > 2)
72 hostString = hostString.substr(0, pos);
76 logfileName.append(logfileDir);
77 logfileName.append(
"/");
78 logfileName.append(progname);
79 logfileName.append(
"-");
80 logfileName.append(timeBuff);
81 logfileName.append(
"-");
82 if (hostString.size() > 0)
84 logfileName.append(hostString);
85 logfileName.append(
"-");
87 logfileName.append(boost::lexical_cast<std::string>(getpid()));
88 logfileName.append(
".log");
93 std::ostringstream ss;
94 ss <<
"debugModules:[\"*\"] statistics:[\"stats\"] "
95 <<
" destinations : { ";
98 if (artdaqMfextensionsDir !=
nullptr)
100 ss <<
" console : { "
101 <<
" type : \"ANSI\" threshold : \"INFO\" "
102 <<
" noTimeStamps : true "
103 <<
" bell_on_error: true "
108 ss <<
" console : { "
109 <<
" type : \"cout\" threshold : \"INFO\" "
110 <<
" noTimeStamps : true "
115 if (logfileName.length() > 0)
118 <<
" type : \"file\" threshold : \"DEBUG\" "
119 <<
" filename : \"" << logfileName <<
"\" "
120 <<
" append : false "
124 if (artdaqMfextensionsDir !=
nullptr)
127 <<
" type : \"TRACE\" threshold : \"DEBUG\" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF"
131 if (logFhiclCode !=
nullptr)
133 std::ifstream logfhicl(logFhiclCode);
135 if (logfhicl.is_open())
137 std::stringstream fhiclstream;
138 fhiclstream << logfhicl.rdbuf();
139 ss << fhiclstream.str();
143 throw cet::exception(
"configureMessageFacility") <<
144 "Unable to open requested fhicl file \"" <<
145 logFhiclCode <<
"\".";
151 fhicl::ParameterSet pset;
152 std::string pstr(ss.str());
154 fhicl::make_ParameterSet(pstr, pset);
156 # if ART_HEX_VERSION >= 0x20703 // art v2_07_03 means a new versions of fhicl, boost, etc
157 mf::StartMessageFacility(pset);
159 mf::SetApplicationName(progname);
161 mf::StartMessageFacility(mf::MessageFacilityService::MultiThread,
164 mf::SetModuleName(progname);
165 mf::SetContext(progname);
168 if (logPathProblem.size() > 0)
170 mf::LogError(progname) << logPathProblem;
176 std::string appName(appType);
179 if (gethostname(&hostname[0], 256) == 0)
181 std::string hostString(hostname);
182 size_t pos = hostString.find(
".");
183 if (pos != std::string::npos && pos > 2)
185 hostString = hostString.substr(0, pos);
188 appName.append(hostString);
192 appName.append(boost::lexical_cast<std::string>(port));
194 mf::SetApplicationName(appName);
void configureMessageFacility(char const *progname, bool useConsole=true)
Configure and start the message facility. Provide the program name so that messages will be appropria...
void setMsgFacAppName(const std::string &appType, unsigned short port)
Set the message facility application name using the specified application type and port number...