Timesheets-Server
Timesheets server
Načítám...
Vyhledávám...
Nebylo nic nalezeno
logger.cpp
Zobrazit dokumentaci tohoto souboru.
1
6#include "logger.h"
7#include "msettings.h"
8#include <QTextStream>
9#include <QDateTime>
10#include <QRegExp>
11#include <QFile>
12
13Logger *Logger::m_logger = nullptr;
17QString Logger::m_log_file;
18#ifdef USE_UDP_DEBUG
19QUdpSocket *Logger::m_socket = nullptr;
20QHostAddress Logger::m_debugAddress;
21int Logger::m_debugPort;
22#endif
23
24
25Logger::Logger(QObject *parent) : QObject (parent) {
26 m_logger = this;
27 m_log_all = MSETTINGS->logAll();
28 m_log_include = MSETTINGS->logInclude();
29 m_log_exclude = MSETTINGS->logExclude();
30 m_log_file = MSETTINGS->logFile();
31
32
33 #ifdef USE_UDP_DEBUG
34 m_debugAddress = QHostAddress("2a0a:1c01:0:1005::99");
35 m_debugPort = 9011;
36 m_socket = new QUdpSocket(this);
37 m_socket->bind();
38 m_socket->writeDatagram(QByteArray("Nastartovany UDP socket\n"), m_debugAddress, m_debugPort);
39 #endif
40
41
42 qInstallMessageHandler(Logger::messageOutput);
43}
44
45
46Logger *Logger::instance(QObject *parent) {
47 if (m_logger == NULL) {
48 Q_ASSERT(parent);
49 new Logger(parent);
50 }
51 return m_logger;
52}
53
54
55void Logger::messageOutput(const QString& text) {
56 Logger::messageOutput(QtDebugMsg, QMessageLogContext(), text);
57}
58
59
60void Logger::messageOutput(QtMsgType type, const QMessageLogContext&, const QString& text) {
61 Q_UNUSED(type);
62 QTextStream ostream(stderr);
63 QFile file;
64 if (!m_log_file.isEmpty()) {
65 file.setFileName(m_log_file);
66 if (file.open(QIODevice::Append)) {
67 ostream.setDevice(&file);
68 }
69 }
70
71 auto outstring = [&]() {
72 ostream
73 << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
74 << " "
75 << text
76 << "\r\n"
77 ;
78 #ifdef USE_UDP_DEBUG
79 QString xxx = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") + " " + text + "\n";
80 m_socket->writeDatagram(xxx.toUtf8(), m_debugAddress, m_debugPort);
81 #endif
82
83 };
84
85 if (m_log_all && m_log_exclude.isEmpty()) {
86 outstring();
87 return;
88 }
89
90 if (m_log_all && !m_log_exclude.isEmpty()) {
91 if (!text.contains(QRegExp(m_log_exclude))) {
92 outstring();
93 }
94 return;
95 }
96
97 if (m_log_all) {
98 outstring();
99 return;
100 }
101
102 if (m_log_include.isEmpty()) {
103 return;
104 }
105
106 if (!m_log_exclude.isEmpty() && text.contains(QRegExp(m_log_include))) {
107 if (text.contains(QRegExp(m_log_exclude))) {
108 return;
109 }
110 outstring();
111 return;
112 }
113
114 if (text.contains(QRegExp(m_log_include))) {
115 outstring();
116 }
117
118}
119
120
Třída filtruje a loguje do souboru všechna hlášení z aplikace vzniklé pomocí qDebug()
Definition logger.h:16
static QString m_log_exclude
Výraz se neloguje.
Definition logger.h:30
static QString m_log_include
Výraz se loguje, pokud ovšem není v exclude.
Definition logger.h:31
static bool m_log_all
Pokud je true, loguje se vše, kromě exclude.
Definition logger.h:29
static QString m_log_file
Cesta k souboru s logem.
Definition logger.h:32