libquentier  0.8.0
The library for rich desktop clients of Evernote service
QuentierLogger.h
1 /*
2  * Copyright 2016-2024 Dmitry Ivanov
3  *
4  * This file is part of libquentier
5  *
6  * libquentier is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, version 3 of the License.
9  *
10  * libquentier 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libquentier. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #include <quentier/utility/Linkage.h>
22 
23 #include <QDebug>
24 #include <QRegularExpression>
25 #include <QString>
26 #include <QTextStream>
27 
28 namespace quentier {
29 
34 enum class LogLevel
35 {
36  Trace,
37  Debug,
38  Info,
39  Warning,
40  Error
41 };
42 
43 QUENTIER_EXPORT QDebug & operator<<(QDebug & dbg, LogLevel logLevel);
44 
45 QUENTIER_EXPORT QTextStream & operator<<(QTextStream & strm, LogLevel logLevel);
46 
54 void QUENTIER_EXPORT QuentierInitializeLogging();
55 
59 void QUENTIER_EXPORT QuentierAddLogEntry(
60  const QString & sourceFileName, int sourceFileLineNumber,
61  const QString & component, const QString & message, LogLevel logLevel);
62 
68 LogLevel QUENTIER_EXPORT QuentierMinLogLevel();
69 
73 void QUENTIER_EXPORT QuentierSetMinLogLevel(LogLevel logLevel);
74 
79 void QUENTIER_EXPORT QuentierAddStdOutLogDestination();
80 
85 [[nodiscard]] bool QUENTIER_EXPORT QuentierIsLogLevelActive(LogLevel logLevel);
86 
90 [[nodiscard]] QString QUENTIER_EXPORT QuentierLogFilesDirPath();
91 
95 void QUENTIER_EXPORT QuentierRestartLogging();
96 
100 [[nodiscard]] QRegularExpression QUENTIER_EXPORT QuentierLogComponentFilter();
101 
105 void QUENTIER_EXPORT
106  QuentierSetLogComponentFilter(const QRegularExpression & filter);
107 
108 } // namespace quentier
109 
110 #define QNLOG_PRIVATE_BASE(component, message, level) \
111  if (quentier::QuentierIsLogLevelActive(quentier::LogLevel::level)) { \
112  QString msg; \
113  QDebug dbg(&msg); \
114  dbg.nospace(); \
115  dbg.noquote(); \
116  dbg << message; \
117  quentier::QuentierAddLogEntry( \
118  QStringLiteral(__FILE__), __LINE__, QString::fromUtf8(component), \
119  msg, quentier::LogLevel::level); \
120  } \
121  // QNLOG_PRIVATE_BASE
122 
123 #define QNTRACE(component, message) \
124  QNLOG_PRIVATE_BASE(component, message, Trace) \
125  // QNTRACE
126 
127 #define QNDEBUG(component, message) \
128  QNLOG_PRIVATE_BASE(component, message, Debug) \
129  // QNDEBUG
130 
131 #define QNINFO(component, message) \
132  QNLOG_PRIVATE_BASE(component, message, Info) \
133  // QNINFO
134 
135 #define QNWARNING(component, message) \
136  QNLOG_PRIVATE_BASE(component, message, Warning) \
137  // QNWARNING
138 
139 #define QNERROR(component, message) \
140  QNLOG_PRIVATE_BASE(component, message, Error) \
141  // QNERROR
142 
143 #define QUENTIER_SET_MIN_LOG_LEVEL(level) \
144  quentier::QuentierSetMinLogLevel(quentier::LogLevel::level)
145  // QUENTIER_SET_MIN_LOG_LEVEL
146 
147 #define QUENTIER_INITIALIZE_LOGGING() quentier::QuentierInitializeLogging()
148  // QUENTIER_INITIALIZE_LOGGING
149 
150 // clang-format off
151 #define QUENTIER_ADD_STDOUT_LOG_DESTINATION() \
152  quentier::QuentierAddStdOutLogDestination() \
153  // QUENTIER_ADD_STDOUT_LOG_DESTINATION
154 // clang-format on
155 
156 #define QNLOG_FILE_LINENUMBER_DELIMITER ":"