diff options
Diffstat (limited to 'utils/rbutilqt/logger/src/AbstractAppender.cpp')
-rw-r--r-- | utils/rbutilqt/logger/src/AbstractAppender.cpp | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/utils/rbutilqt/logger/src/AbstractAppender.cpp b/utils/rbutilqt/logger/src/AbstractAppender.cpp new file mode 100644 index 0000000000..778bbddd11 --- /dev/null +++ b/utils/rbutilqt/logger/src/AbstractAppender.cpp | |||
@@ -0,0 +1,147 @@ | |||
1 | /* | ||
2 | Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com) | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License version 2.1 | ||
6 | as published by the Free Software Foundation and appearing in the file | ||
7 | LICENSE.LGPL included in the packaging of this file. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | */ | ||
14 | // Local | ||
15 | #include "AbstractAppender.h" | ||
16 | |||
17 | // Qt | ||
18 | #include <QMutexLocker> | ||
19 | |||
20 | |||
21 | /** | ||
22 | * \class AbstractAppender | ||
23 | * | ||
24 | * \brief The AbstractAppender class provides an abstract base class for writing a log entries. | ||
25 | * | ||
26 | * The AbstractAppender class is the base interface class for all log appenders that could be used with Logger. | ||
27 | * | ||
28 | * AbstractAppender provides a common implementation for the thread safe, mutex-protected logging of application | ||
29 | * messages, such as ConsoleAppender, FileAppender or something else. AbstractAppender is abstract and can not be | ||
30 | * instantiated, but you can use any of its subclasses or create a custom log appender at your choice. | ||
31 | * | ||
32 | * Appenders are the logical devices that is aimed to be attached to Logger object by calling | ||
33 | * Logger::registerAppender(). On each log record call from the application Logger object sequentially calls write() | ||
34 | * function on all the appenders registered in it. | ||
35 | * | ||
36 | * You can subclass AbstractAppender to implement a logging target of any kind you like. It may be the external logging | ||
37 | * subsystem (for example, syslog in *nix), XML file, SQL database entries, D-Bus messages or anything else you can | ||
38 | * imagine. | ||
39 | * | ||
40 | * For the simple non-structured plain text logging (for example, to a plain text file or to the console output) you may | ||
41 | * like to subclass the AbstractStringAppender instead of AbstractAppender, which will give you a more convinient way to | ||
42 | * control the format of the log output. | ||
43 | * | ||
44 | * \sa AbstractStringAppender | ||
45 | * \sa Logger::registerAppender() | ||
46 | */ | ||
47 | |||
48 | |||
49 | //! Constructs a AbstractAppender object. | ||
50 | AbstractAppender::AbstractAppender() | ||
51 | : m_detailsLevel(Logger::Debug) | ||
52 | {} | ||
53 | |||
54 | |||
55 | //! Destructs the AbstractAppender object. | ||
56 | AbstractAppender::~AbstractAppender() | ||
57 | {} | ||
58 | |||
59 | |||
60 | //! Returns the current details level of appender. | ||
61 | /** | ||
62 | * Log records with a log level lower than a current detailsLevel() will be silently ignored by appender and would not | ||
63 | * be sent to its append() function. | ||
64 | * | ||
65 | * It provides additional logging flexibility, allowing you to set the different severity levels for different types | ||
66 | * of logs. | ||
67 | * | ||
68 | * \note This function is thread safe. | ||
69 | * | ||
70 | * \sa setDetailsLevel() | ||
71 | * \sa Logger::LogLevel | ||
72 | */ | ||
73 | Logger::LogLevel AbstractAppender::detailsLevel() const | ||
74 | { | ||
75 | QMutexLocker locker(&m_detailsLevelMutex); | ||
76 | return m_detailsLevel; | ||
77 | } | ||
78 | |||
79 | |||
80 | //! Sets the current details level of appender. | ||
81 | /** | ||
82 | * Default details level is Logger::Debug | ||
83 | * | ||
84 | * \note This function is thread safe. | ||
85 | * | ||
86 | * \sa detailsLevel() | ||
87 | * \sa Logger::LogLevel | ||
88 | */ | ||
89 | void AbstractAppender::setDetailsLevel(Logger::LogLevel level) | ||
90 | { | ||
91 | QMutexLocker locker(&m_detailsLevelMutex); | ||
92 | m_detailsLevel = level; | ||
93 | } | ||
94 | |||
95 | |||
96 | |||
97 | //! Sets the current details level of appender | ||
98 | /** | ||
99 | * This function is provided for convenience, it behaves like an above function. | ||
100 | * | ||
101 | * \sa detailsLevel() | ||
102 | * \sa Logger::LogLevel | ||
103 | */ | ||
104 | void AbstractAppender::setDetailsLevel(const QString& level) | ||
105 | { | ||
106 | setDetailsLevel(Logger::levelFromString(level)); | ||
107 | } | ||
108 | |||
109 | |||
110 | //! Tries to write the log record to this logger | ||
111 | /** | ||
112 | * This is the function called by Logger object to write a log message to the appender. | ||
113 | * | ||
114 | * \note This function is thread safe. | ||
115 | * | ||
116 | * \sa Logger::write() | ||
117 | * \sa detailsLevel() | ||
118 | */ | ||
119 | void AbstractAppender::write(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, | ||
120 | const char* function, const QString& category, const QString& message) | ||
121 | { | ||
122 | if (logLevel >= detailsLevel()) | ||
123 | { | ||
124 | QMutexLocker locker(&m_writeMutex); | ||
125 | append(timeStamp, logLevel, file, line, function, category, message); | ||
126 | } | ||
127 | } | ||
128 | |||
129 | |||
130 | /** | ||
131 | * \fn virtual void AbstractAppender::append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, | ||
132 | * int line, const char* function, const QString& message) | ||
133 | * | ||
134 | * \brief Writes the log record to the logger instance | ||
135 | * | ||
136 | * This function is called every time when user tries to write a message to this AbstractAppender instance using | ||
137 | * the write() function. Write function works as proxy and transfers only the messages with log level more or equal | ||
138 | * to the current logLevel(). | ||
139 | * | ||
140 | * Overload this function when you are implementing a custom appender. | ||
141 | * | ||
142 | * \note This function is not needed to be thread safe because it is never called directly by Logger object. The | ||
143 | * write() function works as a proxy and protects this function from concurrent access. | ||
144 | * | ||
145 | * \sa Logger::write() | ||
146 | */ | ||
147 | |||