artdaq_core  v3_01_05
configureMessageFacility.cc
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>
6 #endif
7 #include "fhiclcpp/make_ParameterSet.h"
8 #include "cetlib_except/exception.h"
9 #include <boost/filesystem.hpp>
10 #include <unistd.h>
11 #include <fstream>
12 #include <sstream>
13 #define TRACE_NAME "configureMessageFacility"
14 #include "tracemf.h" // TRACE_CNTL, TRACE
15 
16 namespace BFS = boost::filesystem;
17 
18 std::string artdaq::generateMessageFacilityConfiguration(char const* progname, bool useConsole, bool printDebug)
19 {
20  std::string logPathProblem = "";
21  std::string logfileName = "";
22  char* logRootString = getenv("ARTDAQ_LOG_ROOT");
23  char* logFhiclCode = getenv("ARTDAQ_LOG_FHICL");
24  char* artdaqMfextensionsDir = getenv("ARTDAQ_MFEXTENSIONS_DIR");
25  char* useMFExtensionsS = getenv("ARTDAQ_MFEXTENSIONS_ENABLED");
26  bool useMFExtensions = false;
27  if (useMFExtensionsS != nullptr && !(strncmp(useMFExtensionsS, "0", 1) == 0)) {
28  useMFExtensions = true;
29  }
30 
31  std::string logfileDir = "";
32  if (logRootString != nullptr)
33  {
34  if (!BFS::exists(logRootString))
35  {
36  logPathProblem = "Log file root directory ";
37  logPathProblem.append(logRootString);
38  logPathProblem.append(" does not exist!");
39  throw cet::exception("ConfigureMessageFacility") << logPathProblem;
40  }
41  else
42  {
43  logfileDir = logRootString;
44  logfileDir.append("/");
45  logfileDir.append(progname);
46 
47  // As long as the top-level directory exists, I don't think we
48  // really care if we have to create application directories...
49  if (!BFS::exists(logfileDir))
50  BFS::create_directory(logfileDir);
51 
52  time_t rawtime;
53  struct tm* timeinfo;
54  char timeBuff[256];
55  time(&rawtime);
56  timeinfo = localtime(&rawtime);
57  strftime(timeBuff, 256, "%Y%m%d%H%M%S", timeinfo);
58 
59  char hostname[256];
60  std::string hostString = "";
61  if (gethostname(&hostname[0], 256) == 0)
62  {
63  std::string tmpString(hostname);
64  hostString = tmpString;
65  size_t pos = hostString.find(".");
66  if (pos != std::string::npos && pos > 2)
67  {
68  hostString = hostString.substr(0, pos);
69  }
70  }
71 
72  logfileName.append(logfileDir);
73  logfileName.append("/");
74  logfileName.append(progname);
75  logfileName.append("-");
76  if (hostString.size() > 0 && logfileName.find(hostString) == std::string::npos) {
77  logfileName.append(hostString);
78  logfileName.append("-");
79  }
80  logfileName.append(timeBuff);
81  logfileName.append("-");
82  logfileName.append(boost::lexical_cast<std::string>(getpid()));
83  logfileName.append(".log");
84  }
85  }
86 
87  std::ostringstream ss;
88  ss << "debugModules:[\"*\"] "
89  << " destinations : { ";
90 
91  if (useConsole)
92  {
93  std::string outputLevel = "\"INFO\" ";
94  if (printDebug) outputLevel = "\"DEBUG\" ";
95  if (artdaqMfextensionsDir != nullptr && useMFExtensions)
96  {
97  ss << " console : { "
98  << " type : \"ANSI\" threshold : " << outputLevel
99 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
100  << " noTimeStamps : true "
101 #else
102  << " format: { timestamp: none } "
103 #endif
104  << " bell_on_error: true "
105  << " } ";
106  }
107  else
108  {
109  ss << " console : { "
110  << " type : \"cout\" threshold :" << outputLevel
111 #if MESSAGEFACILITY_HEX_VERSION < 0x20103
112  << " noTimeStamps : true "
113 #else
114  << " format: { timestamp: none } "
115 #endif
116  << " } ";
117  }
118  }
119 
120  if (logfileDir.size())
121  {
122  ss << " file: {";
123  ss << " type: \"GenFile\" threshold: \"DEBUG\" seperator: \"-\"";
124  ss << " pattern: \"" << progname << "-%?H%t-%p.log" << "\"";
125  ss << " timestamp_pattern: \"%Y%m%d%H%M%S\"";
126  ss << " directory: \"" << logfileDir << "\"";
127  ss << " append : false";
128  ss << " }";
129  }
130 #if 0 // ELF 01/17/2018 Removed because it violates the "every EVB art process must have identical configuration" rule
131  else if (logfileName.length() > 0)
132  {
133  ss << " file : { "
134  << " type : \"file\" threshold : \"DEBUG\" "
135  << " filename : \"" << logfileName << "\" "
136  << " append : false "
137  << " } ";
138  }
139 #endif
140 
141  if (artdaqMfextensionsDir != nullptr && useMFExtensions)
142  {
143  ss << " trace : { "
144  << " type : \"TRACE\" threshold : \"DEBUG\" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF"
145  << " } ";
146  }
147 
148  if (logFhiclCode != nullptr)
149  {
150  std::ifstream logfhicl(logFhiclCode);
151 
152  if (logfhicl.is_open())
153  {
154  std::stringstream fhiclstream;
155  fhiclstream << logfhicl.rdbuf();
156  ss << fhiclstream.str();
157  }
158  else
159  {
160  throw cet::exception("configureMessageFacility") << "Unable to open requested fhicl file \"" << logFhiclCode << "\".";
161  }
162  }
163 
164  ss << " } ";
165 
166  std::string pstr(ss.str());
167  return pstr;
168 }
169 // generateMessageFacilityConfiguration
170 
171 void artdaq::configureTRACE(fhicl::ParameterSet &trace_pset)
172 {
173  /* The following code handles this example fhicl:
174  TRACE:{
175  TRACE_NUMENTS:500000
176  TRACE_ARGSMAX:10
177  TRACE_MSGMAX:0
178  TRACE_FILE:"/tmp/trace_buffer_%u" # this is the default
179  TRACE_LIMIT_MS:[8,80,800]
180  TRACE_MODE:0xf
181  TRACE_NAMLVLSET:{
182  #name:[lvlsmskM,lvlsmskS[,lvlsmskT]] lvlsmskT is optional
183  name0:[0x1f,0x7]
184  name1:[0x2f,0xf]
185  name2:[0x3f,0x7,0x1]
186  }
187  }
188  */
189  std::vector<std::string> names = trace_pset.get_names();
190  std::vector<std::string> trace_envs = {//"TRACE_NUMENTS", "TRACE_ARGSMAX", "TRACE_MSGMAX", "TRACE_FILE",
191  "TRACE_LIMIT_MS", "TRACE_MODE", "TRACE_NAMLVLSET" };
192  std::unordered_map<std::string, bool> envs_set_to_unset;
193  for (auto env : trace_envs) envs_set_to_unset[env] = false;
194  // tricky - some env. vars. will over ride info in "mapped" (file) context while others cannot.
195  for (auto name : names) {
196  if (name == "TRACE_NUMENTS" || name == "TRACE_ARGSMAX"
197  || name == "TRACE_MSGMAX" || name == "TRACE_FILE") // only applicable if env.var. set before before traceInit
198  // don't override and don't "set_to_unset" (if "mapping", want any subprocess to map also)
199  setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
200  // These next 3 are looked at when TRACE_CNTL("namlvlset") is called. And, if mapped, get into file! (so may want to unset env???)
201  else if (name == "TRACE_LIMIT_MS") { // there is also TRACE_CNTL
202  if (!getenv(name.c_str())) {
203  envs_set_to_unset[name] = true;
204  std::vector<uint32_t> limit = trace_pset.get<std::vector<uint32_t>>(name);
205  // could check that it is size()==3???
206  std::string limits = std::to_string(limit[0]) + "," + std::to_string(limit[1]) + "," + std::to_string(limit[2]);
207  setenv(name.c_str(), limits.c_str(), 0);
208  }
209  }
210  else if (name == "TRACE_MODE") { // env.var. only applicable if TRACE_NAMLVLSET is set, BUT could TRACE_CNTL("mode",mode)???
211  if (!getenv(name.c_str())) {
212  envs_set_to_unset[name] = true;
213  setenv(name.c_str(), trace_pset.get<std::string>(name).c_str(), 0);
214  }
215  }
216  else if (name == "TRACE_NAMLVLSET") {
217  if (!getenv(name.c_str())) {
218  envs_set_to_unset[name] = true;
219  std::stringstream lvlsbldr; // levels builder
220  fhicl::ParameterSet lvls_pset = trace_pset.get<fhicl::ParameterSet>(name);
221  std::vector<std::string> tnames = lvls_pset.get_names();
222  for (auto tname : tnames) {
223  lvlsbldr << tname;
224  std::vector<uint64_t> msks = lvls_pset.get<std::vector<uint64_t>>(tname);
225  for (auto msk : msks) {
226  lvlsbldr << " 0x" << std::hex << (unsigned long long)msk;
227  }
228  lvlsbldr << "\n";
229  }
230  setenv(name.c_str(), lvlsbldr.str().c_str(), 0); // 0 means: won't overwrite
231  }
232  }
233  }
234  TRACE_CNTL("namlvlset"); // acts upon env.var.
235  for (auto env : trace_envs) if (envs_set_to_unset[env]) unsetenv(env.c_str());
236 }
237 
238 void artdaq::configureMessageFacility(char const* progname, bool useConsole, bool printDebug)
239 {
240  auto pstr = generateMessageFacilityConfiguration(progname, useConsole, printDebug);
241  fhicl::ParameterSet pset;
242  fhicl::make_ParameterSet(pstr, pset);
243 
244  fhicl::ParameterSet trace_pset;
245  if (!pset.get_if_present<fhicl::ParameterSet>("TRACE", trace_pset)) {
246  fhicl::ParameterSet trace_dflt_pset;
247  fhicl::make_ParameterSet("TRACE:{TRACE_MSGMAX:0 TRACE_LIMIT_MS:[10,500,1500]}", trace_dflt_pset);
248  pset.put<fhicl::ParameterSet>("TRACE", trace_dflt_pset.get<fhicl::ParameterSet>("TRACE"));
249  trace_pset = pset.get<fhicl::ParameterSet>("TRACE");
250  }
251  configureTRACE(trace_pset);
252  pstr = pset.to_string();
253  pset.erase("TRACE");
254 
255 #if CANVAS_HEX_VERSION >= 0x20002 // art v2_07_03 means a new versions of fhicl, boost, etc
256  mf::StartMessageFacility(pset);
257 
258  mf::SetApplicationName(progname);
259 
260  mf::setEnabledState("");
261 # else
262  mf::StartMessageFacility(mf::MessageFacilityService::MultiThread, pset);
263 
264  mf::SetModuleName(progname);
265  mf::SetContext(progname);
266 # endif
267  TLOG(4) << "Message Facility Config input is: " << pstr;
268  TLOG_INFO("configureMessageFacility") << "Message Facility Application " << progname << " configured with: " << pset.to_string();
269 }
270 
271 std::string artdaq::setMsgFacAppName(const std::string& appType, unsigned short port)
272 {
273  std::string appName(appType);
274 
275  char hostname[256];
276  if (gethostname(&hostname[0], 256) == 0)
277  {
278  std::string hostString(hostname);
279  size_t pos = hostString.find(".");
280  if (pos != std::string::npos && pos > 2)
281  {
282  hostString = hostString.substr(0, pos);
283  }
284  appName.append("-");
285  appName.append(hostString);
286  }
287 
288  appName.append("-");
289  appName.append(boost::lexical_cast<std::string>(port));
290 
291  mf::SetApplicationName(appName);
292  return appName;
293 }
void configureTRACE(fhicl::ParameterSet &trace_pset)
Configure TRACE.
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...
std::string setMsgFacAppName(const std::string &appType, unsigned short port)
Set the message facility application name using the specified application type and port number...