LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
QsLog.h
Go to the documentation of this file.
1// Copyright (c) 2010, Razvan Petru
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6// Redistribution and use in source and binary forms, with or without modification,
7// are permitted provided that the following conditions are met:
8
9// * Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above copyright notice, this
12// list of conditions and the following disclaimer in the documentation and/or other
13// materials provided with the distribution.
14// * The name of the contributors may not be used to endorse or promote products
15// derived from this software without specific prior written permission.
16
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26// OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef QSLOG_H
29#define QSLOG_H
30
31#include "QsLogDest.h"
32
33#ifdef WIN32
34#pragma warning(disable: 4127)
35#endif
36#include <QDebug>
37#include <QString>
38
39#include <iostream>
40
41#include "QsLog_export.h"
42
43
44namespace QsLogging
45{
55
56
57class Destination;
58class LoggerImpl; // d pointer
60{
61 friend class LoggerImpl;
62public:
63 static Logger& instance(const QString& zone = QLatin1String(""))
64 {
65 static QMap<QString,Logger*> staticLog;
66 QMap<QString,Logger*>::iterator it = staticLog.find(zone);
67 if (it == staticLog.end())
68 {
69 Logger* logger = new Logger(zone);
70 for (auto& destination: Destinations::instance().destinations())
71 {
72 logger->addDestination(destination);
73 }
74 return **staticLog.insert(zone, logger);
75 }
76 return **it;
77 }
78
80 void addDestination(std::shared_ptr<Destination> destination);
82 void setLoggingLevel(Level newLevel);
84 Level loggingLevel() const;
85 const QString& zone() const;
86
88 bool isLoggingEnabled() const;
90 bool isTraceEnabled() const;
92 bool isDebugEnabled() const;
94 bool isInfoEnabled() const;
96 bool isWarnEnabled() const;
98 bool isErrorEnabled() const;
100 bool isFatalEnabled() const;
101
105 {
106 public:
107 explicit Helper(Level logLevel, const QString& zone) :
108 level(logLevel),
109 qtDebug(&buffer),
110 zone(zone){}
111 ~Helper();
112 QDebug& stream(){ return qtDebug; }
113
114 private:
115 void writeToLog();
116
117 Level level;
118 QString buffer;
119 QDebug qtDebug;
120 QString zone;
121 };
122
124private:
125 Logger(const QString& aZone = QLatin1String(""));
126 Logger(const Logger&);
127 ~Logger();
128
129 void write(const QString& message);
130
131 LoggerImpl* d;
132};
133
134LIMA_COMMONQSLOG_EXPORT QDebug& operator<< (QDebug& qd, const std::string& str );
135
136} // end namespace
137
140#ifndef QS_LOG_LINE_NUMBERS
141 #define QLOG_TRACE() \
142 if( logger.loggingLevel() > QsLogging::TraceLevel ){} \
143 else QsLogging::Logger::Helper(QsLogging::TraceLevel, logger.zone()).stream()
144 #define QLOG_DEBUG() \
145 if( logger.loggingLevel() > QsLogging::DebugLevel ){} \
146 else QsLogging::Logger::Helper(QsLogging::DebugLevel, logger.zone()).stream()
147 #define QLOG_INFO() \
148 if( logger.loggingLevel() > QsLogging::InfoLevel ){} \
149 else QsLogging::Logger::Helper(QsLogging::InfoLevel, logger.zone()).stream()
150 #define QLOG_WARN() \
151 if( logger.loggingLevel() > QsLogging::WarnLevel ){} \
152 else QsLogging::Logger::Helper(QsLogging::WarnLevel, logger.zone()).stream()
153 #define QLOG_ERROR() \
154 if( logger.loggingLevel() > QsLogging::ErrorLevel ){} \
155 else QsLogging::Logger::Helper(QsLogging::ErrorLevel, logger.zone()).stream()
156 #define QLOG_FATAL() \
157 QsLogging::Logger::Helper(QsLogging::FatalLevel, logger.zone()).stream()
158#else
159 #define QLOG_TRACE() \
160 if( logger.loggingLevel() > QsLogging::TraceLevel ){} \
161 else QsLogging::Logger::Helper(QsLogging::TraceLevel, logger.zone()).stream() << __FILE__ << '@' << __LINE__
162 #define QLOG_DEBUG() \
163 if( logger.loggingLevel() > QsLogging::DebugLevel ){} \
164 else QsLogging::Logger::Helper(QsLogging::DebugLevel, logger.zone()).stream() << __FILE__ << '@' << __LINE__
165 #define QLOG_INFO() \
166 if( logger.loggingLevel() > QsLogging::InfoLevel ){} \
167 else QsLogging::Logger::Helper(QsLogging::InfoLevel, logger.zone()).stream() << __FILE__ << '@' << __LINE__
168 #define QLOG_WARN() \
169 if( logger.loggingLevel() > QsLogging::WarnLevel ){} \
170 else QsLogging::Logger::Helper(QsLogging::WarnLevel, logger.zone()).stream() << __FILE__ << '@' << __LINE__
171 #define QLOG_ERROR() \
172 if( logger.loggingLevel() > QsLogging::ErrorLevel ){} \
173 else QsLogging::Logger::Helper(QsLogging::ErrorLevel, logger.zone()).stream() << __FILE__ << '@' << __LINE__
174 #define QLOG_FATAL() \
175 QsLogging::Logger::Helper(QsLogging::FatalLevel, logger.zone()).stream() << __FILE__ << '@' << __LINE__
176#endif
177
178#endif // QSLOG_H
#define LIMA_COMMONQSLOG_EXPORT
The helper forwards the streaming to QDebug and builds the final log message.
Definition QsLog.h:105
Helper(Level logLevel, const QString &zone)
Definition QsLog.h:107
static Logger & instance(const QString &zone=QLatin1String(""))
Definition QsLog.h:63
void addDestination(std::shared_ptr< Destination > destination)
Adds a log message destination. Don't add null destinations.
Definition QsLog.cpp:108
Logger & operator=(const Logger &)
@ TraceLevel
Definition QsLog.h:48
@ DebugLevel
Definition QsLog.h:49
@ ErrorLevel
Definition QsLog.h:52
@ InfoLevel
Definition QsLog.h:50
@ FatalLevel
Definition QsLog.h:53
@ WarnLevel
Definition QsLog.h:51
LIMA_COMMONQSLOG_EXPORT QDebug & operator<<(QDebug &qd, const std::string &str)