【问题标题】:How to use one shared config file in C++ Boost with a sink per application如何在 C++ Boost 中使用一个共享配置文件和每个应用程序的接收器
【发布时间】:2021-06-28 08:01:47
【问题描述】:

我可以轻松创建一个配置文件来将消息发送到控制台、日志文件、DebugView 和 Eventlog,但我不知道如何配置它 - 作为共享配置文件 - 应用程序“A”有 4 个不同的接收器部分比应用程序“B”。

  1. 这可能吗?
  2. 如何实现? (有链接吗?)

我在 main 函数中的基本设置如下:

std::ifstream logConfiguration(getCwd() + "\\test-logging.ini");
try {
    boost::log::init_from_stream(logConfiguration);
    boost::log::add_common_attributes();
    boost::log::core::get()->add_global_attribute("Scope", boost::log::attributes::named_scope());
}
catch (std::exception e) {
    MYTRACE(error) << e.what() << std::endl;
    return 1;
}

亲切的问候, 托马斯

【问题讨论】:

    标签: c++ boost-log


    【解决方案1】:
    1. 这可能吗?

    是的,但 Boost.Log 开箱即用不支持。

    1. 如何实现? (有链接吗?)

    您可以自己实现读取配置文件,也可以在 Boost.Log 中重用配置文件解析器。例如:

    std::ifstream logConfiguration(getCwd() + "\\test-logging.ini");
    boost::log::settings unifiedConfig = boost::log::parse_settings(logConfiguration);
    

    这里,settings 是一个容器,其中包含从文件中解析的所有设置(有关文档,请参阅 herehere)。

    解析文件后,您必须构建一个过滤设置容器,该容器对应于每个特定的应用程序。如何执行此操作取决于统一设置文件格式。例如,如果您的每个应用程序设置都存储在统一配置的单独子部分中,则可以像这样简单:

    boost::property_tree::ptree const& unifiedPtree = unifiedConfig.property_tree();
    boost::optional<boost::property_tree::ptree const&> ptreeA =
        unifiedPtree.get_child_optional("A");
    if (ptreeA) {
        // We have some settings for application A
        boost::log::settings configA(*ptreeA);
    
        // ...
    }
    

    如果您有更复杂的统一配置格式,您可能需要遍历 ptree 节点以过滤掉与不同应用程序相关的设置。有关详细信息,请参阅 Boost.PropertyTree documentation

    最后,当你构建了特定于应用程序的设置容器后,你可以在 Boost.Log 中调用init_from_settings

    boost::log::init_from_settings(configA);
    

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多