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