00001 #include "otsdaq-core/MessageFacility/configureMessageFacility.hh"
00002 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00003 #include "messagefacility/MessageLogger/MessageLogger.h"
00004 #include "fhiclcpp/make_ParameterSet.h"
00005 #include <boost/filesystem.hpp>
00006 #include <unistd.h>
00007 #include <fstream>
00008 #include <sstream>
00009 #include <iostream>
00010
00011 namespace BFS = boost::filesystem;
00012
00013 void ots::configureMessageFacility(char const* progname)
00014 {
00015 if(!messagefacility_initialized)
00016 {
00017 std::cout << __COUT_HDR_FL__ << "#######################################################################" << std::endl;
00018 std::cout << __COUT_HDR_FL__ << "Initializing MessageFacility with progname " << progname << std::endl;
00019 std::cout << __COUT_HDR_FL__ << "#######################################################################" << std::endl;
00020 std::string logPathProblem = "";
00021 std::string logfileName = "";
00022 char* logRootString = getenv("OTSDAQ_LOG_ROOT");
00023 char* logFhiclCode = getenv("OTSDAQ_LOG_FHICL");
00024
00025 if (logRootString != nullptr)
00026 {
00027 if (! BFS::exists(logRootString))
00028 {
00029 logPathProblem = "Log file root directory ";
00030 logPathProblem.append(logRootString);
00031 logPathProblem.append(" does not exist!");
00032 }
00033 logfileName = std::string(logRootString);
00034 }
00035
00036
00037 std::ostringstream ss;
00038 ss << "debugModules:[\"*\"] statistics:[\"stats\"] "
00039 << " destinations : { "
00040
00041
00042
00043
00044 ;
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 if (logFhiclCode != nullptr)
00057 {
00058 std::ifstream logfhicl( logFhiclCode );
00059
00060 if ( logfhicl.is_open() )
00061 {
00062 std::stringstream fhiclstream;
00063 fhiclstream << logfhicl.rdbuf();
00064 ss << fhiclstream.str();
00065 } else
00066 {
00067
00068 std::cout << __COUT_HDR_FL__ <<
00069 "Unable to open requested fhicl file \"" <<
00070 logFhiclCode << "\".";
00071 return;
00072 }
00073 }
00074
00075 ss << " } ";
00076
00077 std::cout << __COUT_HDR_FL__ << "Configuring MessageFacility with Parameter Set: " << ss.str() << std::endl;
00078 fhicl::ParameterSet pset;
00079 std::string pstr(ss.str());
00080 fhicl::make_ParameterSet(pstr, pset);
00081
00082 mf::StartMessageFacility(mf::MessageFacilityService::MultiThread,
00083 pset);
00084
00085 if (logPathProblem.size() > 0)
00086 {
00087 mf::LogError(progname) << logPathProblem;
00088 }
00089 messagefacility_initialized = true;
00090 }
00091 }
00092
00093 void ots::setMsgFacAppName(const std::string& appType)
00094 {
00095 std::string appName(appType);
00096
00097 char hostname[256];
00098 if (gethostname(&hostname[0], 256) == 0) {
00099 std::string hostString(hostname);
00100 size_t pos = hostString.find(".");
00101 if (pos != std::string::npos && pos > 2) {
00102 hostString = hostString.substr(0, pos);
00103 }
00104 appName.append("-");
00105 appName.append(hostString);
00106 }
00107 mf::SetApplicationName(appName);
00108 mf::SetModuleName(appName);
00109 mf::SetContext(appName);
00110 }