tdaq-develop-2025-02-12
MessageFacility.h
1 #ifndef OTSDAQ_CORE_MESSAGEFACILITY_MESSAGEFACILITY_H
2 #define OTSDAQ_CORE_MESSAGEFACILITY_MESSAGEFACILITY_H
3 
4 #include "otsdaq/Macros/CoutMacros.h"
5 
6 #include <messagefacility/MessageLogger/MessageLogger.h>
7 #include "artdaq-core/Utilities/configureMessageFacility.hh"
8 
9 namespace ots
10 {
11 static bool MESSAGE_FACILITY_INITIALIZED;
12 
13 inline void INIT_MF(const char* name)
14 {
15  if(MESSAGE_FACILITY_INITIALIZED)
16  return;
17 
18  char* logRootString = getenv("OTSDAQ_LOG_ROOT");
19  if(logRootString == nullptr)
20  {
21  __COUT_ERR__ << "\n**********************************************************"
22  << std::endl;
23  __COUT_ERR__ << "WARNING: OTSDAQ_LOG_ROOT environment variable was not set!"
24  << std::endl;
25  __COUT_ERR__ << "**********************************************************\n"
26  << std::endl;
27  // exit(0);
28  }
29  else
30  setenv("ARTDAQ_LOG_ROOT", logRootString, 1);
31 
32  char* logFhiclCode = getenv("OTSDAQ_LOG_FHICL");
33  if(logFhiclCode == nullptr)
34  {
35  __COUT_ERR__ << "\n***********************************************************"
36  << std::endl;
37  __COUT_ERR__ << "WARNING: OTSDAQ_LOG_FHICL environment variable was not set!"
38  << std::endl;
39  __COUT_ERR__ << "***********************************************************\n"
40  << std::endl;
41  // exit(0);
42  }
43  else
44  {
45  setenv("ARTDAQ_LOG_FHICL", logFhiclCode, 1);
46 
47  char* userDataString = getenv("USER_DATA");
48  if(userDataString == nullptr)
49  {
50  __COUT_ERR__
51  << "\n***********************************************************"
52  << std::endl;
53  __COUT_ERR__ << "WARNING: USER_DATA environment variable was not set!"
54  << std::endl;
55  __COUT_ERR__
56  << "***********************************************************\n"
57  << std::endl;
58  __SS__ << "WARNING: USER_DATA environment variable was not set!" << std::endl;
59  __SS_THROW__;
60  }
61 
62  setenv(
63  "DAQINTERFACE_MESSAGEFACILITY_FHICL",
64  (std::string(userDataString) +
65  "/MessageFacilityConfigurations/ARTDAQInterfaceMessageFacilityGen.fcl")
66  .c_str(),
67  1);
68  }
69 
70  __COUT__ << "Configuring message facility with " << logFhiclCode << __E__;
71  {
72  FILE* fp = fopen(logFhiclCode, "r");
73  unsigned int charCount = 0;
74  while(fp)
75  {
76  ++charCount;
77  char line[100];
78  while(fgets(line, 100, fp))
79  {
80  std::cout << line;
81  charCount += strlen(line);
82  }
83  std::cout << __E__;
84 
85  fclose(fp);
86  fp = 0;
87 
88  if(charCount < 10)
89  {
90  std::cout << "Retry - Was file really empty? " << logFhiclCode << __E__;
91  sleep(1);
92  fp = fopen(logFhiclCode, "r");
93  }
94  }
95  }
96  artdaq::configureMessageFacility(name /*application name*/,
97  false /*cout display*/,
98  true /*enable debug messages*/);
99 
100  artdaq::setMsgFacAppName(name, 0);
101 
102  MESSAGE_FACILITY_INITIALIZED = true;
103 
104 } // end INIT_MF()
105 
106 } // namespace ots
107 
108 #endif
void INIT_MF(const char *name)