openPilotLog
alog.h
1 /*
2  *openPilotLog - A FOSS Pilot Logbook Application
3  *Copyright (C) 2020-2021 Felix Turowsky
4  *
5  *This program is free software: you can redistribute it and/or modify
6  *it under the terms of the GNU General Public License as published by
7  *the Free Software Foundation, either version 3 of the License, or
8  *(at your option) any later version.
9  *
10  *This program is distributed in the hope that it will be useful,
11  *but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *GNU General Public License for more details.
14  *
15  *You should have received a copy of the GNU General Public License
16  *along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 #ifndef ALOG_H
19 #define ALOG_H
20 
21 #include <QTime>
22 #include <QFile>
23 #include <QFileInfo>
24 #include <QDebug>
25 #include <QDir>
26 #include <QFileInfoList>
27 #include <iostream>
28 #include <QDebug>
29 #include "src/classes/astandardpaths.h"
30 
50 namespace ALog
51 {
52  static QDir logFolder;
53  static QString logFileName;
54  const static int numberOfLogs = 10; // max number of log files to keep
55  const static int sizeOfLogs = 1024 * 100; // max log size in bytes, = 100kB
56 
57  const static auto DEB_HEADER = QLatin1String(" [DEBG]:\t");
58  const static auto INFO_HEADER = QLatin1String(" [INFO]:\t");
59  const static auto WARN_HEADER = QLatin1String(" [WARN]:\t");
60  const static auto CRIT_HEADER = QLatin1String(" [CRIT]:\t");
61  const static auto DEB_HEADER_CONSOLE = QLatin1String("\u001b[38;5;75m[DEBG]:\t");
62  const static auto INFO_HEADER_CONSOLE = QLatin1String("\033[32m[INFO]:\t\033[m");
63  const static auto WARN_HEADER_CONSOLE = QLatin1String("\033[33m[WARN]:\t\033[m");
64  const static auto CRIT_HEADER_CONSOLE = QLatin1String("\033[35m[CRIT]:\t\033[m");
65  const static auto SPACER = QLatin1String("\t\t");
66  const static auto D_SPACER = QLatin1String("\t\t\t\t");
67 
68  bool init(bool log_debug = false);
69  void setLogFileName();
70  void deleteOldLogs();
71  void aMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString& msg);
72  inline static const QString timeNow(){return QTime::currentTime().toString(Qt::ISODate);}
73 
83 } // namespace ALog
84 
104 #define REPR(cls, str) \
105 friend \
106 QDebug operator<<(QDebug qdb, const cls& object) \
107 { \
108  qdb << QString(#cls) + '(' + str + ')'; \
109  return qdb; \
110 }
111 
112 #endif // ALOG_H
ALog
The ALog namespace encapsulates constants and functions used to provide logging to files and logging ...
Definition: alog.cpp:21
ALog::aMessageHandler
void aMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
aMessageHandler Intercepts Messages and prints to console and log file
Definition: alog.cpp:84
ALog::setLogFileName
void setLogFileName()
setLogFileName sets a log file name ("Log_<Date>_<Time>.txt")
Definition: alog.cpp:28
ALog::deleteOldLogs
void deleteOldLogs()
Cleans up old logs and initializes logging by preparing and installing a QMessageHandler.
Definition: alog.cpp:39
ALog::init
bool init(bool log_debug)
initialise logging, clean up logfiles and install a QMessageHandler. To enable logging of debug messa...
Definition: alog.cpp:59