首页 > 代码库 > TB log 的实现

TB log 的实现

1 : 首先定义一个接口类  ITBCAFLog

/*------------------------------------------------------------------------------------------------------------------------------*//*! *    \file            ITBCAFLog.hpp * *  \brief             This file contains ITBCAFLog interface definition. * *    Copyright:        TelcoBridges 2002-2007, All Rights Reserved * *    Revision:           $Id: ITBCAFLog.hpp 63819 2008-08-08 19:51:22Z rblanchet $ * *    Notes:          Tabs = 4 * *------------------------------------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------------------------------------- |  Define header file *------------------------------------------------------------------------------------------------------------------------------*/#ifndef __ITBCAF_LOG_H__#define __ITBCAF_LOG_H__/*-------------------------------------------------------------------------------------------------------------------------------- |  Namespace declaration *------------------------------------------------------------------------------------------------------------------------------*/namespace TBCAF{/*-------------------------------------------------------------------------------------------------------------------------------- |  Forward declaration *------------------------------------------------------------------------------------------------------------------------------*/typedef class CTBCAFLogTraceListener *PCTBCAFLogTraceListener;/*-------------------------------------------------------------------------------------------------------------------------------- |  Global variables *------------------------------------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------------------------------------- |  Defines *------------------------------------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------------------------------------- |  Macros *------------------------------------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------------------------------------- |  Public Types *------------------------------------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------------------------------------- |  Private Types *------------------------------------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------------------------------------- |  Interface Documentation *------------------------------------------------------------------------------------------------------------------------------*//**    \addtogroup GRP_DOC_LOG_GENERAL System Logging * * \section GRP_DOC_LOG_CAFLOGINT Description * * The Log Interface is one of the most commonly used throughout ToolPack.  This interface is directly accessible * from the TBCAF namespace for ease of use.  This interface can be user implemented and thus allows a wide range * of logging facilities. * * This Interface is implemented by TBCAF::CTBCAFLog which in turn, uses TBCAF::ITBCAFLogDao Interface. * An xml logger, TBCAF::CTBCAFLogDao, is currently provided as reference implementation of the later interface. * By subclassing TBCAF::ITBCAFLogDao, the logging interface could easily be adapted to use syslog. * The fastest implementation of this class would be to create an implementation that simply uses std::cout(). * * \section CAFLOGINT_USAGE Usage * * As previously mentioned, the Log Interface is static to the namespace, and thus always accessible. * Here‘s a sample code showing howto re-implement the logger to use fprintf. * * \subsection CAFLOGINT_USAGE_DECL Class declaration * \include CTBCAFLogl_example_fprintf.hpp * * \subsection CAFLOGINT_USAGE_IMPL Class implementation * \include CTBCAFLogl_example_fprintf.cpp * * \subsection CAFLOGINT_USAGE_INST Class instantiation * We are ready to instantiate and initalize the framework logger. * \include ITBCAFLog_example.cpp * * From this point on, TBCAF::LogTrace() can be called from any anywhere having access to the TBCAF namespace. * * \section CAFLOGINT_CAVEATS Caveats * *    None * * \section CAFLOGINT_CLASSES Classes * *  - \ref TBCAF::LogTrace                "LogTrace() - Globally accesible logging facility" *  - \ref TBCAF::ITBCAFLog                "ITBCAFLog - Log Intefrace" *  - \ref TBCAF::CTBCAFLog                "CTBCAFLog - Default Log implementation" *  - \ref TBCAF::ITBCAFLogDao            "ITBCAFLogDao - Xml Data Access Object Interface used by TBCAF::CTBCAFLog" *  - \ref TBCAF::CTBCAFLogDao            "CTBCAFLogDao - Xml Data Access Object used by TBCAF::CTBCAFLog" * *//*-------------------------------------------------------------------------------------------------------------------------------- |  Class Definition *------------------------------------------------------------------------------------------------------------------------------*//** \addtogroup GRP_CAF_LOG_GENERAL *//*@{*//*------------------------------------------------------------------------------------------------------------------------------*//*! *    \class ITBCAFLog * *    \brief    This class represents a logging interface. * *    \remarks *            ~ *    \warning *            ~ *    \exception *            ~ * *//*@}*//*------------------------------------------------------------------------------------------------------------------------------*/typedef class ITBCAFLog *PITBCAFLog;class TBCAF_API ITBCAFLog{public:/*-------------------------------------------------------------------------------------------------------------------------------- |  Constructor/Destructor *------------------------------------------------------------------------------------------------------------------------------*/    /*!     * \brief   Constructor.     *     */    ITBCAFLog() {}    /*!     * \brief   Destructor.     *     */    virtual ~ITBCAFLog() {}/*-------------------------------------------------------------------------------------------------------------------------------- |  Public API Definition *------------------------------------------------------------------------------------------------------------------------------*/    /*!     * \brief    Initialization method of the log object     *     * \retval    TBX_RESULT_OK                    Successful     * \retval    TBX_RESULT_FAIL                    Open fail     * \retval    TBX_RESULT_OUT_OF_MEMORY        Unable to allocate context     * \retval    TBX_RESULT_NOT_SUPPORTED        Not supported with this Database type     * \retval    TBX_RESULT_INVALID_PARAM        Invalid parameter     *     */    virtual TBX_RESULT Init() = 0;    /*!     * \brief    Un-initialization method of the log object     *     * \retval    TBX_RESULT_OK                    Successful     * \retval    TBX_RESULT_NOT_SUPPORTED        Not supported with this Database type     * \retval    TBX_RESULT_INVALID_PARAM        Invalid parameter     *     */    virtual TBX_RESULT Uninit() = 0;    /*!     * \brief    Set the level trace of the log     *     * \param    in_aTraceLevel                    Trace level pre module id.     *     */    virtual TBX_RESULT SetTraceLevel( TBCAF_TRACE_LEVEL in_aTraceLevel[ TBCAF_SUBMODULE_TYPE_MAX ] ) = 0;    /*!     * \brief    Set the level trace of the log     *     * \param    in_SubModuleId                    Sub module Id     * \param    in_aTraceLevel                    Trace level pre module id.     *     */    virtual TBX_RESULT SetSubModTraceLevel(        IN        TBCAF_SUBMODULE_TYPE                in_SubModuleId,        IN        TBCAF_TRACE_LEVEL                    in_TraceLevel ) = 0;    /*!     * \brief    Get the level trace of the log     *     * \param    in_SubModuleId                    Sub module Id     *     * \result    Trace level of this sub-module Id     */    virtual TBCAF_TRACE_LEVEL GetSubModTraceLevel(        IN        TBCAF_SUBMODULE_TYPE                in_SubModuleId ) = 0;    /*!     * \brief    Set a sub module string     *     * \param    in_SubModuleId    Sub module Id.     * \param    in_pszName        Should not exceed 7 chars.     */    virtual TBX_VOID SetSubModName(        IN        TBCAF_SUBMODULE_TYPE                in_SubModuleId,        IN        PTBX_CHAR                            in_pszName ) = 0;    /*!     * \brief    Get the sub module string     *     * \param    in_SubModuleId    Sub module Id.     */    virtual const TBX_CHAR* GetSubModName(        IN        TBCAF_SUBMODULE_TYPE                in_SubModuleId ) = 0;    /*!     * \brief    Tracing method for the framework     *     * \param    in_SubModId                        Sub module Id.     * \param    in_TraceLevel                    Trace level.     * \param    in_apszPrefix                    Array of pointer to string prefix to append to trace     * \param    in_un32NbPrefix                    Number of prefix in in_pszPrefix     * \param    in_pszString                    Format string.     * \param    in_VaArgs                        Parameter list.     *     */    virtual TBX_VOID Trace    (        IN        TBCAF_SUBMODULE_TYPE            in_SubModId,        IN        TBCAF_TRACE_LEVEL                in_TraceLevel,        IN        PTBX_CHAR                        in_apszPrefix[ TBCAF_LOG_TRACE_MAX_PREFIX ],        IN        TBX_UINT32                        in_un32NbPrefix,        IN        const TBX_CHAR*                    in_pszString,        IN        TBX_VA_LIST&                    in_VaArgs    ) = 0;    /*!     * \brief    Add a listener for the traces     *     * \param    in_pTraceListener                Pointer to a trace listener object.     *     */    virtual TBX_RESULT AddTraceListener( PCTBCAFLogTraceListener in_pTraceListener ) = 0;    /*!     * \brief    Remove a listener     *     * \param    in_pTraceListener                Pointer to a trace listener object.     *     */    virtual TBX_RESULT RemoveTraceListener( PCTBCAFLogTraceListener in_pTraceListener ) = 0;/*-------------------------------------------------------------------------------------------------------------------------------- |  Disable copy constructor and assignment operator *------------------------------------------------------------------------------------------------------------------------------*/    TBCAF_DISABLE_DEFAULT_COPY_CONSTRUCTOR( ITBCAFLog );    TBCAF_DISABLE_DEFAULT_ASSIGNEMENT_OPERATOR( ITBCAFLog );};/*-------------------------------------------------------------------------------------------------------------------------------- |  Namespace declaration *------------------------------------------------------------------------------------------------------------------------------*/} /* namespace TBCAF */#endif /* __ITBCAF_LOG_H__ */

 

