Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
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 <filesystem>
#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 "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 128 of file JLogger.cc.

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