openPilotLog
log.h
1 /*
2  *openPilotLog - A FOSS Pilot Logbook Application
3  *Copyright (C) 2020-2022 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 LOG_H
19 #define LOG_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 
49 namespace OPL::Log
50 {
51  static QDir logFolder;
52  static QString logFileName;
53  const static int numberOfLogs = 10; // max number of log files to keep
54  const static int sizeOfLogs = 1024 * 100; // max log size in bytes, = 100kB
55 
56  const static auto DEB_HEADER = QLatin1String(" [DEBG]:\t");
57  const static auto INFO_HEADER = QLatin1String(" [INFO]:\t");
58  const static auto WARN_HEADER = QLatin1String(" [WARN]:\t");
59  const static auto CRIT_HEADER = QLatin1String(" [CRIT]:\t");
60  const static auto DEB_HEADER_CONSOLE = QLatin1String("\u001b[38;5;75m[DEBG]:\t");
61  const static auto INFO_HEADER_CONSOLE = QLatin1String("\033[32m[INFO]:\t\033[m");
62  const static auto WARN_HEADER_CONSOLE = QLatin1String("\033[33m[WARN]:\t\033[m");
63  const static auto CRIT_HEADER_CONSOLE = QLatin1String("\033[35m[CRIT]:\t\033[m");
64  const static auto SPACER = QLatin1String("\t\t");
65  const static auto D_SPACER = QLatin1String("\t\t\t\t");
66 
67  bool init(bool log_debug = false);
68  void setLogFileName();
69  void deleteOldLogs();
70  void aMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString& msg);
71  inline static const QString timeNow(){return QTime::currentTime().toString(Qt::ISODate);}
72 
82 } // namespace ALog
83 
103 #define REPR(cls, str) \
104 friend \
105 QDebug operator<<(QDebug qdb, const cls& object) \
106 { \
107  qdb << QString(#cls) + '(' + str + ')'; \
108  return qdb; \
109 }
110 
111 #endif // LOG_H
The OPL::Log namespace encapsulates constants and functions used to provide logging to files and logg...
Definition: log.cpp:23
void setLogFileName()
setLogFileName sets a log file name ("Log_<Date>_<Time>.txt")
Definition: log.cpp:30
void deleteOldLogs()
Cleans up old logs and initializes logging by preparing and installing a QMessageHandler.
Definition: log.cpp:41
void aMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
aMessageHandler Intercepts Messages and prints to console and log file
Definition: log.cpp:94
bool init(bool log_debug)
initialise logging, clean up logfiles and install a QMessageHandler. To enable logging of debug messa...
Definition: log.cpp:61