• 读取配置文件动态创建对象 [C++] (2)

    2006-02-02

    Tag:OO

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://jnn.blogbus.com/logs/1879308.html

    代码部分

     void

    log4cplus::PropertyConfigurator::replaceEnvironVariables()

    {

    std::vector<log4cplus::tstring> keys = properties.propertyNames();

    // 解析属性文件中的内容, 需要定义什么是主属性,什么次属性

    -- 注意 propertyName 以及 substEnvironVars

        std::vector<log4cplus::tstring>::iterator it = keys.begin();

        for(; it!=keys.end(); ++it) {

            log4cplus::tstring key = *it;

            log4cplus::tstring val = properties.getProperty(key);

            log4cplus::tstring subKey = substEnvironVars(key, getLogLog());

            if(subKey != key) {

                properties.removeProperty(key);

                properties.setProperty(subKey, val);

            }

            log4cplus::tstring subVal = substEnvironVars(val, getLogLog());

            if(subVal != val) {

                properties.setProperty(subKey, subVal);

            }

        }

    }

    void

    log4cplus::PropertyConfigurator::configureLoggers()

    {

        // 定义属性文件的属性项

        if(properties.exists( LOG4CPLUS_TEXT("rootLogger") )) {

            Logger root = h.getRoot();

            // 加载有关根log模块的信息

            configureLogger(root,

            properties.getProperty(LOG4CPLUS_TEXT("rootLogger")));

        }

        // 获取子属性部件

        Properties loggerProperties =

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

    vector<tstring> loggers = loggerProperties.propertyNames();

    // 注意ConfigureLogger部分的内容,获取有关属性值的描述

        for(vector<tstring>::iterator it=loggers.begin(); it!=loggers.end(); ++it) {

            Logger log = getLogger(*it);

            configureLogger(log, loggerProperties.getProperty(*it));

        }

    }

    void

    log4cplus::PropertyConfigurator::configureLogger(log4cplus::Logger logger,

                                                     const log4cplus::tstring& config)

    {

        // Remove all spaces from config

    tstring configString;

    //如何实现trim的功能,实现一个范型的方法调用

        remove_copy_if(config.begin(), config.end(),

                       string_append_iterator<tstring>(configString),

                       bind1st(equal_to<tchar>(), ' '));

    // "Tokenize" configString

    // 分离字符部分的内容,按照  “,”进行分隔

        vector<tstring> tokens;

        tokenize(configString, ',',

                 back_insert_iterator<vector<tstring> >(tokens));

        if(tokens.size() == 0) {

            getLogLog().error(  LOG4CPLUS_TEXT("PropertyConfigurator::configureLogger()- Invalid config string(Logger = ")

                              + logger.getName()

                              + LOG4CPLUS_TEXT("): \"")

                              + config 

                              + LOG4CPLUS_TEXT("\""));

            return;

        }

     

     

     

     

     

     

     

     

     

     

     


    收藏到:Del.icio.us