Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
9 #include "JNet/JControlHost.hh"
10 
11 #include "JLang/JTimeval.hh"
12 #include "JLang/JException.hh"
13 #include "JLang/gzstream.h"
14 #include "JSystem/JDate.hh"
15 #include "JSystem/JStat.hh"
16 
17 #include "Jeep/JeepToolkit.hh"
18 #include "Jeep/JParser.hh"
19 #include "Jeep/JMessage.hh"
20 
21 
22 namespace {
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  if (!getFileStatus(file_name.c_str())) {
91  ogzstream::open(file_name.c_str());
92  }
93  }
94  }
95 
96 
97  /**
98  * Close file.
99  */
100  void close()
101  {
102  using namespace std;
103 
105 
106  file_name = "";
107  }
108 
109  protected:
110  std::string path;
111  std::string file_name;
112  };
113 }
114 
115 
116 /**
117  * \file
118  *
119  * Auxiliary program to save logger messages from ControlHost server.
120  *
121  * The option <tt>-H <hostname>[:port]</tt> correponds to the hostname and the port of the server, respectively.
122  * Each message tag will be written to file in the directory specified.
123  * The program will terminate when it receives message <stop>.
124  * \author mdejong
125  */
126 int main(int argc, const char *argv[])
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 }
259 
260 
ControlHost prefix.
Definition: JPrefix.hh:31
Utility class to parse command line options.
Definition: JParser.hh:1493
#define WARNING(A)
Definition: JMessage.hh:65
Exceptions.
void set() const
Set to actual time.
ControlHost class.
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
static const std::string MESSAGE_TAG
Message logging tag.
int Subscribe(const JSubscription &subscription)
Subscribe to single tag.
int SendMeAlways()
Tell server to send messages forever.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:63
std::string getFullPath(const std::string &path)
Get full path, i.e. add JEEP::PATHNAME_SEPARATOR if necessary.
Definition: JeepToolkit.hh:128
Subscription list.
T * open(const std::string &file_name)
Open file.
Definition: JeepToolkit.hh:306
Auxiliary class for time values.
Definition: JTimeval.hh:26
int CheckHead(JPrefix &prefix, JTimeval timeout=JTimeval::min())
Check for header, without waiting.
Date and time functions.
do cat driver txt<< EOFevent ev_configure{RC_EVT%< ev_configure.txt > RC_DWRT path
void open(const char *name, int open_mode=std::ios::out)
Definition: gzstream.h:218
int GetFullString(std::string &buffer)
Receive string.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
void close(std::istream *pf)
Close file.
Definition: JeepToolkit.hh:346
Auxiliary methods for handling file names, type names and environment.
#define ERROR(A)
Definition: JMessage.hh:66
JSubscriptionList & add(const JSubscription &subscription)
Add subscription.
Exception for ControlHost.
Definition: JException.hh:450
int debug
debug level
Definition: JSirene.cc:61
Auxiliary class for all subscription.
Definition: JControlHost.hh:96
General purpose messaging.
Auxiliary data structure for sequence of same character.
Definition: JPrint.hh:361
double getElapsedTime(const JDateAndTime &object) const
Get elapsed time since given date and time.
#define FATAL(A)
Definition: JMessage.hh:67
static JStat getFileStatus
Function object for file status.
Definition: JStat.hh:173
void close()
Definition: gzstream.h:185
General purpose message reporting.
Auxililary class to get date and time.
Utility class to parse command line options.
int getHour() const
hours after midnight [0-23]
std::string getFilename(const std::string &file_name)
Get file name part, i.e. part after last JEEP::PATHNAME_SEPARATOR if any.
Definition: JeepToolkit.hh:88
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
then print u2 $script< option > print u2 Possible stop
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
File status.
int main(int argc, char *argv[])
Definition: Main.cpp:15