1 #include "artdaq/DAQdata/configureMessageFacility.hh"
2 #include "messagefacility/MessageLogger/MessageLogger.h"
3 #include "fhiclcpp/make_ParameterSet.h"
4 #include <boost/filesystem.hpp>
9 namespace BFS = boost::filesystem;
13 std::string logPathProblem =
"";
14 std::string logfileName =
"";
15 char* logRootString = getenv(
"ARTDAQ_LOG_ROOT");
16 char* logFhiclCode = getenv(
"ARTDAQ_LOG_FHICL");
17 char* artdaqMfextensionsDir = getenv(
"ARTDAQ_MFEXTENSIONS_DIR");
19 if (logRootString !=
nullptr)
21 if (!BFS::exists(logRootString))
23 logPathProblem =
"Log file root directory ";
24 logPathProblem.append(logRootString);
25 logPathProblem.append(
" does not exist!");
29 std::string logfileDir(logRootString);
30 logfileDir.append(
"/");
31 logfileDir.append(progname);
32 if (!BFS::exists(logfileDir))
34 logPathProblem =
"Log file directory ";
35 logPathProblem.append(logfileDir);
36 logPathProblem.append(
" does not exist!");
44 timeinfo = localtime(&rawtime);
45 strftime(timeBuff, 256,
"%Y%m%d%H%M%S", timeinfo);
48 std::string hostString =
"";
49 if (gethostname(&hostname[0], 256) == 0)
51 std::string tmpString(hostname);
52 hostString = tmpString;
53 size_t pos = hostString.find(
".");
54 if (pos != std::string::npos && pos > 2)
56 hostString = hostString.substr(0, pos);
60 logfileName.append(logfileDir);
61 logfileName.append(
"/");
62 logfileName.append(progname);
63 logfileName.append(
"-");
64 logfileName.append(timeBuff);
65 logfileName.append(
"-");
66 if (hostString.size() > 0)
68 logfileName.append(hostString);
69 logfileName.append(
"-");
71 logfileName.append(boost::lexical_cast<std::string>(getpid()));
72 logfileName.append(
".log");
77 std::ostringstream ss;
78 ss <<
"debugModules:[\"*\"] statistics:[\"stats\"] "
79 <<
" destinations : { ";
82 if (artdaqMfextensionsDir !=
nullptr)
85 <<
" type : \"ANSI\" threshold : \"INFO\" "
86 <<
" noTimeStamps : true "
87 <<
" bell_on_error: true "
93 <<
" type : \"cout\" threshold : \"INFO\" "
94 <<
" noTimeStamps : true "
99 if (logfileName.length() > 0)
102 <<
" type : \"file\" threshold : \"DEBUG\" "
103 <<
" filename : \"" << logfileName <<
"\" "
104 <<
" append : false "
108 if (artdaqMfextensionsDir !=
nullptr)
111 <<
" type : \"TRACE\" threshold : \"DEBUG\" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF"
115 if (logFhiclCode !=
nullptr)
117 std::ifstream logfhicl(logFhiclCode);
119 if (logfhicl.is_open())
121 std::stringstream fhiclstream;
122 fhiclstream << logfhicl.rdbuf();
123 ss << fhiclstream.str();
127 throw cet::exception(
"configureMessageFacility") <<
128 "Unable to open requested fhicl file \"" <<
129 logFhiclCode <<
"\".";
135 fhicl::ParameterSet pset;
136 std::string pstr(ss.str());
138 fhicl::make_ParameterSet(pstr, pset);
140 mf::StartMessageFacility(mf::MessageFacilityService::MultiThread,
143 mf::SetModuleName(progname);
144 mf::SetContext(progname);
146 if (logPathProblem.size() > 0)
148 mf::LogError(progname) << logPathProblem;
154 std::string appName(appType);
157 if (gethostname(&hostname[0], 256) == 0)
159 std::string hostString(hostname);
160 size_t pos = hostString.find(
".");
161 if (pos != std::string::npos && pos > 2)
163 hostString = hostString.substr(0, pos);
166 appName.append(hostString);
170 appName.append(boost::lexical_cast<std::string>(port));
172 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...