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 
38 #include "blocxx/BLOCXX_config.h"
39 #include "blocxx/Logger.hpp"
40 #include "blocxx/ExceptionIds.hpp"
41 #include "blocxx/LogMessage.hpp"
42 #include "blocxx/Assertion.hpp"
43 #include "blocxx/Array.hpp"
46 #include "blocxx/LogAppender.hpp"
47 #include "blocxx/Format.hpp"
48 
49 namespace BLOCXX_NAMESPACE
50 {
51 
53 
64 
66 Logger::Logger(const String& defaultComponent, const LogAppenderRef& appender)
67  : m_defaultComponent(defaultComponent)
68  , m_appender(appender ? appender : LogAppender::getCurrentLogAppender())
69  , m_logLevel(m_appender->getLogLevel())
70 {
71  BLOCXX_ASSERT(m_defaultComponent.length());
72 }
73 
75 Logger::Logger(const String& defaultComponent, const ELogLevel logLevel)
76  : m_defaultComponent(defaultComponent)
77  , m_appender(LogAppender::getCurrentLogAppender())
78  , m_logLevel(logLevel)
79 {
80  BLOCXX_ASSERT(m_defaultComponent.length());
81 }
82 
84 Logger::Logger(const Logger& x)
86  , m_defaultComponent(x.m_defaultComponent)
87  , m_appender(x.m_appender)
88  , m_logLevel(x.m_logLevel)
89 {
90 }
91 
93 Logger&
94 Logger::operator=(const Logger& x)
95 {
99 
100  return *this;
101 }
102 
104 void
106 {
110 }
111 
114 {
115 }
116 
118 LoggerRef
119 Logger::clone() const
120 {
121  return LoggerRef(new Logger(*this));
122 }
123 
125 void
126 Logger::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 
135 void
136 Logger::logError(const String& message, const char* filename, int fileline, const char* methodname) const
137 {
138  if (m_logLevel >= E_ERROR_LEVEL)
139  {
140  processLogMessage( LogMessage(m_defaultComponent, STR_ERROR_CATEGORY, message, filename, fileline, methodname) );
141  }
142 }
143 
145 void
146 Logger::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 
155 void
156 Logger::logInfo(const String& message, const char* filename, int fileline, const char* methodname) const
157 {
158  if (m_logLevel >= E_INFO_LEVEL)
159  {
160  processLogMessage( LogMessage(m_defaultComponent, STR_INFO_CATEGORY, message, filename, fileline, methodname) );
161  }
162 }
163 
165 void
166 Logger::logDebug(const String& message, const char* filename, int fileline, const char* methodname) const
167 {
168  if (m_logLevel >= E_DEBUG_LEVEL)
169  {
170  processLogMessage( LogMessage(m_defaultComponent, STR_DEBUG_CATEGORY, message, filename, fileline, methodname) );
171  }
172 }
173 
175 void
176 Logger::logDebug2(const String& message, const char* filename, int fileline, const char* methodname) const
177 {
178  if (m_logLevel >= E_DEBUG2_LEVEL)
179  {
180  processLogMessage( LogMessage(m_defaultComponent, STR_DEBUG2_CATEGORY, message, filename, fileline, methodname) );
181  }
182 }
183 
185 void
186 Logger::logDebug3(const String& message, const char* filename, int fileline, const char* methodname) const
187 {
188  if (m_logLevel >= E_DEBUG3_LEVEL)
189  {
190  processLogMessage( LogMessage(m_defaultComponent, STR_DEBUG3_CATEGORY, message, filename, fileline, methodname) );
191  }
192 }
193 
195 void
196 Logger::logMessage(const String& component, const String& category, const String& message) const
197 {
198  processLogMessage(LogMessage(component, category, message, 0, -1, 0));
199 }
200 
202 void
203 Logger::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 
209 void
210 Logger::logMessage(const String& category, const String& message) const
211 {
212  processLogMessage(LogMessage(m_defaultComponent, category, message, 0, -1, 0));
213 }
214 
216 void
217 Logger::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 
223 void
224 Logger::logMessage(const LogMessage& message) const
225 {
227 }
228 
230 void
231 Logger::setDefaultComponent(const String& component)
232 {
233  BLOCXX_ASSERT(component != "");
234  m_defaultComponent = component;
235 }
236 
238 String
240 {
241  return m_defaultComponent;
242 }
243 
245 void
247 {
248  m_logLevel = logLevel;
249 }
250 
252 void
253 Logger::setLogLevel(const String& l)
254 {
256 }
257 
259 ELogLevel
261 {
263  {
264  return E_INFO_LEVEL;
265  }
267  {
268  return E_DEBUG_LEVEL;
269  }
271  {
272  return E_DEBUG2_LEVEL;
273  }
275  {
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 
301 String
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:
315  return STR_WARNING_CATEGORY;
316  case E_INFO_LEVEL:
317  return STR_INFO_CATEGORY;
318  case E_ERROR_LEVEL:
319  return STR_ERROR_CATEGORY;
320  case E_FATAL_ERROR_LEVEL:
321  return STR_FATAL_CATEGORY;
322  default:
323  return STR_NONE_CATEGORY;
324  }
325 }
326 
328 bool
329 Logger::categoryIsEnabled(const String& category) const
330 {
331  return m_appender->categoryIsEnabled(category);
332 }
333 
335 bool
336 Logger::levelIsEnabled(const ELogLevel level) const
337 {
338  return (getLogLevel() >= level);
339 }
340 
342 bool
343 Logger::componentAndCategoryAreEnabled(const String& component, const String& category) const
344 {
345  return m_appender->componentAndCategoryAreEnabled(component, category);
346 }
347 
349 void
350 Logger::processLogMessage(const LogMessage& message) const
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 
BLOCXX_NAMESPACE::IntrusiveCountableBase
Definition: IntrusiveCountableBase.hpp:97
BLOCXX_NAMESPACE::IntrusiveReference::swap
void swap(IntrusiveReference &rhs)
Definition: IntrusiveReference.hpp:204
LogMessage.hpp
BLOCXX_NAMESPACE::Logger::m_appender
LogAppenderRef m_appender
Definition: Logger.hpp:353
BLOCXX_NAMESPACE::Logger::m_defaultComponent
String m_defaultComponent
Definition: Logger.hpp:352
BLOCXX_NAMESPACE::E_WARNING_LEVEL
@ E_WARNING_LEVEL
Definition: LogLevel.hpp:110
BLOCXX_NAMESPACE::Logger::logLevelToString
static String logLevelToString(ELogLevel logLevel)
Convert a log level enum to a string.
Definition: Logger.cpp:332
BLOCXX_NAMESPACE::Logger::getLogLevel
ELogLevel getLogLevel() const
Definition: Logger.hpp:283
BLOCXX_NAMESPACE::IntrusiveReference
Definition: IntrusiveReference.hpp:106
BLOCXX_NAMESPACE::Logger::logDebug
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:196
BLOCXX_NAMESPACE::Logger::STR_WARNING_CATEGORY
static const GlobalString STR_WARNING_CATEGORY
Definition: Logger.hpp:123
BLOCXX_NAMESPACE::Logger::levelIsEnabled
bool levelIsEnabled(const ELogLevel level) const
Check if the logger is enabled for given level.
Definition: Logger.cpp:366
ExceptionIds.hpp
BLOCXX_NAMESPACE::String
This String class is an abstract data type that represents as NULL terminated string of characters.
Definition: String.hpp:97
BLOCXX_NAMESPACE::E_ALL_LEVEL
@ E_ALL_LEVEL
Definition: LogLevel.hpp:115
BLOCXX_NAMESPACE::String::swap
void swap(String &x)
Swap this instance with another.
Definition: String.cpp:339
BLOCXX_NAMESPACE::Logger::Logger
Logger(const String &defaultComponent=STR_DEFAULT_COMPONENT, const LogAppenderRef &appender=LogAppenderRef())
Definition: Logger.cpp:96
BLOCXX_NAMESPACE
Taken from RFC 1321.
Definition: AppenderLogger.cpp:48
Format.hpp
BLOCXX_NAMESPACE::Logger::getDefaultComponent
String getDefaultComponent() const
Gets the default component.
Definition: Logger.cpp:269
Assertion.hpp
BLOCXX_NAMESPACE::E_DEBUG_LEVEL
@ E_DEBUG_LEVEL
Definition: LogLevel.hpp:112
BLOCXX_NAMESPACE::Logger::STR_INFO_CATEGORY
static const GlobalString STR_INFO_CATEGORY
Definition: Logger.hpp:124
BLOCXX_NAMESPACE::swap
void swap(Array< T > &x, Array< T > &y)
Definition: ArrayImpl.hpp:474
BLOCXX_NAMESPACE::Logger::stringToLogLevel
static ELogLevel stringToLogLevel(const String &logLevel)
Convert a log level string to an enum value.
Definition: Logger.cpp:290
BLOCXX_NAMESPACE::Logger::setLogLevel
void setLogLevel(ELogLevel logLevel)
Set the log level.
Definition: Logger.cpp:276
BLOCXX_NAMESPACE::Logger::logError
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:166
BLOCXX_NAMESPACE::Logger::processLogMessage
void processLogMessage(const LogMessage &message) const
Definition: Logger.cpp:380
BLOCXX_NAMESPACE::Logger::logWarning
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:176
BLOCXX_NAMESPACE::E_ERROR_LEVEL
@ E_ERROR_LEVEL
Definition: LogLevel.hpp:109
LogMessagePatternFormatter.hpp
BLOCXX_NAMESPACE::E_DEBUG2_LEVEL
@ E_DEBUG2_LEVEL
Definition: LogLevel.hpp:113
BLOCXX_NAMESPACE::Logger::swap
void swap(Logger &x)
Definition: Logger.cpp:135
AppenderLogger.hpp
BLOCXX_NAMESPACE::Logger::logFatalError
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:156
BLOCXX_NAMESPACE::Logger::STR_DEBUG3_CATEGORY
static const GlobalString STR_DEBUG3_CATEGORY
Definition: Logger.hpp:127
BLOCXX_NAMESPACE::Logger
class BLOCXX_COMMON_API Logger
Definition: CommonFwd.hpp:63
BLOCXX_NAMESPACE::GlobalString
LazyGlobal< String, char const *const > GlobalString
Definition: GlobalString.hpp:79
BLOCXX_NAMESPACE::Logger::STR_FATAL_CATEGORY
static const GlobalString STR_FATAL_CATEGORY
Definition: Logger.hpp:121
BLOCXX_NAMESPACE::LoggerRef
IntrusiveReference< Logger > LoggerRef
Definition: CommonFwd.hpp:64
BLOCXX_NAMESPACE::ELogLevel
ELogLevel
Definition: LogLevel.hpp:76
Array.hpp
BLOCXX_NAMESPACE::Logger::STR_ALL_CATEGORY
static const GlobalString STR_ALL_CATEGORY
Definition: Logger.hpp:128
BLOCXX_NAMESPACE::LogAppender
class BLOCXX_COMMON_API LogAppender
Definition: CommonFwd.hpp:66
BLOCXX_NAMESPACE::E_FATAL_ERROR_LEVEL
@ E_FATAL_ERROR_LEVEL
Definition: LogLevel.hpp:108
BLOCXX_NAMESPACE::Logger::STR_DEFAULT_COMPONENT
static const GlobalString STR_DEFAULT_COMPONENT
Definition: Logger.hpp:129
BLOCXX_NAMESPACE::Logger::logDebug2
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:206
BLOCXX_GLOBAL_STRING_INIT
#define BLOCXX_GLOBAL_STRING_INIT(str)
Definition: GlobalString.hpp:81
BLOCXX_NAMESPACE::LogAppender::logMessage
void logMessage(const LogMessage &message) const
Log a message using the specified component and category.
Definition: LogAppender.cpp:289
BLOCXX_NAMESPACE::LogAppender::componentAndCategoryAreEnabled
bool componentAndCategoryAreEnabled(const String &component, const String &category) const
Definition: LogAppender.cpp:308
BLOCXX_NAMESPACE::Logger::STR_ERROR_CATEGORY
static const GlobalString STR_ERROR_CATEGORY
Definition: Logger.hpp:122
BLOCXX_NAMESPACE::Logger::categoryIsEnabled
bool categoryIsEnabled(const String &category) const
Determine if the log category is enabled.
Definition: Logger.cpp:359
BLOCXX_NAMESPACE::LogAppender::categoryIsEnabled
bool categoryIsEnabled(const String &category) const
Definition: LogAppender.cpp:301
BLOCXX_NAMESPACE::Logger::clone
virtual LoggerRef clone() const BLOCXX_DEPRECATED
Definition: Logger.cpp:149
BLOCXX_NAMESPACE::Logger::STR_DEBUG_CATEGORY
static const GlobalString STR_DEBUG_CATEGORY
Definition: Logger.hpp:125
BLOCXX_NAMESPACE::Logger
Logging interface.
Definition: Logger.hpp:117
BLOCXX_DEFINE_EXCEPTION_WITH_ID
#define BLOCXX_DEFINE_EXCEPTION_WITH_ID(NAME)
Define a new exception class named <NAME>Exception that derives from Exception.
Definition: Exception.hpp:479
BLOCXX_NAMESPACE::Logger::logInfo
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:186
BLOCXX_NAMESPACE::E_DEBUG3_LEVEL
@ E_DEBUG3_LEVEL
Definition: LogLevel.hpp:114
BLOCXX_NAMESPACE::Logger::logDebug3
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:216
BLOCXX_NAMESPACE::LogMessage
Definition: LogMessage.hpp:77
BLOCXX_NAMESPACE::Logger::logMessage
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:226
BLOCXX_NAMESPACE::Logger::setDefaultComponent
void setDefaultComponent(const String &component)
Sets the default component.
Definition: Logger.cpp:261
LogAppender.hpp
BLOCXX_NAMESPACE::Logger::componentAndCategoryAreEnabled
bool componentAndCategoryAreEnabled(const String &component, const String &category) const
Determine if the component and category are both enabled.
Definition: Logger.cpp:373
BLOCXX_NAMESPACE::Logger::~Logger
virtual ~Logger()
Definition: Logger.cpp:143
BLOCXX_NAMESPACE::LogAppenderRef
IntrusiveReference< LogAppender > LogAppenderRef
Definition: CommonFwd.hpp:67
BLOCXX_NAMESPACE::Logger::STR_DEBUG2_CATEGORY
static const GlobalString STR_DEBUG2_CATEGORY
Definition: Logger.hpp:126
BLOCXX_NAMESPACE::E_INFO_LEVEL
@ E_INFO_LEVEL
Definition: LogLevel.hpp:111
BLOCXX_ASSERT
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(),...
Definition: Assertion.hpp:87
BLOCXX_NAMESPACE::String::equalsIgnoreCase
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
BLOCXX_NAMESPACE::Logger::operator=
Logger & operator=(const Logger &)
Definition: Logger.cpp:124
Logger.hpp
BLOCXX_NAMESPACE::Logger::m_logLevel
ELogLevel m_logLevel
Definition: Logger.hpp:354
BLOCXX_NAMESPACE::Logger::STR_NONE_CATEGORY
static const GlobalString STR_NONE_CATEGORY
Definition: Logger.hpp:120
BLOCXX_NAMESPACE::E_NONE_LEVEL
@ E_NONE_LEVEL
Definition: LogLevel.hpp:107