otsdaq  v2_05_02_indev
MessageFacilityTable_table.cc
1 #include "otsdaq/ConfigurationInterface/ConfigurationManager.h"
2 #include "otsdaq/Macros/TablePluginMacros.h"
3 #include "otsdaq/TablePlugins/MessageFacilityTable.h"
4 
5 #include <stdio.h>
6 #include <fstream> // std::fstream
7 #include <iostream>
8 using namespace ots;
9 
10 // clang-format off
11 #define MF_CFG_FILE std::string(__ENV__("USER_DATA")) + "/MessageFacilityConfigurations/MessageFacilityGen.fcl"
12 #define MF_ARTDAQ_INTERFACE_CFG_FILE std::string(__ENV__("USER_DATA")) + "/MessageFacilityConfigurations/ARTDAQInterfaceMessageFacilityGen.fcl"
13 #define QT_CFG_FILE std::string(__ENV__("USER_DATA")) + "/MessageFacilityConfigurations/QTMessageViewerGen.fcl"
14 #define QUIET_CFG_FILE std::string(__ENV__("USER_DATA")) + "/MessageFacilityConfigurations/QuietForwarderGen.cfg"
15 #define USE_WEB_BOOL_FILE std::string(__ENV__("USER_DATA")) + "/MessageFacilityConfigurations/UseWebConsole.bool"
16 #define USE_QT_BOOL_FILE std::string(__ENV__("USER_DATA")) + "/MessageFacilityConfigurations/UseQTViewer.bool"
17 
18 // MessageFacilityTable Column names
19 #define COL_NAME "UID"
20 #define COL_STATUS TableViewColumnInfo::COL_NAME_STATUS
21 #define COL_ENABLE_FWD "EnableUDPForwarding"
22 
23 #define COL_USE_WEB "ForwardToWebConsoleGUI"
24 #define COL_WEB_IP "WebConsoleForwardingIPAddress"
25 #define COL_WEB_PORT0 "WebConsoleForwardingPort0"
26 #define COL_WEB_PORT1 "WebConsoleForwardingPort1"
27 
28 #define COL_USE_QT "ForwardToQTViewerGUI"
29 #define COL_QT_IP "QTViewerForwardingIPAddress"
30 #define COL_QT_PORT "QTViewerForwardingPort"
31 
32 // clang-format on
33 
34 MessageFacilityTable::MessageFacilityTable(void) : TableBase("MessageFacilityTable")
35 {
37  // WARNING: the names used in C++ MUST match the Table INFO //
39 }
40 
41 MessageFacilityTable::~MessageFacilityTable(void) {}
42 
43 void MessageFacilityTable::init(ConfigurationManager* configManager)
44 {
45  // use isFirstAppInContext to only run once per context, for example to avoid
46  // generating files on local disk multiple times.
47  bool isFirstAppInContext = configManager->isOwnerFirstAppInContext();
48 
49  //__COUTV__(isFirstAppInContext);
50  if(!isFirstAppInContext)
51  return;
52 
53  // __COUT__ << "*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*" << std::endl;
54  // __COUT__ << configManager->__SELF_NODE__ << std::endl;
55 
56  bool status, enableFwd, useWeb, useQT;
57  int fwdPort;
58  std::string fwdIP;
59 
60  auto childrenMap = configManager->__SELF_NODE__.getChildren();
61 
62  std::stringstream fclSs;
63 
64  // loop through all children just to be same as other tables
65  // exit loop after first active one
66  for(auto& child : childrenMap)
67  {
68  child.second.getNode(COL_STATUS).getValue(status);
69 
70  if(!status)
71  continue; // skip inactive rows
72 
73  child.second.getNode(COL_ENABLE_FWD).getValue(enableFwd);
74 
75  child.second.getNode(COL_USE_WEB).getValue(useWeb);
76  child.second.getNode(COL_USE_QT).getValue(useQT);
77 
78  if(useWeb && useQT)
79  {
80  __SS__ << "Illegal Message Facility table: "
81  << "Can only enable Web Console or QT Viewer, not both." << std::endl;
82  __SS_THROW__;
83  }
84 
85  std::fstream bfs;
86  // output use web bool for StartOTS.sh
87  bfs.open(USE_WEB_BOOL_FILE, std::fstream::out | std::fstream::trunc);
88  if(bfs.fail())
89  {
90  __SS__ << "Failed to open boolean Use of Web Console table file: " << USE_WEB_BOOL_FILE << std::endl;
91  __SS_THROW__;
92  }
93  bfs << (useWeb ? 1 : 0);
94  bfs.close();
95 
96  // output use web bool for StartOTS.sh
97  bfs.open(USE_QT_BOOL_FILE, std::fstream::out | std::fstream::trunc);
98  if(bfs.fail())
99  {
100  __SS__ << "Failed to open boolean Use of QT Viewer table file: " << USE_QT_BOOL_FILE << std::endl;
101  __SS_THROW__;
102  }
103  bfs << (useQT ? 1 : 0);
104  bfs.close();
105 
106  if(enableFwd) // write udp forward config
107  {
108  // handle using web gui
109  if(useWeb)
110  {
111  __COUT__ << "Forwarding to Web GUI with UDP forward MesageFacility "
112  "table."
113  << std::endl;
114 
115  child.second.getNode(COL_WEB_PORT0).getValue(fwdPort);
116  child.second.getNode(COL_WEB_IP).getValue(fwdIP);
117 
118  fclSs << "udp: {\n";
119  fclSs << "\t"
120  << "type: UDP\n";
121  fclSs << "\t"
122  << "threshold: DEBUG\n";
123  fclSs << "\t"
124  << "port: " << fwdPort << "\n";
125  fclSs << "\t"
126  << "host: \"" << fwdIP << "\"\n";
127  fclSs << "}\n";
128 
129  fclSs << "console: {\n";
130  fclSs << "\t"
131  << "type: \"OTS\"\n";
132  fclSs << "\t"
133  << "threshold: \"DEBUG\"\n";
134  fclSs << "\t"
135  << "filename_delimit: \"/srcs/\"\n";
136  fclSs << "\t"
137  << "format_string: \"|%L:%N:%f [%u]\t%m\"\n";
138 
139  fclSs << "\n}\n";
140 
141  // output quiet forwarder config file
142  std::fstream qtfs;
143  qtfs.open(QUIET_CFG_FILE, std::fstream::out | std::fstream::trunc);
144  if(qtfs.fail())
145  {
146  __SS__ << "Failed to open Web Console's 'Quiet Forwarder' "
147  "table file: "
148  << QUIET_CFG_FILE << std::endl;
149  __SS_THROW__;
150  }
151  qtfs << "RECEIVE_PORT \t " << fwdPort << "\n";
152  child.second.getNode(COL_WEB_PORT1).getValue(fwdPort);
153  qtfs << "DESTINATION_PORT \t " << fwdPort << "\n";
154  qtfs << "DESTINATION_IP \t " << fwdIP << "\n";
155  qtfs.close();
156  }
157 
158  // handle using qt viewer
159  if(useQT)
160  {
161  __COUT__ << "Forwarding to Web GUI with UDP forward MesageFacility "
162  "table."
163  << std::endl;
164 
165  child.second.getNode(COL_QT_PORT).getValue(fwdPort);
166  child.second.getNode(COL_QT_IP).getValue(fwdIP);
167 
168  fclSs << "udp: {\n";
169  fclSs << "\t"
170  << "type: UDP\n";
171  fclSs << "\t"
172  << "threshold: DEBUG\n";
173  fclSs << "\t"
174  << "port: " << fwdPort << "\n";
175  fclSs << "\t"
176  << "host: \"" << fwdIP << "\"\n";
177  fclSs << "}\n";
178 
179  // output QT Viewer config file
180  std::fstream qtfs;
181  qtfs.open(QT_CFG_FILE, std::fstream::out | std::fstream::trunc);
182  if(qtfs.fail())
183  {
184  __SS__ << "Failed to open QT Message Viewer table file: " << QT_CFG_FILE << std::endl;
185  __SS_THROW__;
186  }
187  qtfs << "receivers: \n{\n";
188  qtfs << "\t"
189  << "syslog: \n{\n";
190  qtfs << "\t\t"
191  << "receiverType: "
192  << "\"UDP\""
193  << "\n";
194  qtfs << "\t\t"
195  << "port: " << fwdPort << "\n";
196  qtfs << "\t}\n"; // close syslog
197  qtfs << "}\n"; // close receivers
198  qtfs << "\n";
199  qtfs << "threshold: "
200  << "DEBUG"
201  << "\n";
202  qtfs.close();
203  }
204  }
205  else // write cout config (not forwarding to external process)
206  {
207  __COUT__ << "Using cout-only MesageFacility table." << std::endl;
208  fclSs << "console: {\n";
209  fclSs << "\t"
210  << "type: \"OTS\"\n";
211  fclSs << "\t"
212  << "threshold: \"DEBUG\"\n";
213  fclSs << "\t"
214  << "filename_delimit: \"/srcs/\"\n";
215  fclSs << "\t"
216  << "format_string: \"|%L:%N:%f [%u]\t%m\"\n";
217 
218  fclSs << "\n}\n";
219  }
220 
221  break; // take first enable row only!
222  } // end record loop
223 
224  // generate MF_CFG_FILE file
225  std::fstream fs;
226  fs.open(MF_CFG_FILE, std::fstream::out | std::fstream::trunc);
227  if(fs.fail())
228  {
229  __SS__ << "Failed to open Message Facility table file: " << MF_CFG_FILE << __E__;
230  __SS_THROW__;
231  }
232  else
233  __COUT__ << "Opened.. " << MF_CFG_FILE << __E__;
234 
235  // generate MF_CFG_MF_ARTDAQ_INTERFACE_CFG_FILE file
236  std::fstream artdaqfs;
237  artdaqfs.open(MF_ARTDAQ_INTERFACE_CFG_FILE, std::fstream::out | std::fstream::trunc);
238  if(artdaqfs.fail())
239  {
240  __SS__ << "Failed to open artdaq interface Message Facility table file: " << MF_ARTDAQ_INTERFACE_CFG_FILE << __E__;
241  __SS_THROW__;
242  }
243  else
244  __COUT__ << "Opened for artdaq.. " << MF_ARTDAQ_INTERFACE_CFG_FILE << __E__;
245 
246  // close MF config files
247  artdaqfs << fclSs.str();
248  //fs << fclSs.str() << "\nfile: {type:\"file\" filename:\"/dev/null\"}\n";
249  fs << fclSs.str() << "\nfile: \"\"\n";
250  fs.close();
251  artdaqfs.close();
252 
253 } // end init()
254 
255 DEFINE_OTS_TABLE(MessageFacilityTable)