summaryrefslogtreecommitdiff
path: root/ext/Log4Qt/src/logstream.h
blob: a0e355c2281c91a57eb57f5ed65fb0722b8ca1a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/******************************************************************************
 *
 * package:     Log4Qt
 * file:        logstream.h
 * created:     March, 2011
 * author:      Tim Besard
 *
 *
 * Copyright 2011 Tim Besard
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

#ifndef LOG4QT_LOGSTREAM_H
#define LOG4QT_LOGSTREAM_H


/******************************************************************************
 * Dependencies
 ******************************************************************************/

#include <QtCore/QTextStream>
#include <QtCore/QString>
#include "level.h"


/******************************************************************************
 * Declarations
 ******************************************************************************/

namespace Log4Qt
{
    class Logger;

    class LOG4QT_EXPORT LogStream
    {

    private:
        struct Stream {
            Stream() : ts(&buffer, QIODevice::WriteOnly), ref(1) {}
            QTextStream ts;
            QString buffer;
            int ref;
        } *stream;

    public:
        inline LogStream(const Logger& iLogger, Level iLevel) : stream(new Stream()),  mLogger(iLogger), mLevel(iLevel)
        {
        }
        inline LogStream(const LogStream &o):stream(o.stream),mLogger(o.mLogger),mLevel(o.mLevel)
        {
            ++stream->ref;
        }
        ~LogStream();

        inline LogStream &operator<<(QChar t) { stream->ts << '\'' << t << '\''; return *this; }
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
        inline LogStream &operator<<(QBool t) { stream->ts << (bool(t != 0) ? "true" : "false"); return *this; }
#endif
        inline LogStream &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return *this; }
        inline LogStream &operator<<(char t) { stream->ts << t; return *this; }
        inline LogStream &operator<<(signed short t) { stream->ts << t; return *this; }
        inline LogStream &operator<<(unsigned short t) { stream->ts << t; return *this; }
        inline LogStream &operator<<(signed int t) { stream->ts << t; return *this; }
        inline LogStream &operator<<(unsigned int t) { stream->ts << t; return *this; }
        inline LogStream &operator<<(signed long t) { stream->ts << t; return *this; }
        inline LogStream &operator<<(unsigned long t) { stream->ts << t; return *this; }
        inline LogStream &operator<<(qint64 t) { stream->ts << QString::number(t); return *this; }
        inline LogStream &operator<<(quint64 t) { stream->ts << QString::number(t); return *this; }
        inline LogStream &operator<<(float t) { stream->ts << t; return *this; }
        inline LogStream &operator<<(double t) { stream->ts << t; return *this; }
        inline LogStream &operator<<(const char* t) { stream->ts << QString::fromLatin1(t); return *this; }
        inline LogStream &operator<<(const QString & t) { stream->ts << t ; return *this; }
        inline LogStream &operator<<(const QStringRef & t) { return operator<<(t.toString()); }
        inline LogStream &operator<<(const QLatin1String &t) { stream->ts << t.latin1(); return *this; }
        inline LogStream &operator<<(const QByteArray & t) { stream->ts  << t; return *this; }
        inline LogStream &operator<<(const void * t) { stream->ts << t; return *this; }
        inline LogStream &operator<<(QTextStreamFunction f) { stream->ts << f; return *this; }
        inline LogStream &operator<<(QTextStreamManipulator m) { stream->ts << m; return *this; }

    private:
        const Logger& mLogger;
        Level mLevel;
    };
}

#endif // LOG4QT_LOGSTREAM_H