Projekt

Obecné

Profil

Stáhnout (5.7 KB) Statistiky
| Větev: | Tag: | Revize:
1 c137512e Oto Šťáva
// 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
#ifndef QSLOG_H
27
#define QSLOG_H
28
29
#include "QsLogLevel.h"
30
#include "QsLogDest.h"
31
#include <QDebug>
32
#include <QString>
33
34
#define QS_LOG_VERSION "2.0b3"
35
36
namespace QsLogging
37
{
38
class Destination;
39
class LoggerImpl; // d pointer
40
41
class QSLOG_SHARED_OBJECT Logger
42
{
43
public:
44
    static Logger& instance();
45
    static void destroyInstance();
46
    static Level levelFromLogMessage(const QString& logMessage, bool* conversionSucceeded = 0);
47
48
    ~Logger();
49
50
    //! Adds a log message destination. Don't add null destinations.
51
    void addDestination(DestinationPtr destination);
52
    //! Logging at a level < 'newLevel' will be ignored
53
    void setLoggingLevel(Level newLevel);
54
    //! The default level is INFO
55
    Level loggingLevel() const;
56
    //! Set to false to disable timestamp inclusion in log messages
57
    void setIncludeTimestamp(bool e);
58
    //! Default value is true.
59
    bool includeTimestamp() const;
60
    //! Set to false to disable log level inclusion in log messages
61
    void setIncludeLogLevel(bool l);
62
    //! Default value is true.
63
    bool includeLogLevel() const;
64
65
    //! The helper forwards the streaming to QDebug and builds the final
66
    //! log message.
67
    class QSLOG_SHARED_OBJECT Helper
68
    {
69
    public:
70
        explicit Helper(Level logLevel) :
71
            level(logLevel),
72
            qtDebug(&buffer)
73
        {}
74
        ~Helper();
75
        QDebug& stream(){ return qtDebug; }
76
77
    private:
78
        void writeToLog();
79
80
        Level level;
81
        QString buffer;
82
        QDebug qtDebug;
83
	};
84
85
private:
86
    Logger();
87
    Logger(const Logger&);            // not available
88
    Logger& operator=(const Logger&); // not available
89
90
    void enqueueWrite(const QString& message, Level level);
91
    void write(const QString& message, Level level);
92
93
    LoggerImpl* d;
94
95
    friend class LogWriterRunnable;
96
};
97
98
} // end namespace
99
100
//! Logging macros: define QS_LOG_LINE_NUMBERS to get the file and line number
101
//! in the log output.
102
#ifndef QS_LOG_LINE_NUMBERS
103
#define QLOG_TRACE() \
104
    if (QsLogging::Logger::instance().loggingLevel() > QsLogging::TraceLevel) {} \
105
    else QsLogging::Logger::Helper(QsLogging::TraceLevel).stream()
106
#define QLOG_DEBUG() \
107
    if (QsLogging::Logger::instance().loggingLevel() > QsLogging::DebugLevel) {} \
108
    else QsLogging::Logger::Helper(QsLogging::DebugLevel).stream()
109
#define QLOG_INFO()  \
110
    if (QsLogging::Logger::instance().loggingLevel() > QsLogging::InfoLevel) {} \
111
    else QsLogging::Logger::Helper(QsLogging::InfoLevel).stream()
112
#define QLOG_WARN()  \
113
    if (QsLogging::Logger::instance().loggingLevel() > QsLogging::WarnLevel) {} \
114
    else QsLogging::Logger::Helper(QsLogging::WarnLevel).stream()
115
#define QLOG_ERROR() \
116
    if (QsLogging::Logger::instance().loggingLevel() > QsLogging::ErrorLevel) {} \
117
    else QsLogging::Logger::Helper(QsLogging::ErrorLevel).stream()
118
#define QLOG_FATAL() \
119
    if (QsLogging::Logger::instance().loggingLevel() > QsLogging::FatalLevel) {} \
120
    else QsLogging::Logger::Helper(QsLogging::FatalLevel).stream()
121
#else
122
#define QLOG_TRACE() \
123
    if (QsLogging::Logger::instance().loggingLevel() > QsLogging::TraceLevel) {} \
124
    else  QsLogging::Logger::Helper(QsLogging::TraceLevel).stream() << __FILE__ << '@' << __LINE__
125
#define QLOG_DEBUG() \
126
    if (QsLogging::Logger::instance().loggingLevel() > QsLogging::DebugLevel) {} \
127
    else QsLogging::Logger::Helper(QsLogging::DebugLevel).stream() << __FILE__ << '@' << __LINE__
128
#define QLOG_INFO()  \
129
    if (QsLogging::Logger::instance().loggingLevel() > QsLogging::InfoLevel) {} \
130
    else QsLogging::Logger::Helper(QsLogging::InfoLevel).stream() << __FILE__ << '@' << __LINE__
131
#define QLOG_WARN()  \
132
    if (QsLogging::Logger::instance().loggingLevel() > QsLogging::WarnLevel) {} \
133
    else QsLogging::Logger::Helper(QsLogging::WarnLevel).stream() << __FILE__ << '@' << __LINE__
134
#define QLOG_ERROR() \
135
    if (QsLogging::Logger::instance().loggingLevel() > QsLogging::ErrorLevel) {} \
136
    else QsLogging::Logger::Helper(QsLogging::ErrorLevel).stream() << __FILE__ << '@' << __LINE__
137
#define QLOG_FATAL() \
138
    if (QsLogging::Logger::instance().loggingLevel() > QsLogging::FatalLevel) {} \
139
    else QsLogging::Logger::Helper(QsLogging::FatalLevel).stream() << __FILE__ << '@' << __LINE__
140
#endif
141
142
#ifdef QS_LOG_DISABLE
143
#include "QsLogDisableForThisFile.h"
144
#endif
145
146
#endif // QSLOG_H