Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
JLogger.cc
Go to the documentation of this file.
1#include <string>
2#include <iostream>
3#include <fstream>
4#include <sstream>
5#include <iomanip>
6#include <set>
7#include <filesystem>
8
10#include "JNet/JControlHost.hh"
11
12#include "JLang/JTimeval.hh"
13#include "JLang/JException.hh"
14#include "JLang/gzstream.h"
16
17#include "Jeep/JeepToolkit.hh"
18#include "Jeep/JParser.hh"
19#include "Jeep/JMessage.hh"
20
21
22namespace {
23
24 using namespace JPP;
25
26
27 const std::string save = "save"; //!< Close and re-open file;
28 const std::string stop = "stop"; //!< Stop process.
29
30
31 const int MINIMUM_NUMBER_OF_MESSAGES = 100;
32 const double MINIMUM_ELAPSED_TIME_S = 60*60;
33
34
35 /**
36 * Logger file.
37 */
38 struct JLoggerFile :
39 public ogzstream,
40 public JDateAndTime
41 {
42 static const int MAXIMUM_FILE_NUMBER = 100;
43
44 /**
45 * Constructor.
46 *
47 * \param path path
48 */
49 JLoggerFile(const std::string& path) :
50 path(path)
51 {}
52
53
54 /**
55 * Get current file name.
56 *
57 * \return file name
58 */
59 std::string getFilename() const
60 {
61 return file_name;
62 }
63
64
65 /**
66 * Open file.
67 */
68 void open()
69 {
70 using namespace std;
71
73
74 for (int i = 0; !this->is_open() && i != MAXIMUM_FILE_NUMBER; ++i) {
75
76 ostringstream os;
77
78 os << getFullPath(path)
79 << "KM3NeT"
80 << "_" << this->getYear() << '-' << FILL(2,'0') << this->getMonth() << '-' << FILL(2,'0') << this->getDay();
81
82 if (i != 0) {
83 os << "_" << i;
84 }
85
86 os << ".txt.gz";
87
88 file_name = os.str();
89
90 error_code error;
91
92 if (!filesystem::is_regular_file(file_name, error)) {
93 ogzstream::open(file_name.c_str());
94 }
95 }
96 }
97
98
99 /**
100 * Close file.
101 */
102 void close()
103 {
104 using namespace std;
105
107
108 file_name = "";
109 }
110
111 protected:
112 std::string path;
113 std::string file_name;
114 };
115}
116
117
118/**
119 * \file
120 *
121 * Auxiliary program to save logger messages from ControlHost server.
122 *
123 * The option <tt>-H <hostname>[:port]</tt> correponds to the hostname and the port of the server, respectively.
124 * Each message tag will be written to file in the directory specified.
125 * The program will terminate when it receives message <stop>.
126 * \author mdejong
127 */
128int main(int argc, const char *argv[])
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}
261
262
Date and time functions.
Exceptions.
General purpose message reporting.
General purpose messaging.
#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
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2107
Auxiliary methods for handling file names, type names and environment.
Exception for ControlHost.
static void Throw(const bool option)
Definition JThrow.hh:37
Auxiliary class for time values.
Definition JTimeval.hh:29
ControlHost class.
int GetFullString(std::string &buffer)
Receive string.
int CheckHead(JPrefix &prefix, JTimeval timeout=JTimeval::min())
Check for header, without waiting.
int SendMeAlways()
Tell server to send messages forever.
int Subscribe(const JSubscription &subscription)
Subscribe to single tag.
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
void close()
Definition gzstream.h:185
void open(const char *name, int open_mode=std::ios::out)
Definition gzstream.h:218
std::string getFullPath(const std::string &path)
Get full path, i.e. add JEEP::PATHNAME_SEPARATOR if necessary.
void close(std::istream *pf)
Close file.
std::string getFilename(const std::string &file_name)
Get file name part, i.e. part after last JEEP::PATHNAME_SEPARATOR if any.
T * open(const std::string &file_name)
Open file.
static const std::string MESSAGE_TAG
Message logging tag.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for sequence of same character.
Definition JManip.hh:330
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.