artdaq  v2_03_02
 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 #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>
6 #endif
7 #include "fhiclcpp/make_ParameterSet.h"
8 #include <boost/filesystem.hpp>
9 #include <unistd.h>
10 #include <fstream>
11 #include <sstream>
12 #include "trace.h" // TRACE_CNTL, TRACE
13 
14 namespace BFS = boost::filesystem;
15 
16 void artdaq::configureMessageFacility(char const* progname, bool useConsole)
17 {
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");
23 
24 #if 0
25  setenv( "TRACE_LVLS", "0xf", 0/*0 = no overwrite*/ );
26  unsigned long long lvls=strtoull( getenv("TRACE_LVLS"), NULL, 0 );
27  // NOTE: If TRACEs occur before this, they would be done with a different lvl S mask.
28  // To check this, one could: treset;tonMg 0-63; tcntl trig -nTRACE 4 50; <app>
29  // checking for TRACEs before the TRACE below.
30  // If an existing trace file is used, the value of modeS is unchanged.
31  TRACE_CNTL( "lvlmskSg", lvls );
32  TRACE( 4, "configureMessageFacilit lvlmskSg set to 0x%llx", lvls );
33 #endif
34 
35  if (logRootString != nullptr)
36  {
37  if (!BFS::exists(logRootString))
38  {
39  logPathProblem = "Log file root directory ";
40  logPathProblem.append(logRootString);
41  logPathProblem.append(" does not exist!");
42  }
43  else
44  {
45  std::string logfileDir(logRootString);
46  logfileDir.append("/");
47  logfileDir.append(progname);
48  if (!BFS::exists(logfileDir))
49  {
50  logPathProblem = "Log file directory ";
51  logPathProblem.append(logfileDir);
52  logPathProblem.append(" does not exist!");
53  }
54  else
55  {
56  time_t rawtime;
57  struct tm* timeinfo;
58  char timeBuff[256];
59  time(&rawtime);
60  timeinfo = localtime(&rawtime);
61  strftime(timeBuff, 256, "%Y%m%d%H%M%S", timeinfo);
62 
63  char hostname[256];
64  std::string hostString = "";
65  if (gethostname(&hostname[0], 256) == 0)
66  {
67  std::string tmpString(hostname);
68  hostString = tmpString;
69  size_t pos = hostString.find(".");
70  if (pos != std::string::npos && pos > 2)
71  {
72  hostString = hostString.substr(0, pos);
73  }
74  }
75 
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)
83  {
84  logfileName.append(hostString);
85  logfileName.append("-");
86  }
87  logfileName.append(boost::lexical_cast<std::string>(getpid()));
88  logfileName.append(".log");
89  }
90  }
91  }
92 
93  std::ostringstream ss;
94  ss << "debugModules:[\"*\"] statistics:[\"stats\"] "
95  << " destinations : { ";
96 
97  if (useConsole) {
98  if (artdaqMfextensionsDir != nullptr)
99  {
100  ss << " console : { "
101  << " type : \"ANSI\" threshold : \"INFO\" "
102  << " noTimeStamps : true "
103  << " bell_on_error: true "
104  << " } ";
105  }
106  else
107  {
108  ss << " console : { "
109  << " type : \"cout\" threshold : \"INFO\" "
110  << " noTimeStamps : true "
111  << " } ";
112  }
113  }
114 
115  if (logfileName.length() > 0)
116  {
117  ss << " file : { "
118  << " type : \"file\" threshold : \"DEBUG\" "
119  << " filename : \"" << logfileName << "\" "
120  << " append : false "
121  << " } ";
122  }
123 
124  if (artdaqMfextensionsDir != nullptr)
125  {
126  ss << " trace : { "
127  << " type : \"TRACE\" threshold : \"DEBUG\" format:{noLineBreaks: true} lvls: 0x7 lvlm: 0xF"
128  << " }";
129  }
130 
131  if (logFhiclCode != nullptr)
132  {
133  std::ifstream logfhicl(logFhiclCode);
134 
135  if (logfhicl.is_open())
136  {
137  std::stringstream fhiclstream;
138  fhiclstream << logfhicl.rdbuf();
139  ss << fhiclstream.str();
140  }
141  else
142  {
143  throw cet::exception("configureMessageFacility") <<
144  "Unable to open requested fhicl file \"" <<
145  logFhiclCode << "\".";
146  }
147  }
148 
149  ss << " } ";
150 
151  fhicl::ParameterSet pset;
152  std::string pstr(ss.str());
153  //std::cout << "Message Facility Config is: " << pstr << std::endl;
154  fhicl::make_ParameterSet(pstr, pset);
155 
156 # if ART_HEX_VERSION >= 0x20703 // art v2_07_03 means a new versions of fhicl, boost, etc
157  mf::StartMessageFacility(pset);
158 
159  mf::SetApplicationName(progname);
160 # else
161  mf::StartMessageFacility(mf::MessageFacilityService::MultiThread,
162  pset);
163 
164  mf::SetModuleName(progname);
165  mf::SetContext(progname);
166 # endif
167 
168  if (logPathProblem.size() > 0)
169  {
170  mf::LogError(progname) << logPathProblem;
171  }
172 }
173 
174 void artdaq::setMsgFacAppName(const std::string& appType, unsigned short port)
175 {
176  std::string appName(appType);
177 
178  char hostname[256];
179  if (gethostname(&hostname[0], 256) == 0)
180  {
181  std::string hostString(hostname);
182  size_t pos = hostString.find(".");
183  if (pos != std::string::npos && pos > 2)
184  {
185  hostString = hostString.substr(0, pos);
186  }
187  appName.append("-");
188  appName.append(hostString);
189  }
190 
191  appName.append("-");
192  appName.append(boost::lexical_cast<std::string>(port));
193 
194  mf::SetApplicationName(appName);
195 }
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...