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 {
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 {
81 }
82 
86  , m_defaultComponent(x.m_defaultComponent)
87  , m_appender(x.m_appender)
88  , m_logLevel(x.m_logLevel)
89 {
90 }
91 
93 Logger&
95 {
99 
100  return *this;
101 }
102 
104 void
106 {
110 }
111 
114 {
115 }
116 
118 LoggerRef
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 {
226  processLogMessage(message);
227 }
228 
230 void
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
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  {
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 
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
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
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