Jpp  19.1.0-rc.1
the software that should make you happy
JLoggerMessage.hh
Go to the documentation of this file.
1 #ifndef __JDB_JLOGGERMESSAGE__
2 #define __JDB_JLOGGERMESSAGE__
3 
4 #include <string>
5 #include <istream>
6 #include <ostream>
7 #include <sstream>
8 #include <iomanip>
9 
10 #include "JSystem/JDateAndTime.hh"
11 
12 #include "JNet/JTag.hh"
13 
14 #include "JLang/JException.hh"
15 #include "JLang/JManip.hh"
16 
18 #include "JDAQ/JDAQTags.hh"
19 
20 #include "JDatalogString.hh"
21 
22 
23 namespace JDB {}
24 namespace JPP { using namespace JDB; }
25 
26 namespace JDB {
27 
29  using JLANG::JParseError;
31 
32 
33  /**
34  * Auxiliary data structure for logger message.
35  */
36  struct JLoggerMessage {
37  /**
38  * Default constructor.
39  */
41  {}
42 
43 
44  /**
45  * Copy constructor.
46  *
47  * \param datalog data log
48  */
50  {
51  using namespace std;
52  using namespace JPP;
53  using namespace KM3NETDAQ;
54 
55  if (datalog.source == MESSAGE_TAG) {
56 
57  this->tag = datalog.source;
58 
59  istringstream is(datalog.data);
60 
61  if (is >> this->datim >> this->ip >> this->nickname >> this->level) {
62 
63  this->datim.set(datalog.utc / 1000, (float) (datalog.utc%1000) * 1.0e-3, true);
64 
65  while (is.peek() != EOF && isspace((char) is.peek())) { is.ignore(1); }
66 
67  getline(is, this->data);
68 
69  } else {
70 
71  THROW(JParseError, "Invalid string " << datalog.source << " " << datalog.data);
72  }
73 
74  } else {
75 
76  THROW(JParseError, "Invalid source " << datalog.source);
77  }
78  }
79 
80 
81  /**
82  * Get match.
83  *
84  * \param message message
85  * \return true if message matches; else false
86  */
87  bool match(const JLoggerMessage& message) const
88  {
89  using namespace std;
90 
91  if (!message.tag .empty() && message.tag != tag) { return false; }
92  if (!message.process .empty() && message.process != process) { return false; }
93  if (!message.nickname.empty() && message.nickname != nickname) { return false; }
94  if (!message.ip .empty() && message.ip != ip) { return false; }
95  if (!message.fullname.empty() && message.fullname != fullname) { return false; }
96  if (!message.level .empty() && message.level != level) { return false; }
97  if (!message.data .empty() && !regex_match(data, regex(message.data))) { return false; }
98 
99  return true;
100  }
101 
102 
103  /**
104  * Check if message has date and time.
105  *
106  * \return true if messages has date and time; else false
107  */
108  bool hasTime() const
109  {
110  using namespace JPP;
111 
112  return (tag != DISPTAG_Born.toString() &&
113  tag != DISPTAG_Died.toString());
114  }
115 
116 
117  /**
118  * Get UTC time.
119  *
120  * \return time [ms]
121  */
122  long int getTime() const
123  {
124  return (long int) 1000 * (long int) datim.getTime() + (long int) 500;
125  }
126 
127 
128  /**
129  * Read message from input.
130  *
131  * \param in input stream
132  * \param object message
133  * \return input stream
134  */
135  friend inline std::istream& operator>>(std::istream& in, JLoggerMessage& object)
136  {
137  using namespace JPP;
138 
139  object = JLoggerMessage();
140 
141  in >> object.file_name
142  >> object.tag
143  >> object.process;
144 
145  if (object.tag != DISPTAG_Born.toString() &&
146  object.tag != DISPTAG_Died.toString()) {
147 
148  in >> object.datim
149  >> object.ip
150  >> object.nickname
151  >> object.level;
152 
153  while (in.peek() != EOF && isspace((char) in.peek())) { in.ignore(1); }
154 
155  return getline(in, object.data);
156 
157  } else {
158 
159  return in >> object.fullname;
160  }
161  }
162 
163 
164  /**
165  * Write message to output.
166  *
167  * \param out output stream
168  * \param object message
169  * \return output stream
170  */
171  friend inline std::ostream& operator<<(std::ostream& out, const JLoggerMessage& object)
172  {
173  using namespace std;
174  using namespace JPP;
175 
176  if (object.tag != DISPTAG_Born.toString() &&
177  object.tag != DISPTAG_Died.toString()) {
178 
179  out << LEFT(8)
180  << object.tag << ' ' << right;
181  out << object.datim << ' ';
182  //out << object.getTime() << ' ';
183  //out << object.ip << ' ';
184  out << object.nickname << ' ';
185  //out << object.level << ' ';
186  out << object.data;
187 
188  } else {
189 
190  out << object.tag << ' ';
191  //out << object.process << ' ';
192  out << object.fullname;
193  }
194 
195  return out;
196  }
197 
198 
199  std::string file_name;
200  std::string tag;
201  std::string process;
203  std::string ip;
204  std::string nickname;
205  std::string fullname;
206  std::string level;
207  std::string data;
208  };
209 }
210 
211 #endif
Fixed parameters and ControlHost tags for KM3NeT DAQ.
Date and time functions.
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
I/O manipulators.
General purpose message reporting.
ControlHost tag.
Exception for opening of file.
Definition: JException.hh:360
Exception for parsing value.
Definition: JException.hh:198
std::string toString() const
Convert tag to string.
Definition: JTag.hh:171
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
@ LEFT
Definition: JTwosome.hh:18
static const std::string MESSAGE_TAG
Message logging tag.
static const JTag DISPTAG_Born("Born")
static const JTag DISPTAG_Died("Died")
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
KM3NeT DAQ data structures and auxiliaries.
Definition: DataQueue.cc:39
Definition: JSTDTypes.hh:14
Auxiliary data structure for datalog strings.
Auxiliary data structure for logger message.
friend std::ostream & operator<<(std::ostream &out, const JLoggerMessage &object)
Write message to output.
bool hasTime() const
Check if message has date and time.
long int getTime() const
Get UTC time.
JLoggerMessage()
Default constructor.
bool match(const JLoggerMessage &message) const
Get match.
JLoggerMessage(const JDatalogString &datalog)
Copy constructor.
friend std::istream & operator>>(std::istream &in, JLoggerMessage &object)
Read message from input.
Auxiliary class for date and time.
Definition: JDateAndTime.hh:80
time_t getTime() const
time
void set(const bool utc=false)
Set to current local time.