otsdaq  v1_01_02
 All Classes Namespaces Functions
configureMessageFacility.cc
1 #include "otsdaq-core/MessageFacility/configureMessageFacility.hh"
2 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
3 #include "messagefacility/MessageLogger/MessageLogger.h"
4 #include "fhiclcpp/make_ParameterSet.h"
5 #include <boost/filesystem.hpp>
6 #include <unistd.h>
7 #include <fstream>
8 #include <sstream>
9 #include <iostream>
10 
11 namespace BFS = boost::filesystem;
12 
13 void ots::configureMessageFacility(char const* progname)
14 {
15  if(!messagefacility_initialized)
16  {
17  std::cout << __COUT_HDR_FL__ << "#######################################################################" << std::endl;
18  std::cout << __COUT_HDR_FL__ << "Initializing MessageFacility with progname " << progname << std::endl;
19  std::cout << __COUT_HDR_FL__ << "#######################################################################" << std::endl;
20  std::string logPathProblem = "";
21  std::string logfileName = "";
22  char* logRootString = getenv("OTSDAQ_LOG_ROOT");
23  char* logFhiclCode = getenv("OTSDAQ_LOG_FHICL");
24 
25  if (logRootString != nullptr)
26  {
27  if (! BFS::exists(logRootString))
28  {
29  logPathProblem = "Log file root directory ";
30  logPathProblem.append(logRootString);
31  logPathProblem.append(" does not exist!");
32  }
33  logfileName = std::string(logRootString);
34  }
35 
36 
37  std::ostringstream ss;
38  ss << "debugModules:[\"*\"] statistics:[\"stats\"] "
39  << " destinations : { "
40  // << " console : { "
41  // << " type : \"cout\" threshold : \"DEBUG\" "
42  // << " noTimeStamps : true noLineBreaks: true"
43  // << " } "
44  ;
45 
46  /*if (logfileName.length() > 0)
47  {
48  ss << " multifile : { "
49  << " type : \"MultiFile\" threshold : \"DEBUG\" "
50  << " base_directory : \"" << logfileName << "\" "
51  << " append : true "
52  << " use_hostname: true use_application: true "
53  << " } ";
54  }*/
55 
56  if (logFhiclCode != nullptr)
57  {
58  std::ifstream logfhicl( logFhiclCode );
59 
60  if ( logfhicl.is_open() )
61  {
62  std::stringstream fhiclstream;
63  fhiclstream << logfhicl.rdbuf();
64  ss << fhiclstream.str();
65  } else
66  {
67  //throw cet::exception("configureMessageFacility")
68  std::cout << __COUT_HDR_FL__ <<
69  "Unable to open requested fhicl file \"" <<
70  logFhiclCode << "\".";
71  return;
72  }
73  }
74 
75  ss << " } ";
76 
77  std::cout << __COUT_HDR_FL__ << "Configuring MessageFacility with Parameter Set: " << ss.str() << std::endl;
78  fhicl::ParameterSet pset;
79  std::string pstr(ss.str());
80  fhicl::make_ParameterSet(pstr, pset);
81 
82  mf::StartMessageFacility(mf::MessageFacilityService::MultiThread,
83  pset);
84 
85  if (logPathProblem.size() > 0)
86  {
87  mf::LogError(progname) << logPathProblem;
88  }
89  messagefacility_initialized = true;
90  }
91 }
92 
93 void ots::setMsgFacAppName(const std::string& appType)
94 {
95  std::string appName(appType);
96 
97  char hostname[256];
98  if (gethostname(&hostname[0], 256) == 0) {
99  std::string hostString(hostname);
100  size_t pos = hostString.find(".");
101  if (pos != std::string::npos && pos > 2) {
102  hostString = hostString.substr(0, pos);
103  }
104  appName.append("-");
105  appName.append(hostString);
106  }
107  mf::SetApplicationName(appName);
108  mf::SetModuleName(appName);
109  mf::SetContext(appName);
110 }