summaryrefslogtreecommitdiff
path: root/ext/Log4Qt/src/level.cpp
blob: f546740ed333ccd7f9ba5dba03141f8cd43d43cc (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/******************************************************************************
 *
 * package:     Log4Qt
 * file:        level.cpp
 * created:     September 2007
 * author:      Martin Heinrich
 *
 *
 * Copyright 2007 Martin Heinrich
 *
 * 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.
 *
 ******************************************************************************/



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


#include "level.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include "logger.h"
#include <QDataStream>

namespace Log4Qt
{


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



	/**************************************************************************
	 * C helper functions
	 **************************************************************************/


    LOG4QT_DECLARE_STATIC_LOGGER(logger, Log4Qt::Level)



	/**************************************************************************
	 * Class implementation: Level
	 **************************************************************************/


	int Level::syslogEquivalent() const
	{
        // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe

	    switch (mValue)
	    {
	        case NULL_INT:
	        case ALL_INT:
	        case TRACE_INT:
	        case DEBUG_INT:
	            return 7;
	        case INFO_INT:
	            return 6;
	        case WARN_INT:
	            return 4;
	        case ERROR_INT:
	            return 3;
	        case FATAL_INT:
	        case OFF_INT:
	            return 0;
	        default:
	            Q_ASSERT_X(false, "Level::syslogEquivalent()", "Unknown level value");
	            return 7;
	    }
	}


	QString Level::toString() const
	{
        // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe

	    const char *p_context = "Level";

	    switch (mValue)
	    {
	        case NULL_INT:
	            return QCoreApplication::translate(p_context, "NULL");
	        case ALL_INT:
	            return QCoreApplication::translate(p_context, "ALL");
	        case TRACE_INT:
	            return QCoreApplication::translate(p_context, "TRACE");
	        case DEBUG_INT:
	            return QCoreApplication::translate(p_context, "DEBUG");
	        case INFO_INT:
	            return QCoreApplication::translate(p_context, "INFO");
	        case WARN_INT:
	            return QCoreApplication::translate(p_context, "WARN");
	        case ERROR_INT:
	            return QCoreApplication::translate(p_context, "ERROR");
	        case FATAL_INT:
	            return QCoreApplication::translate(p_context, "FATAL");
	        case OFF_INT:
	            return QCoreApplication::translate(p_context, "OFF");
	        default:
	            Q_ASSERT_X(false, "Level::toString()", "Unknown level value");
	            return QCoreApplication::translate(p_context, "NULL");
	    }
	}


	Level Level::fromString(const QString &rLevel, bool *pOk)
	{
	    const char *p_context = "Level";
	    if (pOk)
	        *pOk = true;

	    if (rLevel == QLatin1String("OFF") ||
	        rLevel == QCoreApplication::translate(p_context, "OFF"))
	        return OFF_INT;
	    if (rLevel == QLatin1String("FATAL") ||
	        rLevel == QCoreApplication::translate(p_context, "FATAL"))
	        return FATAL_INT;
	    if (rLevel == QLatin1String("ERROR") ||
	        rLevel == QCoreApplication::translate(p_context, "ERROR"))
	        return ERROR_INT;
	    if (rLevel == QLatin1String("WARN") ||
	        rLevel == QCoreApplication::translate(p_context, "WARN"))
	        return WARN_INT;
	    if (rLevel == QLatin1String("INFO") ||
	        rLevel == QCoreApplication::translate(p_context, "INFO"))
	        return INFO_INT;
	    if (rLevel == QLatin1String("DEBUG") ||
	        rLevel == QCoreApplication::translate(p_context, "DEBUG"))
	        return DEBUG_INT;
	    if (rLevel == QLatin1String("TRACE") ||
	        rLevel == QCoreApplication::translate(p_context, "TRACE"))
	        return TRACE_INT;
	    if (rLevel == QLatin1String("ALL") ||
	        rLevel == QCoreApplication::translate(p_context, "ALL"))
	        return ALL_INT;
	    if (rLevel == QLatin1String("NULL") ||
	        rLevel == QCoreApplication::translate(p_context, "NULL"))
	        return NULL_INT;

	    logger()->warn("Use of invalid level string '%1'. Using 'Level::NULL_INT' instead.", rLevel);
	    if (pOk)
	        *pOk = false;
	    return NULL_INT;
	}



	/**************************************************************************
	 * Implementation: Operators, Helper
	 **************************************************************************/


#ifndef QT_NO_DATASTREAM
    QDataStream &operator<<(QDataStream &rStream,
                            const Level &rLevel)
    {
        quint8 l = rLevel.mValue;
        rStream << l;
        return rStream;
    }


    QDataStream &operator>>(QDataStream &rStream,
                            Level &rLevel)
    {
        quint8 l;
        rStream >> l;
        rLevel.mValue = static_cast<Level::Value>(l);
        return rStream;
    }
#endif // QT_NO_DATASTREAM


#ifndef QT_NO_DEBUG_STREAM
	QDebug operator<<(QDebug debug,
	                  const Level &rLevel)
	{
	    debug.nospace() << "Level("
	        << rLevel.toString()
	        << ")";
	    return debug.space();
	}
#endif // QT_NO_DEBUG_STREAM

Level::Level(Value value)
    : mValue(value)
{
}

int Level::toInt() const
{   // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
    return mValue;
}

bool Level::operator==(const Level &rOther) const
{   // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
    return mValue == rOther.mValue;
}

bool Level::operator!=(const Level &rOther) const
{   // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
    return mValue != rOther.mValue;
}

bool Level::operator<(const Level &rOther) const
{   // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
    return mValue < rOther.mValue;
}

bool Level::operator<=(const Level &rOther) const
{   // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
    return mValue <= rOther.mValue;
}

bool Level::operator>(const Level &rOther) const
{   // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
    return mValue > rOther.mValue;
}

bool Level::operator>=(const Level &rOther) const
{   // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
    return mValue >= rOther.mValue;
}

} // namespace Log4Qt