blocxx
|
00001 /******************************************************************************* 00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved. 00003 * Copyright (C) 2006, Novell, Inc. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of 00014 * Vintela, Inc., 00015 * nor Novell, Inc., 00016 * nor the names of its contributors or employees may be used to 00017 * endorse or promote products derived from this software without 00018 * specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 * POSSIBILITY OF SUCH DAMAGE. 00031 *******************************************************************************/ 00032 00033 00038 #ifndef BLOCXX_LOGGER_HPP_INCLUDE_GUARD_ 00039 #define BLOCXX_LOGGER_HPP_INCLUDE_GUARD_ 00040 #include "blocxx/BLOCXX_config.h" 00041 #include "blocxx/CommonFwd.hpp" 00042 #include "blocxx/String.hpp" 00043 #include "blocxx/LogLevel.hpp" 00044 #include "blocxx/IntrusiveCountableBase.hpp" 00045 #include "blocxx/Exception.hpp" 00046 #include "blocxx/LogAppender.hpp" 00047 #include "blocxx/GlobalString.hpp" 00048 #include <cerrno> 00049 00050 00051 namespace BLOCXX_NAMESPACE 00052 { 00053 00054 BLOCXX_DECLARE_APIEXCEPTION(Logger, BLOCXX_COMMON_API) 00055 00056 00057 00086 class BLOCXX_COMMON_API Logger : public IntrusiveCountableBase 00087 { 00088 public: 00089 00090 static const GlobalString STR_NONE_CATEGORY; 00091 static const GlobalString STR_FATAL_CATEGORY; 00092 static const GlobalString STR_ERROR_CATEGORY; 00093 static const GlobalString STR_WARNING_CATEGORY; 00094 static const GlobalString STR_INFO_CATEGORY; 00095 static const GlobalString STR_DEBUG_CATEGORY; 00096 static const GlobalString STR_DEBUG2_CATEGORY; 00097 static const GlobalString STR_DEBUG3_CATEGORY; 00098 static const GlobalString STR_ALL_CATEGORY; 00099 static const GlobalString STR_DEFAULT_COMPONENT; // "none" 00100 00101 enum ELoggerErrorCodes 00102 { 00103 E_UNKNOWN_LOG_APPENDER_TYPE, 00104 E_INVALID_MAX_FILE_SIZE, 00105 E_INVALID_MAX_BACKUP_INDEX 00106 }; 00107 00112 Logger(const String& defaultComponent = STR_DEFAULT_COMPONENT, const LogAppenderRef& appender = LogAppenderRef()); 00113 00118 Logger(const String& defaultComponent, const ELogLevel logLevel); 00119 00120 Logger(const Logger&); 00121 Logger& operator=(const Logger&); 00122 void swap(Logger& x); 00123 virtual ~Logger(); 00124 00125 virtual LoggerRef clone() const BLOCXX_DEPRECATED; // in 4.0.0 00126 00134 void logFatalError(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const; 00135 00143 void logError(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const; 00144 00152 void logWarning(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const; 00153 00161 void logInfo(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const; 00162 00170 void logDebug(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const; 00171 00179 void logDebug2(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const; 00180 00188 void logDebug3(const String& message, const char* filename = 0, int fileline = -1, const char* methodname = 0) const; 00189 00190 // Note that we don't use defaults on logMessage so the correct overload will be chosen. 00198 void logMessage(const String& component, const String& category, const String& message) const; 00209 void logMessage(const String& component, const String& category, const String& message, const char* filename, int fileline, const char* methodname) const; 00210 00217 void logMessage(const String& category, const String& message) const; 00218 00228 void logMessage(const String& category, const String& message, const char* filename, int fileline, const char* methodname) const; 00229 00235 void logMessage(const LogMessage& message) const; 00236 00242 void setDefaultComponent(const String& component); 00243 00248 String getDefaultComponent() const; 00249 00253 ELogLevel getLogLevel() const 00254 { 00255 return m_logLevel; 00256 } 00257 00263 void setLogLevel(ELogLevel logLevel); 00264 00273 void setLogLevel(const String& logLevel); 00274 00281 static ELogLevel stringToLogLevel(const String& logLevel); 00282 00289 static String logLevelToString(ELogLevel logLevel); 00290 00294 bool categoryIsEnabled(const String& category) const; 00295 00299 bool levelIsEnabled(const ELogLevel level) const; 00300 00304 bool componentAndCategoryAreEnabled(const String& component, const String& category) const; 00305 00309 static inline const Logger& asLogger(const Logger& lgr) 00310 { 00311 return lgr; 00312 } 00313 static inline const Logger& asLogger(const LoggerRef& lgr) 00314 { 00315 return *lgr; 00316 } 00317 00318 private: 00319 void processLogMessage(const LogMessage& message) const; 00320 00321 protected: // data 00322 String m_defaultComponent; 00323 LogAppenderRef m_appender; 00324 ELogLevel m_logLevel; 00325 }; 00326 BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, IntrusiveReference, Logger); 00327 00328 } // end namespace BLOCXX_NAMESPACE 00329 00330 00331 #if defined(BLOCXX_HAVE_UUPRETTY_FUNCTIONUU) 00332 #define BLOCXX_LOGGER_PRETTY_FUNCTION __PRETTY_FUNCTION__ 00333 #elif defined(BLOCXX_HAVE_C99_UUFUNCUU) 00334 #define BLOCXX_LOGGER_PRETTY_FUNCTION __func__ 00335 #else 00336 #define BLOCXX_LOGGER_PRETTY_FUNCTION "" 00337 #endif 00338 00345 #define BLOCXX_LOG_DEBUG3(logger, message) \ 00346 do \ 00347 { \ 00348 int err = errno; \ 00349 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG3_LEVEL) \ 00350 { \ 00351 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG3_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00352 } \ 00353 errno = err; \ 00354 } while (0) 00355 00356 00363 #define BLOCXX_LOG_DEBUG2(logger, message) \ 00364 do \ 00365 { \ 00366 int err = errno; \ 00367 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG2_LEVEL) \ 00368 { \ 00369 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG2_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00370 } \ 00371 errno = err; \ 00372 } while (0) 00373 00374 00381 #define BLOCXX_LOG_DEBUG(logger, message) \ 00382 do \ 00383 { \ 00384 int err = errno; \ 00385 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG_LEVEL) \ 00386 { \ 00387 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00388 } \ 00389 errno = err; \ 00390 } while (0) 00391 00392 00399 #define BLOCXX_LOG_INFO(logger, message) \ 00400 do \ 00401 { \ 00402 int err = errno; \ 00403 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_INFO_LEVEL) \ 00404 { \ 00405 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_INFO_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00406 } \ 00407 errno = err; \ 00408 } while (0) 00409 00416 #define BLOCXX_LOG_WARNING(logger, message) \ 00417 do \ 00418 { \ 00419 int err = errno; \ 00420 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_WARNING_LEVEL) \ 00421 { \ 00422 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_WARNING_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00423 } \ 00424 errno = err; \ 00425 } while (0) 00426 00433 #define BLOCXX_LOG_ERROR(logger, message) \ 00434 do \ 00435 { \ 00436 int err = errno; \ 00437 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_ERROR_LEVEL) \ 00438 { \ 00439 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_ERROR_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00440 } \ 00441 errno = err; \ 00442 } while (0) 00443 00450 #define BLOCXX_LOG_FATAL_ERROR(logger, message) \ 00451 do \ 00452 { \ 00453 int err = errno; \ 00454 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_FATAL_ERROR_LEVEL) \ 00455 { \ 00456 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_FATAL_CATEGORY, (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00457 } \ 00458 errno = err; \ 00459 } while (0) 00460 00468 #define BLOCXX_LOG(logger, category, message) \ 00469 do \ 00470 { \ 00471 int err = errno; \ 00472 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).categoryIsEnabled((category))) \ 00473 { \ 00474 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage((category), (message), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00475 } \ 00476 errno = err; \ 00477 } while (0) 00478 00489 #define BLOCXX_SLOG_DEBUG3(logger, message) \ 00490 do \ 00491 { \ 00492 int err = errno; \ 00493 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG3_LEVEL) \ 00494 { \ 00495 OStringStream buf; \ 00496 buf << message; \ 00497 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG3_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00498 } \ 00499 errno = err; \ 00500 } while (0) 00501 00512 #define BLOCXX_SLOG_DEBUG2(logger, message) \ 00513 do \ 00514 { \ 00515 int err = errno; \ 00516 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG2_LEVEL) \ 00517 { \ 00518 OStringStream buf; \ 00519 buf << message; \ 00520 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG2_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00521 } \ 00522 errno = err; \ 00523 } while (0) 00524 00535 #define BLOCXX_SLOG_DEBUG(logger, message) \ 00536 do \ 00537 { \ 00538 int err = errno; \ 00539 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_DEBUG_LEVEL) \ 00540 { \ 00541 OStringStream buf; \ 00542 buf << message; \ 00543 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_DEBUG_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00544 } \ 00545 errno = err; \ 00546 } while (0) 00547 00558 #define BLOCXX_SLOG_INFO(logger, message) \ 00559 do \ 00560 { \ 00561 int err = errno; \ 00562 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_INFO_LEVEL) \ 00563 { \ 00564 OStringStream buf; \ 00565 buf << message; \ 00566 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_INFO_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00567 } \ 00568 errno = err; \ 00569 } while (0) 00570 00581 #define BLOCXX_SLOG_WARNING(logger, message) \ 00582 do \ 00583 { \ 00584 int err = errno; \ 00585 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_WARNING_LEVEL) \ 00586 { \ 00587 OStringStream buf; \ 00588 buf << message; \ 00589 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_WARNING_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00590 } \ 00591 errno = err; \ 00592 } while (0) 00593 00604 #define BLOCXX_SLOG_ERROR(logger, message) \ 00605 do \ 00606 { \ 00607 int err = errno; \ 00608 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_ERROR_LEVEL) \ 00609 { \ 00610 OStringStream buf; \ 00611 buf << message; \ 00612 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_ERROR_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00613 } \ 00614 errno = err; \ 00615 } while (0) 00616 00626 #define BLOCXX_SLOG_FATAL_ERROR(logger, message) \ 00627 do \ 00628 { \ 00629 int err = errno; \ 00630 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).getLogLevel() >= ::BLOCXX_NAMESPACE::E_FATAL_ERROR_LEVEL) \ 00631 { \ 00632 OStringStream buf; \ 00633 buf << message; \ 00634 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage(::BLOCXX_NAMESPACE::Logger::STR_FATAL_CATEGORY, buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00635 } \ 00636 errno = err; \ 00637 } while (0) 00638 00650 #define BLOCXX_SLOG(logger, category, message) \ 00651 do \ 00652 { \ 00653 int err = errno; \ 00654 if (::BLOCXX_NAMESPACE::Logger::asLogger((logger)).categoryIsEnabled((category))) \ 00655 { \ 00656 OStringStream buf; \ 00657 buf << message; \ 00658 ::BLOCXX_NAMESPACE::Logger::asLogger((logger)).logMessage((category), buf.toString(), __FILE__, __LINE__, BLOCXX_LOGGER_PRETTY_FUNCTION); \ 00659 } \ 00660 errno = err; \ 00661 } while (0) 00662 00663 #endif // BLOCXX_LOGGER_HPP_INCLUDE_GUARD_ 00664