欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

读取配置文件动态创建对象 [C++] (3) 博客分类: OO CC++C# 

程序员文章站 2024-02-22 16:28:58
...
源代码续 

void

log4cplus :: PropertyConfigurator :: configureAppenders ()

{

    Properties   appenderProperties =

         properties . getPropertySubset ( LOG4CPLUS_TEXT ( "appender." ));

    // 根据 Appenders 文本获取相关的数据

    vector < tstring > appendersProps = appenderProperties . propertyNames ();

    for ( vector < tstring >:: iterator   it = appendersProps . begin ();

        it != appendersProps . end ();

        ++ it )

    {

        if ( (* it ). find ( LOG4CPLUS_TEXT ( '.' ) ) == tstring :: npos ) {

                   // 实现基本的数据采集获取功能

tstring   factoryName = appenderProperties . getProperty (* it );

// 解析获得创建实例所需使用的有关 Factory 的方法

AppenderFactory * factory = getAppenderFactoryRegistry (). get ( factoryName );

// 通过工厂名称来获得相关的工厂方法

            if ( factory == 0) {

                tstring   err =

                    LOG4CPLUS_TEXT ( "PropertyConfigurator::configureAppenders()- Cannot find AppenderFactory: " );

                getLogLog (). error ( err + factoryName );

                continue ;

            }

 

            Properties   properties =

                    appenderProperties . getPropertySubset ((* it ) + LOG4CPLUS_TEXT ( "." ));

try {

 

           // 创建相关的对象链接

                SharedAppenderPtr   appender = factory -> createObject ( properties );

                if ( appender . get () == 0) {

                    tstring   err =

                        LOG4CPLUS_TEXT ( "PropertyConfigurator::configureAppenders()- Failed to create appender: " );

                    getLogLog (). error ( err + * it );

                 }

// 实现对属性值的处理

                else {

                    appender -> setName (* it );

                    appenders [* it ] = appender ;

                }

            }

            catch ( std :: exception & e ) {

                tstring   err =

                    LOG4CPLUS_TEXT ( "PropertyConfigurator::configureAppenders()- Error while creating Appender: " );

                getLogLog (). error ( err + LOG4CPLUS_C_STR_TO_TSTRING ( e . what ()));

            }

        }

    } // end for loop

}

 

 

void

log4cplus :: PropertyConfigurator :: configureAdditivity ()

{

    Properties   additivityProperties =

            properties . getPropertySubset ( LOG4CPLUS_TEXT ( "additivity." ));

    vector < tstring > additivitysProps = additivityProperties . propertyNames ();

 

    for ( vector < tstring >:: iterator   it = additivitysProps . begin ();

        it != additivitysProps . end ();

        ++ it )

    {

        Logger   logger = getLogger (* it );

        tstring   actualValue = additivityProperties . getProperty (* it );

        tstring   value = toLower ( actualValue );

 

        if ( value == LOG4CPLUS_TEXT ( "true" )) {

            logger . setAdditivity ( true );

        }

        else   if ( value == LOG4CPLUS_TEXT ( "false" )) {

            logger . setAdditivity ( false );

        }

        else {

            getLogLog (). warn (   LOG4CPLUS_TEXT ( "Invalid Additivity value: "" )

                             + actualValue

                             + LOG4CPLUS_TEXT ( """ ));

        }

    }

}

相关标签: C C++ C#