source: ntrip/trunk/BNC/src/bnclog.h

Last change on this file was 11187, checked in by stuerze, 3 days ago

Structured logging improved

File size: 4.6 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25#ifndef BNCLOG_H
26#define BNCLOG_H
27
28#include <QByteArray>
29#include <QDateTime>
30#include <QMetaType>
31#include <QVector>
32
33class bncTime;
34
35// One record of the structured logfile (option logFileStructured).
36//
37// The classic logfile gets the plain text _msg as before. The structured
38// logfile gets one line per record, JSON (JSON Lines) or logfmt:
39//
40// time [epoch] level component event [mp] [fields ...] msg v
41//
42// mp: mountpoint of the stream or PPP station (member _stream)
43//
44// time: time of the record in UTC; epoch: GPS Time of the data epoch the
45// record refers to (e.g. PPP position), only if set (setEpoch())
46//
47// Messages that are still emitted as plain text (newMessage(QByteArray,bool))
48// become records with event "message" and the component of their sender
49// (t_bncCore::connectMessages(), default "bnc"); the stream is
50// taken from the text prefix if it is a known stream or PPP station, the
51// level from keywords of the text (fromMessage()).
52////////////////////////////////////////////////////////////////////////////
53class t_logRecord {
54 public:
55 enum e_level {debug, info, warning, error}; // increasing severity
56 enum e_format {json, logfmt};
57
58 // version of the record layout, field "v"
59 static const int version = 1;
60
61 t_logRecord() : _level(info) {}
62 t_logRecord(e_level level, const QByteArray& component, const QByteArray& event,
63 const QByteArray& stream, const QByteArray& msg)
64 : _level(level), _component(component), _event(event), _stream(stream), _msg(msg) {}
65
66 // envelope of a plain text message
67 static t_logRecord fromMessage(const QByteArray& msg, const QByteArray& component = "bnc");
68
69 // plain text message with an explicit level, event "message"; an empty
70 // component is filled by the connection (t_bncCore::connectLogRecords())
71 static t_logRecord message(e_level level, const QByteArray& msg,
72 const QByteArray& component = QByteArray());
73
74 // message of a BNC_DEBUG_* build: level debug, event "message"
75 static t_logRecord debugMessage(const QByteArray& component, const QByteArray& msg);
76
77 // fields in the order added; numbers are written with the C locale
78 t_logRecord& add(const char* key, double value, int precision);
79 t_logRecord& add(const char* key, int value);
80 t_logRecord& add(const char* key, bool value);
81 t_logRecord& add(const char* key, const QByteArray& value);
82 t_logRecord& addTime(const char* key, const QDateTime& utc); // ISO 8601, "Z"
83
84 // shorter text for the structured logfile only, e.g. if the fields carry
85 // the details; the classic logfile keeps _msg
86 t_logRecord& setStructuredMsg(const QByteArray& msg) { _msgStructured = msg; return *this; }
87
88 // data epoch of the record (GPS Time, ISO 8601 without "Z"), key "epoch"
89 t_logRecord& setEpoch(const bncTime& epoch);
90
91 // one line without line end; gpst: time of the record in GPS Time,
92 // written as UTC (key "time")
93 QByteArray encode(e_format format, const QDateTime& gpst) const;
94
95 static e_format formatFromString(const QString& str);
96 static e_level levelFromString(const QString& str); // default debug
97 static const char* levelString(e_level level);
98
99 // stream (mountpoint) and PPP station names known for the prefix of plain
100 // text messages (thread safe)
101 static void registerStream(const QByteArray& stream);
102
103 e_level _level;
104 QByteArray _component;
105 QByteArray _event;
106 QByteArray _epoch;
107 QByteArray _stream;
108 QByteArray _msg;
109 QByteArray _msgStructured;
110
111 private:
112 class t_field {
113 public:
114 QByteArray _key;
115 QByteArray _value; // text, numbers already formatted
116 bool _isString;
117 };
118 QVector<t_field> _fields;
119};
120
121Q_DECLARE_METATYPE(t_logRecord)
122
123#endif
Note: See TracBrowser for help on using the repository browser.