Jpp  18.3.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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

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 }
ControlHost prefix.
Definition: JPrefix.hh:31
Utility class to parse command line options.
Definition: JParser.hh:1514
#define WARNING(A)
Definition: JMessage.hh:65
ControlHost class.
void set(const bool utc=false)
Set to current local time.
static const std::string MESSAGE_TAG
Message logging tag.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
Subscription list.
then usage $script[port]< option > nPossible stop
Auxiliary class for time values.
Definition: JTimeval.hh:26
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
#define ERROR(A)
Definition: JMessage.hh:66
then awk string
JSubscriptionList & add(const JSubscription &subscription)
Add subscription.
Exception for ControlHost.
Definition: JException.hh:484
$WORKDIR driver txt done cat $WORKDIR driver txt<< EOFprocess ${DATAFILTER}$FILTER_HOST csh-c '(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&(JDataFilter-H\$SERVER\$-M\$LOGGER\$-d $DEBUG-u ${DATAFILTER}-P $PORT</dev/null > &/dev/null &))';process ${DATAWRITER}$WRITER_HOST csh-c '(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&(JDataWriter-H\$SERVER\$-M\$LOGGER\$-d $DEBUG-u ${DATAWRITER}</dev/null > &/dev/null &))';print enterevent ev_init{RC_CMD}event ev_reset{RC_CMD}event ev_init{RC_CMD}event ev_configure{RC_DFLTR%<$WORKDIR/ev_configure_datafilter.txt > RC_DQSIM<$WORKDIR/ev_configure_dqsimulator.txt > RC_DWRT path
Auxiliary class for all subscription.
Definition: JControlHost.hh:97
double getElapsedTime(const JDateAndTime &object) const
Get elapsed time to given date and time.
#define FATAL(A)
Definition: JMessage.hh:67
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:48
Auxiliary class for date and time.
Definition: JDateAndTime.hh:78
int getHour() const
hours after midnight [0-23]
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62