以上接口类全是纯虚函数 

 

2 : 默认此接口类的实现是  ctbcafLog 类 

 

/*------------------------------------------------------------------------------------------------------------------------------*//*! *    \file            CTBCAFLog.hpp * *  \brief             This file contains CTBCAFLog interface definition. * *    Copyright:        TelcoBridges 2002-2007, All Rights Reserved * *    Revision:           $Id: CTBCAFLog.hpp 84198 2010-03-17 14:09:32Z ltrottier $ * *    Notes:          Tabs = 4 * *------------------------------------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------------------------------------- |  Define header file *------------------------------------------------------------------------------------------------------------------------------*/#ifndef __CTBCAF_LOG_H__#define __CTBCAF_LOG_H__/*-------------------------------------------------------------------------------------------------------------------------------- |  Includes *------------------------------------------------------------------------------------------------------------------------------*/#include <ITBCAFLog.hpp>#include <CTBCAFMutex.hpp>#include <CTBCAFLogTraceListener.hpp>/*-------------------------------------------------------------------------------------------------------------------------------- |  Namespace declaration *------------------------------------------------------------------------------------------------------------------------------*/namespace TBCAF{/*-------------------------------------------------------------------------------------------------------------------------------- |  Forward declaration *------------------------------------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------------------------------------- |  Global variables *------------------------------------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------------------------------------- |  Defines *------------------------------------------------------------------------------------------------------------------------------*/#define ITBCAF_LOG_MAX_TRACE_LISTENER            8    /*!< Maximum number of registered listeners */#define ITBCAF_LOG_MAX_REGISTERED_SUB_MODULES    16    /*!< Maximum number of registered sub-modules that want to be notified of changed trace level */#define    ITBCAF_LOG_SUB_MODULE_MAX_NAME_LEN        8/*-------------------------------------------------------------------------------------------------------------------------------- |  Macros *------------------------------------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------------------------------------- |  Public Types *------------------------------------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------------------------------------- |  Private Types *------------------------------------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------------------------------------- |  Interface Documentation *------------------------------------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------------------------------------- |  Class Definition *------------------------------------------------------------------------------------------------------------------------------*//** \addtogroup GRP_CAF_LOG_GENERAL *//*@{*//*------------------------------------------------------------------------------------------------------------------------------*//*! *    \class CTBCAFLog * *    \brief    This class represents a logging interface. * *            The class can first be initialized with the static function \ref TBCAF::InitLog. * *    \remarks *            TBCAF Threading Info: This object is thread-safe. *    \warning *            ~ *    \exception *            ~ * *//*@}*//*------------------------------------------------------------------------------------------------------------------------------*/typedef class CTBCAFLog *PCTBCAFLog;class TBCAF_API CTBCAFLog : public ITBCAFLog{public:/*-------------------------------------------------------------------------------------------------------------------------------- |  Constructor/Destructor *------------------------------------------------------------------------------------------------------------------------------*/    /*!     * \brief   Create a log object.     *     * \param    in_pszModuleName                Name of the module.     * \param    in_paTraceLevel                    Level trace of the log per module id.     *     */    CTBCAFLog( const TBX_CHAR* in_pszModuleName, TBCAF_TRACE_LEVEL     in_paTraceLevel[ TBCAF_SUBMODULE_TYPE_MAX ] = NULL );    /*!     * \brief   Destructor.     *     */    virtual ~CTBCAFLog();/*-------------------------------------------------------------------------------------------------------------------------------- |  Public API Definition *------------------------------------------------------------------------------------------------------------------------------*/    /*!     * \brief    Initialization method of the log object     *     * \retval    TBX_RESULT_OK                    Successful     * \retval    TBX_RESULT_FAIL                    Open fail     * \retval    TBX_RESULT_OUT_OF_MEMORY        Unable to allocate context     * \retval    TBX_RESULT_NOT_SUPPORTED        Not supported with this Database type     * \retval    TBX_RESULT_INVALID_PARAM        Invalid parameter     *     */    virtual TBX_RESULT Init();    /*!     * \brief    Un-initialization method of the log object     *     * \retval    TBX_RESULT_OK                    Successful     * \retval    TBX_RESULT_NOT_SUPPORTED        Not supported with this Database type     * \retval    TBX_RESULT_INVALID_PARAM        Invalid parameter     *     */    virtual TBX_RESULT Uninit();    /*!     * \brief    Set the level trace of the log     *     * \param    in_aTraceLevel                    Trace level per module id.     *     */    virtual TBX_RESULT SetTraceLevel( TBCAF_TRACE_LEVEL in_aTraceLevel[ TBCAF_SUBMODULE_TYPE_MAX ] );    /*!     * \brief    Set the level trace of the log     *     * \param    in_SubModuleId                    Sub module Id     * \param    in_aTraceLevel                    Trace level pre module id.     *     */    virtual TBX_RESULT SetSubModTraceLevel(        IN        TBCAF_SUBMODULE_TYPE                in_SubModuleId,        IN        TBCAF_TRACE_LEVEL                    in_TraceLevel );    /*!     * \brief    Get the level trace of the log     *     * \param    in_SubModuleId                    Sub module Id     *     * \result    Trace level of this sub-module Id     */    virtual TBCAF_TRACE_LEVEL GetSubModTraceLevel(        IN        TBCAF_SUBMODULE_TYPE                in_SubModuleId );    /*!     * \brief    Set a sub module string     *     * \param    in_SubModuleId    Sub module Id.     * \param    in_pszName        Should not exceed 7 chars.     */    virtual TBX_VOID SetSubModName(        IN        TBCAF_SUBMODULE_TYPE                in_SubModuleId,        IN        PTBX_CHAR                            in_pszName );    /*!     * \brief    Get the sub module string     *     * \param    in_SubModuleId    Sub module Id.     */    virtual const TBX_CHAR* GetSubModName(        IN        TBCAF_SUBMODULE_TYPE                in_SubModuleId);        /*!     * \brief    Generic tracing method calling global logger.     *     * \param    in_SubModId                        Sub module Id.     * \param    in_TraceLevel                    Trace level.     * \param    in_apszPrefix                    Array of pointer to string prefix to append to trace     * \param    in_un32NbPrefix                    Number of prefix in in_pszPrefix     * \param    in_pszString                    Format string.     * \param    in_marker                        va_list marker.     *     */    virtual TBX_VOID Trace    (        IN        TBCAF_SUBMODULE_TYPE            in_SubModId,        IN        TBCAF_TRACE_LEVEL                in_TraceLevel,        IN        PTBX_CHAR                        in_apszPrefix[ TBCAF_LOG_TRACE_MAX_PREFIX ],        IN        TBX_UINT32                        in_un32NbPrefix,        IN        const TBX_CHAR*                    in_pszString,        IN        TBX_VA_LIST&                    in_VaArgs    ) = 0;    /*!     * \brief    Add a listener for the traces     *     * \param    in_pTraceListener                Pointer to a trace listener object.     *     */    virtual TBX_RESULT AddTraceListener( CTBCAFLogTraceListener * in_pTraceListener );    /*!     * \brief    Remove a listener     *     * \param    in_pTraceListener                Pointer to a trace listener object.     *     */    virtual TBX_RESULT RemoveTraceListener( CTBCAFLogTraceListener * in_pTraceListener );    /*!     * \brief    Get global log mutex pointer     *     */    PCTBCAFMutex GetGlobalLogMutex();/*-------------------------------------------------------------------------------------------------------------------------------- |  Protected API Definition *------------------------------------------------------------------------------------------------------------------------------*/protected:    /*!     * \brief    Return the date and time in string format.     *     * \param    out_pszTime                        Pointer to the return date and time string.     *     * \retval    TBX_RESULT_OK                    Successful     * \retval    TBX_RESULT_FAIL                    Open fail     *     */    TBX_RESULT GetStringDate( PTBX_CHAR out_pszTime );    /*!     * \brief    The method generates formatted text, under the control of the format in_pszFormat and     *             any additional arguments, and writes each generated character to the buffer out_pzsBuf.     *            This function validates that the passed arguments to ‘%s‘ are not NULL.     *     * \param    out_pzsBuf                        Pointer to the return buffer.     * \param    in_un32BufLen                    Buffer length.     * \param    in_pszFormat                    Formatted text.     * \param    in_ArgPtr                        Argument(s).     *     */    TBX_UINT32 Snprintf( PTBX_CHAR out_pzsBuf, TBX_UINT32 in_un32BufLen, const char * in_pszFormat, ... );    /*!     * \brief    The method generates formatted text, under the control of the format in_pszFormat and     *             any additional arguments, and writes each generated character to the buffer out_pzsBuf.     *            This function validates that the passed arguments to ‘%s‘ are not NULL.     *     * \param    out_pzsBuf                        Pointer to the return buffer.     * \param    in_un32BufLen                    Buffer length.     * \param    in_pszFormat                    Formatted text.     * \param    in_ArgPtr                        Argument(s).     *     */    TBX_UINT32 Vsnprintf( PTBX_CHAR out_pzsBuf, TBX_UINT32 in_un32BufLen, const char * in_pszFormat, va_list in_ArgPtr );/*-------------------------------------------------------------------------------------------------------------------------------- |  Disable copy constructor and assignment operator *------------------------------------------------------------------------------------------------------------------------------*/    TBCAF_DISABLE_DEFAULT_COPY_CONSTRUCTOR( CTBCAFLog );    TBCAF_DISABLE_DEFAULT_ASSIGNEMENT_OPERATOR( CTBCAFLog );/*-------------------------------------------------------------------------------------------------------------------------------- |  Protected Member Definition *------------------------------------------------------------------------------------------------------------------------------*/protected:    TBX_CHAR                     mszModuleName[TBCAF_MAX_MODULE_NAME];                            /*!<Module name */    TBCAF_TRACE_LEVEL             maTraceLevel[ TBCAF_SUBMODULE_TYPE_MAX ];                        /*!<Trace level of the log per module id */    TBX_CHAR                    aszSubModulePrefix[ TBCAF_SUBMODULE_TYPE_MAX ][ ITBCAF_LOG_SUB_MODULE_MAX_NAME_LEN ];        /* Prefix for all sub-modules */    PCTBCAFLogTraceListener        mapArrayOfTraceListener[ITBCAF_LOG_MAX_TRACE_LISTENER];            /*!<Array of trace listener object pointer */    TBX_UINT32                    mun32TraceListenerCount;                                        /*!<Number of trace listener in the array */    CTBCAFMutex                    mMutex;};/*-------------------------------------------------------------------------------------------------------------------------------- |  Namespace declaration *------------------------------------------------------------------------------------------------------------------------------*/} /* namespace TBCAF */#endif /* __CTBCAF_LOG_H__ */


3 : 但应用程序并不直接 new ctbcaflog , 而是 通过 CTBCAFLogParams 类来创建新的 ctbcaflog , CTBCAFLogParams

包含所有用来创建 ctbcaflog 的参数 。

 

4 :CTBCAFLogParams 的 init 函数 new 一个  CTBCAFLogGeneric

而 CTBCAFLogGeneric 的定义是 : class TBCAF_API CTBCAFLogGeneric: public CTBCAFLog

 

TB log 的实现