blocxx
Logger.hpp
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 #ifndef BLOCXX_LOGGER_HPP_INCLUDE_GUARD_
39 #define BLOCXX_LOGGER_HPP_INCLUDE_GUARD_
40 #include "blocxx/BLOCXX_config.h"
41 #include "blocxx/CommonFwd.hpp"
42 #include "blocxx/String.hpp"
43 #include "blocxx/LogLevel.hpp"
45 #include "blocxx/Exception.hpp"
46 #include "blocxx/LogAppender.hpp"
47 #include "blocxx/GlobalString.hpp"
48 #include <cerrno>
49 
50 
51 namespace BLOCXX_NAMESPACE
52 {
53 
55 
56 
57 
86 class BLOCXX_COMMON_API Logger : public IntrusiveCountableBase
87 {
88 public:
89 
99  static const GlobalString STR_DEFAULT_COMPONENT; // "none"
100 
102  {
105  E_INVALID_MAX_BACKUP_INDEX
106  };
107 
112  Logger(const String& defaultComponent = STR_DEFAULT_COMPONENT, const LogAppenderRef& appender = LogAppenderRef());
113 
118  Logger(const String& defaultComponent, const ELogLevel logLevel);
119 
120  Logger(const Logger&);
121  Logger& operator=(const Logger&);
122  void swap(Logger& x);
123  virtual ~Logger();
124 
125  virtual LoggerRef clone() const BLOCXX_DEPRECATED; // in 4.0.0
126 
134  void logFatalError(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const;
135 
143  void logError(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const;
144 
152  void logWarning(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const;
153 
161  void logInfo(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const;
162 
170  void logDebug(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const;
171 
179  void logDebug2(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const;
180 
188  void logDebug3(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const;
189 
190  // Note that we don't use defaults on logMessage so the correct overload will be chosen.
198  void logMessage(const String& component, const String& category, const String& message) const;
209  void logMessage(const String& component, const String& category, const String& message, const char* filename, int fileline, const char* methodname) const;
210 
217  void logMessage(const String& category, const String& message) const;
218 
228  void logMessage(const String& category, const String& message, const char* filename, int fileline, const char* methodname) const;
229 
235  void logMessage(const LogMessage& message) const;
236 
242  void setDefaultComponent(const String& component);
243 
248  String getDefaultComponent() const;
249 
253  ELogLevel getLogLevel() const
254  {
255  return m_logLevel;
256  }
257 
263  void setLogLevel(ELogLevel logLevel);
264 
273  void setLogLevel(const String& logLevel);
274 
281  static ELogLevel stringToLogLevel(const String& logLevel);
282 
289  static String logLevelToString(ELogLevel logLevel);
290 
294  bool categoryIsEnabled(const String& category) const;
295 
299  bool levelIsEnabled(const ELogLevel level) const;
300 
304  bool componentAndCategoryAreEnabled(const String& component, const String& category) const;
305 
309  static inline const Logger& asLogger(const Logger& lgr)
310  {
311  return lgr;
312  }
313  static inline const Logger& asLogger(const LoggerRef& lgr)
314  {
315  return *lgr;
316  }
317 
318 private:
319  void processLogMessage(const LogMessage& message) const;
320 
321 protected: // data
325 };
327 
328 } // end namespace BLOCXX_NAMESPACE
329 
330 
331 #if defined(BLOCXX_HAVE_UUPRETTY_FUNCTIONUU)
332 #define BLOCXX_LOGGER_PRETTY_FUNCTION __PRETTY_FUNCTION__
333 #elif defined(BLOCXX_HAVE_C99_UUFUNCUU)
334 #define BLOCXX_LOGGER_PRETTY_FUNCTION __func__
335 #else
336 #define BLOCXX_LOGGER_PRETTY_FUNCTION ""
337 #endif
338 
345 #define BLOCXX_LOG_DEBUG3(logger, message) \
346 do \
347 { \
348  int err = errno; \
349  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG3_LEVEL) \
350  { \
351  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG3_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
352  } \
353  errno = err; \
354 } while (0)
355 
356 
363 #define BLOCXX_LOG_DEBUG2(logger, message) \
364 do \
365 { \
366  int err = errno; \
367  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG2_LEVEL) \
368  { \
369  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG2_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
370  } \
371  errno = err; \
372 } while (0)
373 
374 
381 #define BLOCXX_LOG_DEBUG(logger, message) \
382 do \
383 { \
384  int err = errno; \
385  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG_LEVEL) \
386  { \
387  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
388  } \
389  errno = err; \
390 } while (0)
391 
392 
399 #define BLOCXX_LOG_INFO(logger, message) \
400 do \
401 { \
402  int err = errno; \
403  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_INFO_LEVEL) \
404  { \
405  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_INFO_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
406  } \
407  errno = err; \
408 } while (0)
409 
416 #define BLOCXX_LOG_WARNING(logger, message) \
417 do \
418 { \
419  int err = errno; \
420  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_WARNING_LEVEL) \
421  { \
422  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_WARNING_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
423  } \
424  errno = err; \
425 } while (0)
426 
433 #define BLOCXX_LOG_ERROR(logger, message) \
434 do \
435 { \
436  int err = errno; \
437  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_ERROR_LEVEL) \
438  { \
439  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_ERROR_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
440  } \
441  errno = err; \
442 } while (0)
443 
450 #define BLOCXX_LOG_FATAL_ERROR(logger, message) \
451 do \
452 { \
453  int err = errno; \
454  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_FATAL_ERROR_LEVEL) \
455  { \
456  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_FATAL_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
457  } \
458  errno = err; \
459 } while (0)
460 
468 #define BLOCXX_LOG(logger, category, message) \
469 do \
470 { \
471  int err = errno; \
472  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).categoryIsEnabled((category))) \
473  { \
474  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage((category), (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
475  } \
476  errno = err; \
477 } while (0)
478 
489 #define BLOCXX_SLOG_DEBUG3(logger, message) \
490 do \
491 { \
492  int err = errno; \
493  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG3_LEVEL) \
494  { \
495  OStringStream buf; \
496  buf << message; \
497  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG3_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
498  } \
499  errno = err; \
500 } while (0)
501 
512 #define BLOCXX_SLOG_DEBUG2(logger, message) \
513 do \
514 { \
515  int err = errno; \
516  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG2_LEVEL) \
517  { \
518  OStringStream buf; \
519  buf << message; \
520  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG2_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
521  } \
522  errno = err; \
523 } while (0)
524 
535 #define BLOCXX_SLOG_DEBUG(logger, message) \
536 do \
537 { \
538  int err = errno; \
539  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG_LEVEL) \
540  { \
541  OStringStream buf; \
542  buf << message; \
543  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
544  } \
545  errno = err; \
546 } while (0)
547 
558 #define BLOCXX_SLOG_INFO(logger, message) \
559 do \
560 { \
561  int err = errno; \
562  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_INFO_LEVEL) \
563  { \
564  OStringStream buf; \
565  buf << message; \
566  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_INFO_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
567  } \
568  errno = err; \
569 } while (0)
570 
581 #define BLOCXX_SLOG_WARNING(logger, message) \
582 do \
583 { \
584  int err = errno; \
585  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_WARNING_LEVEL) \
586  { \
587  OStringStream buf; \
588  buf << message; \
589  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_WARNING_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
590  } \
591  errno = err; \
592 } while (0)
593 
604 #define BLOCXX_SLOG_ERROR(logger, message) \
605 do \
606 { \
607  int err = errno; \
608  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_ERROR_LEVEL) \
609  { \
610  OStringStream buf; \
611  buf << message; \
612  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_ERROR_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
613  } \
614  errno = err; \
615 } while (0)
616 
626 #define BLOCXX_SLOG_FATAL_ERROR(logger, message) \
627 do \
628 { \
629  int err = errno; \
630  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_FATAL_ERROR_LEVEL) \
631  { \
632  OStringStream buf; \
633  buf << message; \
634  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_FATAL_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
635  } \
636  errno = err; \
637 } while (0)
638 
650 #define BLOCXX_SLOG(logger, category, message) \
651 do \
652 { \
653  int err = errno; \
654  if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).categoryIsEnabled((category))) \
655  { \
656  OStringStream buf; \
657  buf << message; \
658  ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage((category), buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
659  } \
660  errno = err; \
661 } while (0)
662 
663 #endif // BLOCXX_LOGGER_HPP_INCLUDE_GUARD_
664