Logger.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 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 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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; // "none"
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    // Note that we don't use defaults on logMessage so the correct overload will be chosen.
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    // Derived class interface
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: // data
00351    ELogLevel m_logLevel;
00352    String m_defaultComponent;
00353 };
00354 BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, IntrusiveReference, Logger);
00355 
00356 } // end namespace BLOCXX_NAMESPACE
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

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6