// Part of BNC, a utility for retrieving decoding and
// converting GNSS data streams from NTRIP broadcasters.
//
// Copyright (C) 2007
// German Federal Agency for Cartography and Geodesy (BKG)
// http://www.bkg.bund.de
// Czech Technical University Prague, Department of Geodesy
// http://www.fsv.cvut.cz
//
// Email: euref-ip@bkg.bund.de
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#ifndef BNCLOG_H
#define BNCLOG_H

#include <QByteArray>
#include <QDateTime>
#include <QMetaType>
#include <QVector>

class bncTime;

// One record of the structured logfile (option logFileStructured).
//
// The classic logfile gets the plain text _msg as before. The structured
// logfile gets one line per record, JSON (JSON Lines) or logfmt:
//
//   time [epoch] level component event [mp] [fields ...] msg v
//
// mp: mountpoint of the stream or PPP station (member _stream)
//
// time: time of the record in UTC; epoch: GPS Time of the data epoch the
// record refers to (e.g. PPP position), only if set (setEpoch())
//
// Messages that are still emitted as plain text (newMessage(QByteArray,bool))
// become records with event "message" and the component of their sender
// (t_bncCore::connectMessages(), default "bnc"); the stream is
// taken from the text prefix if it is a known stream or PPP station, the
// level from keywords of the text (fromMessage()).
////////////////////////////////////////////////////////////////////////////
class t_logRecord {
 public:
  enum e_level {debug, info, warning, error};   // increasing severity
  enum e_format {json, logfmt};

  // version of the record layout, field "v"
  static const int version = 1;

  t_logRecord() : _level(info) {}
  t_logRecord(e_level level, const QByteArray& component, const QByteArray& event,
              const QByteArray& stream, const QByteArray& msg)
    : _level(level), _component(component), _event(event), _stream(stream), _msg(msg) {}

  // envelope of a plain text message
  static t_logRecord fromMessage(const QByteArray& msg, const QByteArray& component = "bnc");

  // plain text message with an explicit level, event "message"; an empty
  // component is filled by the connection (t_bncCore::connectLogRecords())
  static t_logRecord message(e_level level, const QByteArray& msg,
                             const QByteArray& component = QByteArray());

  // message of a BNC_DEBUG_* build: level debug, event "message"
  static t_logRecord debugMessage(const QByteArray& component, const QByteArray& msg);

  // fields in the order added; numbers are written with the C locale
  t_logRecord& add(const char* key, double value, int precision);
  t_logRecord& add(const char* key, int value);
  t_logRecord& add(const char* key, bool value);
  t_logRecord& add(const char* key, const QByteArray& value);
  t_logRecord& addTime(const char* key, const QDateTime& utc);   // ISO 8601, "Z"

  // shorter text for the structured logfile only, e.g. if the fields carry
  // the details; the classic logfile keeps _msg
  t_logRecord& setStructuredMsg(const QByteArray& msg) { _msgStructured = msg; return *this; }

  // data epoch of the record (GPS Time, ISO 8601 without "Z"), key "epoch"
  t_logRecord& setEpoch(const bncTime& epoch);

  // one line without line end; gpst: time of the record in GPS Time,
  // written as UTC (key "time")
  QByteArray encode(e_format format, const QDateTime& gpst) const;

  static e_format formatFromString(const QString& str);
  static e_level  levelFromString(const QString& str);       // default debug
  static const char* levelString(e_level level);

  // stream (mountpoint) and PPP station names known for the prefix of plain
  // text messages (thread safe)
  static void registerStream(const QByteArray& stream);

  e_level    _level;
  QByteArray _component;
  QByteArray _event;
  QByteArray _epoch;
  QByteArray _stream;
  QByteArray _msg;
  QByteArray _msgStructured;

 private:
  class t_field {
   public:
    QByteArray _key;
    QByteArray _value;      // text, numbers already formatted
    bool       _isString;
  };
  QVector<t_field> _fields;
};

Q_DECLARE_METATYPE(t_logRecord)

#endif
