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