00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00037 #ifndef BLOCXX_LOGGER_HPP_INCLUDE_GUARD_
00038 #define BLOCXX_LOGGER_HPP_INCLUDE_GUARD_
00039 #include "blocxx/BLOCXX_config.h"
00040 #include "blocxx/CommonFwd.hpp"
00041 #include "blocxx/String.hpp"
00042 #include "blocxx/StringStream.hpp"
00043 #include "blocxx/LogConfig.hpp"
00044 #include "blocxx/IntrusiveCountableBase.hpp"
00045 #include "blocxx/Exception.hpp"
00046 #include <errno.h>
00047
00048 namespace BLOCXX_NAMESPACE
00049 {
00050
00051 BLOCXX_DECLARE_APIEXCEPTION(Logger, BLOCXX_COMMON_API)
00052
00053
00054
00078 class BLOCXX_COMMON_API Logger : public IntrusiveCountableBase
00079 {
00080 public:
00081
00082 static const String STR_NONE_CATEGORY;
00083 static const String STR_FATAL_CATEGORY;
00084 static const String STR_ERROR_CATEGORY;
00085 static const String STR_INFO_CATEGORY;
00086 static const String STR_DEBUG_CATEGORY;
00087 static const String STR_ALL_CATEGORY;
00088 static const String STR_DEFAULT_COMPONENT;
00089
00090 enum ELoggerErrorCodes
00091 {
00092 E_UNKNOWN_LOG_APPENDER_TYPE,
00093 E_INVALID_MAX_FILE_SIZE,
00094 E_INVALID_MAX_BACKUP_INDEX
00095 };
00096
00097
00107 BLOCXX_COMMON_API static String getConfigItem(
00108 const LoggerConfigMap& configItems,
00109 const String &itemName,
00110 const String& defRetVal = String());
00111
00124 BLOCXX_COMMON_API static void setConfigItem(
00125 LoggerConfigMap& configItems,
00126 const String& itemName,
00127 const String& value,
00128 EOverwritePreviousConfigItemFlag
00129 overwritePrevious=E_OVERWRITE_PREVIOUS_CONFIG_ITEM);
00130
00137 static blocxx::LoggerRef getCurrentLogger();
00138
00145 static blocxx::LoggerRef getDefaultLogger();
00146
00150 static bool setDefaultLogger(const blocxx::LoggerRef &ref);
00151
00155 static bool setThreadLogger(const blocxx::LoggerRef &ref);
00156
00157
00165 void logFatalError(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const;
00166
00174 void logError(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const;
00175
00183 void logInfo(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const;
00184
00192 void logDebug(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const;
00193
00194
00202 void logMessage(const String& component, const String& category, const String& message) const;
00213 void logMessage(const String& component, const String& category, const String& message, const char* filename, int fileline, const char* methodname) const;
00214
00221 void logMessage(const String& category, const String& message) const;
00222
00232 void logMessage(const String& category, const String& message, const char* filename, int fileline, const char* methodname) const;
00233
00239 void logMessage(const LogMessage& message) const;
00240
00246 void setDefaultComponent(const String& component);
00247
00252 String getDefaultComponent() const;
00253
00257 ELogLevel getLogLevel() const
00258 {
00259 return m_logLevel;
00260 }
00261
00267 void setLogLevel(ELogLevel logLevel);
00268
00276 void setLogLevel(const String& logLevel);
00277
00281 bool categoryIsEnabled(const String& category) const;
00282
00286 bool levelIsEnabled(const ELogLevel level);
00287
00291 bool componentAndCategoryAreEnabled(const String& component, const String& category) const;
00292
00301 LoggerRef clone() const;
00302
00303 virtual ~Logger();
00304
00305 protected:
00306
00307
00312 Logger(const String& defaultComponent, const ELogLevel logLevel);
00313
00318 virtual void doProcessLogMessage(const LogMessage& message) const = 0;
00319
00324 virtual bool doCategoryIsEnabled(const String& category) const;
00325
00330 virtual bool doComponentAndCategoryAreEnabled(const String& component, const String& category) const;
00331
00340 virtual LoggerRef doClone() const = 0;
00341
00342 protected:
00343 Logger(const Logger&);
00344 Logger& operator=(const Logger&);
00345 void swap(Logger& x);
00346
00347 private:
00348 void processLogMessage(const LogMessage& message) const;
00349
00350 private:
00351 ELogLevel m_logLevel;
00352 String m_defaultComponent;
00353 };
00354 BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, IntrusiveReference, Logger);
00355
00356 }
00357
00358
00359 #if defined(BLOCXX_HAVE_UUPRETTY_FUNCTIONUU)
00360 #define BLOCXX_LOGGER_PRETTY_FUNCTION __PRETTY_FUNCTION__
00361 #elif defined(BLOCXX_HAVE_C99_UUFUNCUU)
00362 #define BLOCXX_LOGGER_PRETTY_FUNCTION __func__
00363 #else
00364 #define BLOCXX_LOGGER_PRETTY_FUNCTION ""
00365 #endif
00366
00374 #define BLOCXX_LOG_DEBUG(logger, message) \
00375 do \
00376 { \
00377 int err = errno; \
00378 if ((logger)->getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG_LEVEL) \
00379 { \
00380 (logger)->logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
00381 } \
00382 errno = err; \
00383 } while (0)
00384
00392 #define BLOCXX_LOG_INFO(logger, message) \
00393 do \
00394 { \
00395 int err = errno; \
00396 if ((logger)->getLogLevel() >= ::BLOCXX_NAMESPACE::E_INFO_LEVEL) \
00397 { \
00398 (logger)->logMessage(::BLOCXX_NAMESPACE::Logger::STR_INFO_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
00399 } \
00400 errno = err; \
00401 } while (0)
00402
00410 #define BLOCXX_LOG_ERROR(logger, message) \
00411 do \
00412 { \
00413 int err = errno; \
00414 if ((logger)->getLogLevel() >= ::BLOCXX_NAMESPACE::E_ERROR_LEVEL) \
00415 { \
00416 (logger)->logMessage(::BLOCXX_NAMESPACE::Logger::STR_ERROR_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
00417 } \
00418 errno = err; \
00419 } while (0)
00420
00427 #define BLOCXX_LOG_FATAL_ERROR(logger, message) \
00428 do \
00429 { \
00430 int err = errno; \
00431 if ((logger)->getLogLevel() >= ::BLOCXX_NAMESPACE::E_FATAL_ERROR_LEVEL) \
00432 { \
00433 (logger)->logMessage(::BLOCXX_NAMESPACE::Logger::STR_FATAL_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
00434 } \
00435 errno = err; \
00436 } while (0)
00437
00446 #define BLOCXX_LOG(logger, category, message) \
00447 do \
00448 { \
00449 int err = errno; \
00450 if ((logger)->categoryIsEnabled((category))) \
00451 { \
00452 (logger)->logMessage((category), (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
00453 } \
00454 errno = err; \
00455 } while (0)
00456
00457
00468 #define BLOCXX_SLOG_DEBUG(logger, message) \
00469 do \
00470 { \
00471 int err = errno; \
00472 if ((logger)->getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG_LEVEL) \
00473 { \
00474 OStringStream buf; \
00475 buf << message; \
00476 (logger)->logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
00477 } \
00478 errno = err; \
00479 } while (0)
00480
00491 #define BLOCXX_SLOG_INFO(logger, message) \
00492 do \
00493 { \
00494 int err = errno; \
00495 if ((logger)->getLogLevel() >= ::BLOCXX_NAMESPACE::E_INFO_LEVEL) \
00496 { \
00497 OStringStream buf; \
00498 buf << message; \
00499 (logger)->logMessage(::BLOCXX_NAMESPACE::Logger::STR_INFO_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
00500 } \
00501 errno = err; \
00502 } while (0)
00503
00514 #define BLOCXX_SLOG_ERROR(logger, message) \
00515 do \
00516 { \
00517 int err = errno; \
00518 if ((logger)->getLogLevel() >= ::BLOCXX_NAMESPACE::E_ERROR_LEVEL) \
00519 { \
00520 OStringStream buf; \
00521 buf << message; \
00522 (logger)->logMessage(::BLOCXX_NAMESPACE::Logger::STR_ERROR_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
00523 } \
00524 errno = err; \
00525 } while (0)
00526
00536 #define BLOCXX_SLOG_FATAL_ERROR(logger, message) \
00537 do \
00538 { \
00539 int err = errno; \
00540 if ((logger)->getLogLevel() >= ::BLOCXX_NAMESPACE::E_FATAL_ERROR_LEVEL) \
00541 { \
00542 OStringStream buf; \
00543 buf << message; \
00544 (logger)->logMessage(::BLOCXX_NAMESPACE::Logger::STR_FATAL_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
00545 } \
00546 errno = err; \
00547 } while (0)
00548
00560 #define BLOCXX_SLOG(logger, category, message) \
00561 do \
00562 { \
00563 int err = errno; \
00564 if ((logger)->categoryIsEnabled((category))) \
00565 { \
00566 OStringStream buf; \
00567 buf << message; \
00568 (logger)->logMessage((category), buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \
00569 } \
00570 errno = err; \
00571 } while (0)
00572
00573
00574
00575 #endif