artdaq  v2_03_00
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
configureMessageFacility.cc
1 #include "artdaq/DAQdata/configureMessageFacility.hh"
2 #include "messagefacility/MessageLogger/MessageLogger.h"
3 #include "fhiclcpp/make_ParameterSet.h"
4 #include <boost/filesystem.hpp>
5 #include <unistd.h>
6 #include <fstream>
7 #include <sstream>
8 
9 namespace BFS = boost::filesystem;
10 
11 void artdaq::configureMessageFacility(char const* progname, bool useConsole)
12 {
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");
18 
19  if (logRootString != nullptr)
20  {
21  if (!BFS::exists(logRootString))
22  {
23  logPathProblem = "Log file root directory ";
24  logPathProblem.append(logRootString);
25  logPathProblem.append(" does not exist!");
26  }
27  else
28  {
29  std::string logfileDir(logRootString);
30  logfileDir.append("/");
31  logfileDir.append(progname);
32  if (!BFS::exists(logfileDir))
33  {
34  logPathProblem = "Log file directory ";
35  logPathProblem.append(logfileDir);
36  logPathProblem.append(" does not exist!");
37  }
38  else
39  {
40  time_t rawtime;
41  struct tm* timeinfo;
42  char timeBuff[256];
43  time(&rawtime);
44  timeinfo = localtime(&rawtime);
45  strftime(timeBuff, 256, "%Y%m%d%H%M%S", timeinfo);
46 
47  char hostname[256];
48  std::string hostString = "";
49  if (gethostname(&hostname[0], 256) == 0)
50  {
51  std::string tmpString(hostname);
52  hostString = tmpString;
53  size_t pos = hostString.find(".");
54  if (pos != std::string::npos && pos > 2)
55  {
56  hostString = hostString.substr(0, pos);
57  }
58  }
59 
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)
67  {
68  logfileName.append(hostString);
69  logfileName.append("-");
70  }
71  logfileName.append(boost::lexical_cast<std::string>(getpid()));
72  logfileName.append(".log");
73  }
74  }
75  }
76 
77  std::ostringstream ss;
78  ss << "debugModules:[\"*\"] statistics:[\"stats\"] "
79  << " destinations : { ";
80 
81  if (useConsole) {
82  if (artdaqMfextensionsDir != nullptr)
83  {
84  ss << " console : { "
85  << " type : \"ANSI\" threshold : \"INFO\" "
86  << " noTimeStamps : true "
87  << " bell_on_error: true "
88  << " } ";
89  }
90  else
91  {
92  ss << " console : { "
93  << " type : \"cout\" threshold : \"INFO\" "
94  << " noTimeStamps : true "
95  << " } ";
96  }
97  }
98 
99  if (logfileName.length() > 0)
100  {
101  ss << " file : { "
102  << " type : \"file\" threshold : \"DEBUG\" "
103  << " filename : \"" << logfileName << "\" "
104  << " append : false "
105  << " } ";
106  }
107 
108  if (artdaqMfextensionsDir != nullptr)
109  {
110  ss << " trace : { "
111  << " type : \"TRACE\" threshold : \"DEBUG\" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF"
112  << " }";
113  }
114 
115  if (logFhiclCode != nullptr)
116  {
117  std::ifstream logfhicl(logFhiclCode);
118 
119  if (logfhicl.is_open())
120  {
121  std::stringstream fhiclstream;
122  fhiclstream << logfhicl.rdbuf();
123  ss << fhiclstream.str();
124  }
125  else
126  {
127  throw cet::exception("configureMessageFacility") <<
128  "Unable to open requested fhicl file \"" <<
129  logFhiclCode << "\".";
130  }
131  }
132 
133  ss << " } ";
134 
135  fhicl::ParameterSet pset;
136  std::string pstr(ss.str());
137  //std::cout << "Message Facility Config is: " << pstr << std::endl;
138  fhicl::make_ParameterSet(pstr, pset);
139 
140  mf::StartMessageFacility(mf::MessageFacilityService::MultiThread,
141  pset);
142 
143  mf::SetModuleName(progname);
144  mf::SetContext(progname);
145 
146  if (logPathProblem.size() > 0)
147  {
148  mf::LogError(progname) << logPathProblem;
149  }
150 }
151 
152 void artdaq::setMsgFacAppName(const std::string& appType, unsigned short port)
153 {
154  std::string appName(appType);
155 
156  char hostname[256];
157  if (gethostname(&hostname[0], 256) == 0)
158  {
159  std::string hostString(hostname);
160  size_t pos = hostString.find(".");
161  if (pos != std::string::npos && pos > 2)
162  {
163  hostString = hostString.substr(0, pos);
164  }
165  appName.append("-");
166  appName.append(hostString);
167  }
168 
169  appName.append("-");
170  appName.append(boost::lexical_cast<std::string>(port));
171 
172  mf::SetApplicationName(appName);
173 }
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...