trikRuntime
QsLog.h
Go to the documentation of this file.
1 // Copyright (c) 2013, Razvan Petru
2 // All rights reserved.
3 
4 // Redistribution and use in source and binary forms, with or without modification,
5 // are permitted provided that the following conditions are met:
6 
7 // * Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice, this
10 // list of conditions and the following disclaimer in the documentation and/or other
11 // materials provided with the distribution.
12 // * The name of the contributors may not be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 // IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
19 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23 // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
24 // OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #pragma once
27 
28 #include "QsLogLevel.h"
29 #include "QsLogDest.h"
30 #include <QDebug>
31 #include <QString>
32 
33 #define QS_LOG_VERSION "2.0b3"
34 
35 namespace QsLogging
36 {
37 class Destination;
38 class LoggerImpl; // d pointer
39 
41 {
42 public:
43  static Logger& instance();
44  static void destroyInstance();
45  static Level levelFromLogMessage(const QString& logMessage, bool* conversionSucceeded = 0);
46 
47  ~Logger();
48 
50  void addDestination(DestinationPtr destination);
52  void setLoggingLevel(Level newLevel);
54  Level loggingLevel() const;
55 
59  {
60  public:
61  explicit Helper(Level logLevel) :
62  level(logLevel),
63  qtDebug(&buffer) {}
64  ~Helper();
65  QDebug& stream(){ return qtDebug; }
66 
67  private:
68  void writeToLog();
69 
70  Level level;
71  QString buffer;
72  QDebug qtDebug;
73  };
74 
75 private:
76  Logger();
77  Logger(const Logger&); // not available
78  Logger& operator=(const Logger&); // not available
79 
80  void enqueueWrite(const QString& message, Level level);
81  void write(const QString& message, Level level);
82 
83  LoggerImpl* d;
84 
85  friend class LogWriterRunnable;
86 };
87 
88 } // end namespace
89 
92 #define QLOG_TRACE() \
93  if (QsLogging::Logger::instance().loggingLevel() > QsLogging::TraceLevel) {} \
94  else QsLogging::Logger::Helper(QsLogging::TraceLevel).stream() << __FILE__ << '@' << __LINE__
95 #define QLOG_DEBUG() \
96  if (QsLogging::Logger::instance().loggingLevel() > QsLogging::DebugLevel) {} \
97  else QsLogging::Logger::Helper(QsLogging::DebugLevel).stream() << __FILE__ << '@' << __LINE__
98 #define QLOG_INFO() \
99  if (QsLogging::Logger::instance().loggingLevel() > QsLogging::InfoLevel) {} \
100  else QsLogging::Logger::Helper(QsLogging::InfoLevel).stream() << __FILE__ << '@' << __LINE__
101 #define QLOG_WARN() \
102  if (QsLogging::Logger::instance().loggingLevel() > QsLogging::WarnLevel) {} \
103  else QsLogging::Logger::Helper(QsLogging::WarnLevel).stream() << __FILE__ << '@' << __LINE__
104 #define QLOG_ERROR() \
105  if (QsLogging::Logger::instance().loggingLevel() > QsLogging::ErrorLevel) {} \
106  else QsLogging::Logger::Helper(QsLogging::ErrorLevel).stream() << __FILE__ << '@' << __LINE__
107 #define QLOG_FATAL() \
108  if (QsLogging::Logger::instance().loggingLevel() > QsLogging::FatalLevel) {} \
109  else QsLogging::Logger::Helper(QsLogging::FatalLevel).stream() << __FILE__ << '@' << __LINE__
110 
111 #ifdef QS_LOG_DISABLE
112 #include "QsLogDisableForThisFile.h"
113 #endif
Definition: QsLog.h:40
QSharedPointer< Destination > DestinationPtr
Definition: QsLogDest.h:55
#define QSLOG_SHARED_OBJECT
Definition: QsLogDest.h:39
Level
Definition: QsLogLevel.h:31
The helper forwards the streaming to QDebug and builds the final log message.
Definition: QsLog.h:58
Definition: QsLog.cpp:78
Definition: QsLog.cpp:38
Helper(Level logLevel)
Definition: QsLog.h:61
Definition: QsLog.cpp:89
QDebug & stream()
Definition: QsLog.h:65