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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
/******************************************************************************
*
* package: Log4Qt
* file: logerror.h
* 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.
*
******************************************************************************/
#ifndef LOG4QT_LOGERROR_H
#define LOG4QT_LOGERROR_H
#include "../log4qtshared.h"
/******************************************************************************
* Dependencies
******************************************************************************/
#include <QtCore/QString>
#include <QtCore/QVariant>
/******************************************************************************
* Declarations
******************************************************************************/
namespace Log4Qt
{
/*!
* Creates an LogError object with the error message \a message, the error
* code \a code and the context \a context. The symbol of the error is
* set to \a code as string value.
*
* The following example logs an error, if a character is not a digit.
*
* \code
* if (!c.isDigit())
* {
* Error e = LOG4QT_ERROR(QT_TR_NOOP("Found character '%1' where digit was expected."),
* LAYOUT_EXPECTED_DIGIT_ERROR,
* "Log4Qt::PatternFormatter");
* e << QString(c);
* logger()->error(e);
* }
* \endcode
*/
#define LOG4QT_ERROR(message, code, context) \
LogError(message, code, #code, context)
/*!
* Creates an LogError object with the error message \a message and the
* error code \a code. The symbol of the error is set to \a code as string
* value. The context is set to the class name of the current object. The
* current objects class must be derived from QObject.
*
* The following example handles an error while opening a file.
*
* \code
* if (!mpFile->open(mode))
* {
* LogError e = LOG4QT_QCLASS_ERROR(QT_TR_NOOP("Unable to open file '%1' for appender '%2'"),
* APPENDER_OPENING_FILE_ERROR);
* e << mFileName << name();
* e.addCausingError(LogError(mpFile->errorString(), mpFile->error()));
* logger()->error(e);
* return;
* }
* \endcode
*/
#define LOG4QT_QCLASS_ERROR(message, code) \
LogError(message, code, #code, this->metaObject()->className())
/*!
* \brief The class LogError represents an error.
*
* The class error allows storing error information in a structured way.
* The error message is stored separately from the information that may be
* substituted into the message string. This way it is possible to access
* all information after the error has been raised. It also allows to
* translate the error at a later point in time or to get a translated and
* a not translated error text (e.g. translated for the UI and not
* translated for a log).
*
* The message is accessed using message() and setMessage(). Arguments for
* the message can be added using addArg() or operator<<(). The arguments
* can be retrieved using args(). The message with substituted arguments
* is returned by messageWithArgs().
*
* An error code can be set as integer value code() and/or a symbolic value
* symbol().
*
* To allow the translation of the message the error stores the translation
* context (context(), setContext()). The translated message can be accessed
* using translatedMessage() or using translatedMessageWithArgs(), if it
* should contain the arguments.
*
* An error can have one or more related errors that caused it. An error is
* related using addCausingError(). All causing errors can be retrieved using
* causingErrors().
*
* A per thread error can be maintained using lastError() and setLastError().
*
* There are two macros avaiable to simplify the error creation. The macro
* \ref Log4Qt::LOG4QT_ERROR "LOG4QT_ERROR" is used with classes not derived
* from QObject. The macro \ref Log4Qt::LOG4QT_QCLASS_ERROR "LOG4QT_QCLASS_ERROR"
* is used with classes derived from QObject.
*/
class LOG4QT_EXPORT LogError
{
public:
/*!
* The enum Encoding defines the 8-bit encoding of a character string
* arguments to \ref LogError::LogError(const char *, int, const char *,
* const char *, Encoding) "LogError::LogError()".
*
* \sa \ref LogError::LogError(const char *, int, const char *, const char *, Encoding) "LogError::LogError()"
*/
enum Encoding
{
/*! LATIN-1 */
LATIN1,
/*!
* The encoding specified by QTextCodec::codecForTr()
* (Latin-1 if none has been set).
*/
CODECFORTR,
/*! UTF-8 */
UNICODEUTF8
};
//Q_ENUMS(Encoding)
/*!
* Creates an empty error. The error code is set to 0 and all other
* members are set to be empty.
*
* \sa isEmpty()
*/
LogError();
/*!
* Creates an error with the Message \a rMessage and the error code
* \a code. The symbol for the error code is set to \a rSymbol and the
* context to \a rContext.
*
* \a rContext must be string that can be converted to Latin-1. The
* Latin-1 representation of the string is used with
* QApplication::translate(), if a translation for \a rMessage is
* requested.
*
* \sa translatedMessage(), translatedMessageWithArgs()
*/
LogError(const QString &rMessage,
int code = 0,
const QString &rSymbol = QString(),
const QString &rContext = QString());
/*!
* Creates an error with the Message \a pMessage and the error code
* \a code. The symbol for the error code is set to \a pSymbol and the
* context to \a pContext.
*
* \a encoding specifies the encoding of \a pMessage. \a pSymbol and
* \a pContext are expected to be Latin-1.
*
* \note To support the macros \ref Log4Qt::LOG4QT_ERROR "LOG4QT_ERROR"
* and \ref Log4Qt::LOG4QT_QCLASS_ERROR "LOG4QT_QCLASS_ERROR"
* the function tests, if \a pSymbol is the string representation of
* \a code. If it is, the symbol is set to be empty. Otherwise symbol
* is set to \a pSymbol.
*
* \sa translatedMessage(), translatedMessageWithArgs()
*/
LogError(const char *pMessage,
int code = 0,
const char *pSymbol = 0,
const char *pContext = 0,
Encoding encoding = LATIN1);
// LogError(const LogError &rOther); // Use compiler default
// virtual ~LogError(); // Use compiler default
// LogError &operator=(const LogError &rOther); // Use compiler default
/*!
* Returns the error code.
*
* \sa setCode()
*/
int code() const;
/*!
* Returns the context for the error.
*
* \sa setContext()
*/
QString context() const;
/*!
* Returns the error message.
*
* \sa setMessage()
*/
QString message() const;
/*!
* Returns the symbol for the error code.
*
* \sa setSymbol()
*/
QString symbol() const;
/*!
* Returns the translated error message.
*
* The translated message is created by calling
* QCoreApplication::translate() using context().toLatin1() as
* context and message.toUtf8() as message.
*
* \sa translatedMessageWithArgs()
*/
QString translatedMessage() const;
/*!
* Sets the error code to \a code.
*
* \sa code()
*/
void setCode(int code);
/*!
* Sets the context to \a rClassName.
*
* \a rContext must be string that can be converted to Latin-1. The
* Latin-1 representation of the string is used with
* QApplication::translate(), if a translation for \a rMessage is
* requestd.
*
* \sa context(), translatedMessage(), translatedMessageWithArgs()
*/
void setContext(const QString &rClassName);
/*!
* Sets the error message to \a rMessage
*
* \sa message()
*/
void setMessage(const QString &rMessage);
/*!
* Sets the symbol for the error code to \a rSymbol.
*
* \sa symbol()
*/
void setSymbol(const QString &rSymbol);
/*!
* Returns the last error set for the current thread using
* setLastError().
*
* \note: This function is thread-safe.
*
* \sa setLastError()
*/
static LogError lastError();
/*!
* Sets the last error for the current thread to \a rLogError.
*
* \note: This function is thread-safe.
*
* \sa lastError()
*/
static void setLastError(const LogError &rLogError);
/*!
* Appends \a rArg to the list of arguments and returns a reference to
* this error.
*
* \sa operator<<(), args(), clearArgs()
*/
LogError &addArg(const QVariant &rArg);
/*!
* This is an overloaded member function, provided for convenience.
*/
LogError &addArg(int arg);
/*!
* This is an overloaded member function, provided for convenience.
*/
LogError &addArg(const QString &rArg);
/*!
* Appends \a rLogError to the list of causing errors and returns a
* reference to this error.
*
* \sa causingErrors(), clearCausingErrors()
*/
LogError &addCausingError(const LogError &rLogError);
/*!
* Returns the list of arguments that have been added to this error.
*
* \sa addArg(), operator<<(), clearArgs()
*/
QList<QVariant> args() const;
/*!
* Returns the list of causing errors that have been added to this error.
*
* \sa addArg(), operator<<(), clearArgs()
*/
QList<LogError> causingErrors() const;
/*!
* Clears the list of arguments that have been added to this error.
*
* \sa addArg(), operator<<(), args()
*/
void clearArgs();
/*!
* Clears the list of causing errors that have been added to this error.
*
* \sa addCausingError(), causingErrors()
*/
void clearCausingErrors();
/*!
* Returns true, if the error code is 0 and the message is empty.
* Otherwise it returns false.
*
* \sa code(), message()
*/
bool isEmpty() const;
/*!
* Returns the message with arguments. The arguments are incoorporated
* into the messag using QString::arg().
*
* \sa QString::arg(), translatedMessageWithArgs()
*/
QString messageWithArgs() const;
/*!
* Returns the translated message with arguments. The arguments are
* incoorporated into the messag using QString::arg().
*
* \sa QString::arg(), messageWithArgs(), translatedMessage()
*/
QString translatedMessageWithArgs() const;
/*!
* Appends \a rArg to the list of arguments and returns a reference to
* this error.
*
* \sa addArg()
*/
LogError &operator<<(const QVariant &rArg);
/*!
* This is an overloaded member function, provided for convenience.
*/
LogError &operator<<(int arg);
/*!
* This is an overloaded member function, provided for convenience.
*/
LogError &operator<<(const QString &rArg);
/*!
* Returns a string representation of the error.
*
* The string has the following format:
*
* <tt>
* message (context::symbol, code): causing_error, causing_error
* </tt>
*
* If members are empty they are omitted:
* - Omit context, if empty
* - Omit symbol, if empty
* - Omit double colon with context and symbol, if both are empty
* - Omit code, if 0
* - Omit bracket with context/symbol and code, if all are empty
* - Omit colon with causing errors, if no causing errors exist
*/
QString toString() const;
private:
QString insertArgs(const QString &rMessage) const;
QString cleanMessage(const QString &rMessage);
private:
int mCode;
QString mContext;
QString mMessage;
QString mSymbol;
QList<QVariant> mArgs;
QList<LogError> mCausingErrors;
#ifndef QT_NO_DATASTREAM
// Needs to be friend to stream objects
friend QDataStream &operator<<(QDataStream &rStream,
const LogError &rLogError);
friend QDataStream &operator>>(QDataStream &rStream,
LogError &rLogError);
#endif // QT_NO_DATASTREAM
};
/**************************************************************************
* Operators, Helper
**************************************************************************/
#ifndef QT_NO_DATASTREAM
/*!
* \relates LogError
*
* Writes the given error \a rLogError to the given stream \a rStream,
* and returns a reference to the stream.
*/
QDataStream &operator<<(QDataStream &rStream,
const LogError &rLogError);
/*!
* \relates LogError
*
* Reads an error from the given stream \a rStream into the given
* error \a rLogError, and returns a reference to the stream.
*/
QDataStream &operator>>(QDataStream &rStream,
LogError &rLogError);
#endif // QT_NO_DATASTREAM
#ifndef QT_NO_DEBUG_STREAM
/*!
* \relates LogError
*
* Writes all object member variables to the given debug stream \a debug and
* returns the stream.
*
* <tt>
* %LogError(code:7 context:"Log4Qt::FileAppender"
* message:"Unable to open file '%1' for appender '%2'"
* symbol:"APPENDER_OPENING_FILE_ERROR"
* args:(QVariant(QString, "G:\logs\client.log") , QVariant(QString, "Client FileAppender") )
* translatedMessage: "Unable to open file '%1' for appender '%2'" )
* </tt>
*
* \sa QDebug
*/
QDebug operator<<(QDebug debug,
const LogError &rLogError);
#endif // QT_NO_DEBUG_STREAM
/**************************************************************************
* Inline
**************************************************************************/
inline int LogError::code() const
{ return mCode; }
inline QString LogError::context() const
{ return mContext; }
inline QString LogError::message() const
{ return mMessage; }
inline QString LogError::symbol() const
{ return mSymbol; }
inline void LogError::setCode(int code)
{ mCode = code; }
inline void LogError::setContext(const QString &rContext)
{ mContext = rContext; }
inline void LogError::setMessage(const QString &rMessage)
{ mMessage = cleanMessage(rMessage); }
inline void LogError::setSymbol(const QString &rSymbol)
{ mSymbol = rSymbol; }
inline LogError &LogError::addArg(const QVariant &rArg)
{ mArgs << rArg; return *this; }
inline LogError &LogError::addArg(int arg)
{ mArgs << QVariant(arg); return *this; }
inline LogError &LogError::addArg(const QString &rArg)
{ mArgs << QVariant(rArg); return *this; }
inline LogError &LogError::addCausingError(const LogError &rLogError)
{ mCausingErrors << rLogError; return *this; }
inline QList<QVariant> LogError::args() const
{ return mArgs; }
inline void LogError::clearArgs()
{ mArgs.clear(); }
inline void LogError::clearCausingErrors()
{ mCausingErrors.clear(); }
inline QList<LogError> LogError::causingErrors() const
{ return mCausingErrors; }
inline bool LogError::isEmpty() const
{ return mCode || !mMessage.isEmpty(); }
inline QString LogError::messageWithArgs() const
{ return insertArgs(message()); }
inline QString LogError::translatedMessageWithArgs() const
{ return insertArgs(translatedMessage()); }
inline LogError &LogError::operator<<(const QVariant &rArg)
{ return addArg(rArg); }
inline LogError &LogError::operator<<(int arg)
{ return addArg(arg); }
inline LogError &LogError::operator<<(const QString &rArg)
{ return addArg(rArg); }
} // namespace Log4Qt
Q_DECLARE_METATYPE(Log4Qt::LogError)
Q_DECLARE_TYPEINFO(Log4Qt::LogError, Q_MOVABLE_TYPE);
#endif // LOG4QT_ERROR_H
|