Jpp  master_rocky-43-ge265d140c
the software that should make you happy
Functions
JLogger.cc File Reference

Auxiliary program to save logger messages from ControlHost server. More...

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <set>
#include "JLogger/JMessageLogger.hh"
#include "JNet/JControlHost.hh"
#include "JLang/JTimeval.hh"
#include "JLang/JException.hh"
#include "JLang/gzstream.h"
#include "JSystem/JDateAndTime.hh"
#include "JSystem/JStat.hh"
#include "Jeep/JeepToolkit.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, const char *argv[])
 

Detailed Description

Auxiliary program to save logger messages from ControlHost server.

The option -H <hostname>[:port] correponds to the hostname and the port of the server, respectively. Each message tag will be written to file in the directory specified. The program will terminate when it receives message <stop>.

Author
mdejong

Definition in file JLogger.cc.

Function Documentation

◆ main()

int main ( int  argc,
const char *  argv[] 
)

Definition at line 126 of file JLogger.cc.

127 {
128  using namespace std;
129  using namespace JPP;
130 
131  string hostname;
132  set<JTag> taglist;
133  string path;
134  JTimeval timeout;
135  set<int> happy_hour;
136  int debug;
137 
138  taglist.insert(MESSAGE_TAG);
139 
140  try {
141 
142  JParser<> zap("Auxiliary program to save logger messages from ControlHost server.");
143 
144  zap['H'] = make_field(hostname) = "localhost";
145  zap['T'] = make_field(taglist) = JPARSER::initialised();
146  zap['D'] = make_field(path) = "/tmp/";
147  zap['t'] = make_field(timeout) = JTimeval(1, 0);
148  zap['W'] = make_field(happy_hour) = JPARSER::initialised();
149  zap['d'] = make_field(debug) = 1;
150 
151  zap(argc, argv);
152  }
153  catch(const exception &error) {
154  FATAL(error.what() << endl);
155  }
156 
157  if (happy_hour.empty()) {
158 
159  WARNING("No happy hours (option -W); set to midnight." << endl);
160 
161  happy_hour.insert(0);
162  }
163 
164 
165  JControlHost::Throw(true);
166 
167  try {
168 
169  JControlHost in(hostname);
170 
171  {
172  JSubscriptionList buffer;
173 
174  for (set<JTag>::const_iterator i = taglist.begin(); i != taglist.end(); ++i) {
175  buffer.add(JSubscriptionAll(*i));
176  }
177 
178  in.Subscribe(buffer);
179  in.SendMeAlways();
180  }
181 
182 
183  JLoggerFile out(path);
184 
185  out.open();
186 
187  DEBUG("Open file " << out.getFilename() << endl);
188 
189  JPrefix prefix;
190  string buffer;
191 
192  JDateAndTime clock;
193 
194  for (int number_of_messages; buffer.size() != stop.size() || string(buffer.data(), stop.size()) != stop; ) {
195 
196  const int check = in.CheckHead(prefix, timeout);
197 
198  DEBUG("Check head " << check << endl);
199 
200  if (check < 0) {
201 
202  FATAL("Error at JControlHost::CheckHead " << check << endl);
203 
204  } else if (check == 1) {
205 
206  in.GetFullString(buffer);
207 
208  DEBUG("Message <" << buffer << ">" << endl);
209 
210  if (buffer == stop) {
211 
212  break;
213 
214  } else if (buffer == save) {
215 
216  DEBUG("Close file " << out.getFilename() << endl);
217 
218  out.close();
219  out.open();
220 
221  DEBUG("Open file " << out.getFilename() << endl);
222 
223  } else {
224 
225  ++number_of_messages;
226 
227  if ( ! (out << buffer << endl)) {
228  FATAL("Error writing to file " << out.getFilename() << endl);
229  }
230  }
231  }
232 
233  if (check == 0 || number_of_messages > MINIMUM_NUMBER_OF_MESSAGES) {
234 
235  number_of_messages = 0;
236 
237  clock.set();
238 
239  DEBUG("Test " << setw(2) << clock.getHour() << ' ' << happy_hour.count(clock.getHour()) << ' ' << clock.getElapsedTime(out) << endl);
240 
241  if (happy_hour.count(clock.getHour()) != 0 && clock.getElapsedTime(out) > MINIMUM_ELAPSED_TIME_S) {
242 
243  DEBUG("Close file " << out.getFilename() << endl);
244 
245  out.close();
246  out.open();
247 
248  DEBUG("Open file " << out.getFilename() << endl);
249  }
250  }
251  }
252 
253  out.close();
254  }
255  catch(const JControlHostException& error) {
256  ERROR(error << endl);
257  }
258 }
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
#define ERROR(A)
Definition: JMessage.hh:66
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
#define WARNING(A)
Definition: JMessage.hh:65
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2142
Exception for ControlHost.
Definition: JException.hh:486
Auxiliary class for time values.
Definition: JTimeval.hh:29
ControlHost class.
ControlHost prefix.
Definition: JPrefix.hh:33
Subscription list.
JSubscriptionList & add(const JSubscription &subscription)
Add subscription.
Utility class to parse command line options.
Definition: JParser.hh:1698
static const std::string MESSAGE_TAG
Message logging tag.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
Auxiliary class for all subscription.
Definition: JControlHost.hh:99
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:68
Auxiliary class for date and time.
Definition: JDateAndTime.hh:80
double getElapsedTime(const JDateAndTime &object) const
Get elapsed time to given date and time.
int getHour() const
hours after midnight [0-23]
void set(const bool utc=false)
Set to current local time.