blocxx
Logger.cpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Vintela, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Vintela, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
37
38#include "blocxx/BLOCXX_config.h"
39#include "blocxx/Logger.hpp"
41#include "blocxx/LogMessage.hpp"
42#include "blocxx/Assertion.hpp"
43#include "blocxx/Array.hpp"
47#include "blocxx/Format.hpp"
48
49namespace BLOCXX_NAMESPACE
50{
51
53
64
66Logger::Logger(const String& defaultComponent, const LogAppenderRef& appender)
67 : m_defaultComponent(defaultComponent)
68 , m_appender(appender ? appender : LogAppender::getCurrentLogAppender())
70{
72}
73
75Logger::Logger(const String& defaultComponent, const ELogLevel logLevel)
76 : m_defaultComponent(defaultComponent)
77 , m_appender(LogAppender::getCurrentLogAppender())
78 , m_logLevel(logLevel)
79{
81}
82
91
93Logger&
95{
99
100 return *this;
101}
102
104void
106{
108 m_appender.swap(x.m_appender);
109 std::swap(m_logLevel, x.m_logLevel);
110}
111
114{
115}
116
120{
121 return LoggerRef(new Logger(*this));
122}
123
125void
126Logger::logFatalError(const String& message, const char* filename, int fileline, const char* methodname) const
127{
129 {
130 processLogMessage( LogMessage(m_defaultComponent, STR_FATAL_CATEGORY, message, filename, fileline, methodname) );
131 }
132}
133
135void
136Logger::logError(const String& message, const char* filename, int fileline, const char* methodname) const
137{
139 {
140 processLogMessage( LogMessage(m_defaultComponent, STR_ERROR_CATEGORY, message, filename, fileline, methodname) );
141 }
142}
143
145void
146Logger::logWarning(const String& message, const char* filename, int fileline, const char* methodname) const
147{
149 {
150 processLogMessage( LogMessage(m_defaultComponent, STR_WARNING_CATEGORY, message, filename, fileline, methodname) );
151 }
152}
153
155void
156Logger::logInfo(const String& message, const char* filename, int fileline, const char* methodname) const
157{
159 {
160 processLogMessage( LogMessage(m_defaultComponent, STR_INFO_CATEGORY, message, filename, fileline, methodname) );
161 }
162}
163
165void
166Logger::logDebug(const String& message, const char* filename, int fileline, const char* methodname) const
167{
169 {
170 processLogMessage( LogMessage(m_defaultComponent, STR_DEBUG_CATEGORY, message, filename, fileline, methodname) );
171 }
172}
173
175void
176Logger::logDebug2(const String& message, const char* filename, int fileline, const char* methodname) const
177{
179 {
180 processLogMessage( LogMessage(m_defaultComponent, STR_DEBUG2_CATEGORY, message, filename, fileline, methodname) );
181 }
182}
183
185void
186Logger::logDebug3(const String& message, const char* filename, int fileline, const char* methodname) const
187{
189 {
190 processLogMessage( LogMessage(m_defaultComponent, STR_DEBUG3_CATEGORY, message, filename, fileline, methodname) );
191 }
192}
193
195void
196Logger::logMessage(const String& component, const String& category, const String& message) const
197{
198 processLogMessage(LogMessage(component, category, message, 0, -1, 0));
199}
200
202void
203Logger::logMessage(const String& component, const String& category, const String& message, const char* filename, int fileline, const char* methodname) const
204{
205 processLogMessage(LogMessage(component, category, message, filename, fileline, methodname));
206}
207
209void
210Logger::logMessage(const String& category, const String& message) const
211{
212 processLogMessage(LogMessage(m_defaultComponent, category, message, 0, -1, 0));
213}
214
216void
217Logger::logMessage(const String& category, const String& message, const char* filename, int fileline, const char* methodname) const
218{
219 processLogMessage(LogMessage(m_defaultComponent, category, message, filename, fileline, methodname));
220}
221
223void
224Logger::logMessage(const LogMessage& message) const
225{
226 processLogMessage(message);
227}
228
230void
232{
233 BLOCXX_ASSERT(component != "");
234 m_defaultComponent = component;
235}
236
238String
243
245void
247{
248 m_logLevel = logLevel;
249}
250
252void
257
261{
263 {
264 return E_INFO_LEVEL;
265 }
267 {
268 return E_DEBUG_LEVEL;
269 }
271 {
272 return E_DEBUG2_LEVEL;
273 }
275 {
276 return E_DEBUG3_LEVEL;
277 }
279 {
280 return E_ERROR_LEVEL;
281 }
283 {
284 return E_WARNING_LEVEL;
285 }
287 {
288 return E_ALL_LEVEL;
289 }
291 {
292 return E_NONE_LEVEL;
293 }
294 else
295 {
296 return E_FATAL_ERROR_LEVEL;
297 }
298}
299
301String
303{
304 switch (logLevel)
305 {
306 case E_ALL_LEVEL:
307 return STR_ALL_CATEGORY;
308 case E_DEBUG3_LEVEL:
309 return STR_DEBUG3_CATEGORY;
310 case E_DEBUG2_LEVEL:
311 return STR_DEBUG2_CATEGORY;
312 case E_DEBUG_LEVEL:
313 return STR_DEBUG_CATEGORY;
314 case E_WARNING_LEVEL:
316 case E_INFO_LEVEL:
317 return STR_INFO_CATEGORY;
318 case E_ERROR_LEVEL:
319 return STR_ERROR_CATEGORY;
321 return STR_FATAL_CATEGORY;
322 default:
323 return STR_NONE_CATEGORY;
324 }
325}
326
328bool
329Logger::categoryIsEnabled(const String& category) const
330{
331 return m_appender->categoryIsEnabled(category);
332}
333
335bool
337{
338 return (getLogLevel() >= level);
339}
340
342bool
343Logger::componentAndCategoryAreEnabled(const String& component, const String& category) const
344{
345 return m_appender->componentAndCategoryAreEnabled(component, category);
346}
347
349void
351{
352 BLOCXX_ASSERT(!message.component.empty());
353 BLOCXX_ASSERT(!message.category.empty());
354 BLOCXX_ASSERT(!message.message.empty());
355
356 m_appender->logMessage(message);
357}
358
359} // end namespace BLOCXX_NAMESPACE
360
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(),...
Definition Assertion.hpp:57
#define BLOCXX_DEFINE_EXCEPTION_WITH_ID(NAME)
Define a new exception class named <NAME>Exception that derives from Exception.
#define BLOCXX_GLOBAL_STRING_INIT(str)
Logging interface.
Definition Logger.hpp:87
void logDebug2(const String &message, const char *filename=0, int fileline=-1, const char *methodname=0) const
If getLogLevel() >= E_DEBUG2_LEVEL, Log debug info.
Definition Logger.cpp:176
static const GlobalString STR_DEBUG2_CATEGORY
Definition Logger.hpp:96
static const GlobalString STR_WARNING_CATEGORY
Definition Logger.hpp:93
static const GlobalString STR_DEBUG3_CATEGORY
Definition Logger.hpp:97
Logger(const String &defaultComponent=STR_DEFAULT_COMPONENT, const LogAppenderRef &appender=LogAppenderRef())
Definition Logger.cpp:66
bool categoryIsEnabled(const String &category) const
Determine if the log category is enabled.
Definition Logger.cpp:329
void logFatalError(const String &message, const char *filename=0, int fileline=-1, const char *methodname=0) const
Log message with a fatal error category and the default component.
Definition Logger.cpp:126
static const GlobalString STR_DEBUG_CATEGORY
Definition Logger.hpp:95
bool levelIsEnabled(const ELogLevel level) const
Check if the logger is enabled for given level.
Definition Logger.cpp:336
bool componentAndCategoryAreEnabled(const String &component, const String &category) const
Determine if the component and category are both enabled.
Definition Logger.cpp:343
void setLogLevel(ELogLevel logLevel)
Set the log level.
Definition Logger.cpp:246
static String logLevelToString(ELogLevel logLevel)
Convert a log level enum to a string.
Definition Logger.cpp:302
static const GlobalString STR_ALL_CATEGORY
Definition Logger.hpp:98
virtual LoggerRef clone() const BLOCXX_DEPRECATED
Definition Logger.cpp:119
LogAppenderRef m_appender
Definition Logger.hpp:323
static const GlobalString STR_FATAL_CATEGORY
Definition Logger.hpp:91
void logDebug(const String &message, const char *filename=0, int fileline=-1, const char *methodname=0) const
If getLogLevel() >= E_DEBUG_LEVEL, Log debug info.
Definition Logger.cpp:166
void setDefaultComponent(const String &component)
Sets the default component.
Definition Logger.cpp:231
static const GlobalString STR_ERROR_CATEGORY
Definition Logger.hpp:92
static ELogLevel stringToLogLevel(const String &logLevel)
Convert a log level string to an enum value.
Definition Logger.cpp:260
void processLogMessage(const LogMessage &message) const
Definition Logger.cpp:350
ELogLevel getLogLevel() const
Definition Logger.hpp:253
Logger & operator=(const Logger &)
Definition Logger.cpp:94
static const GlobalString STR_DEFAULT_COMPONENT
Definition Logger.hpp:99
void logDebug3(const String &message, const char *filename=0, int fileline=-1, const char *methodname=0) const
If getLogLevel() >= E_DEBUG3_LEVEL, Log debug info.
Definition Logger.cpp:186
void logInfo(const String &message, const char *filename=0, int fileline=-1, const char *methodname=0) const
If getLogLevel() >= E_INFO_LEVEL, Log info.
Definition Logger.cpp:156
String getDefaultComponent() const
Gets the default component.
Definition Logger.cpp:239
void logError(const String &message, const char *filename=0, int fileline=-1, const char *methodname=0) const
If getLogLevel() >= E_ERROR_LEVEL, Log message with an error category and the default component.
Definition Logger.cpp:136
void swap(Logger &x)
Definition Logger.cpp:105
static const GlobalString STR_INFO_CATEGORY
Definition Logger.hpp:94
void logMessage(const String &component, const String &category, const String &message) const
Log a message using the specified component and category The current log level is ignored.
Definition Logger.cpp:196
static const GlobalString STR_NONE_CATEGORY
Definition Logger.hpp:90
void logWarning(const String &message, const char *filename=0, int fileline=-1, const char *methodname=0) const
If getLogLevel() >= E_WARNING_LEVEL, Log info.
Definition Logger.cpp:146
This String class is an abstract data type that represents as NULL terminated string of characters.
Definition String.hpp:67
bool equalsIgnoreCase(const String &arg) const
Determine if another String object is equal to this String object, ignoring case in the comparision.
Definition String.cpp:529
Taken from RFC 1321.
IntrusiveReference< Logger > LoggerRef
Definition CommonFwd.hpp:64
LazyGlobal< String, char const *const > GlobalString
class BLOCXX_COMMON_API Logger
Definition CommonFwd.hpp:63
IntrusiveReference< LogAppender > LogAppenderRef
Definition CommonFwd.hpp:67