Boost.Log: 手动注册属性

Boost.Log: register attributes manually

在我的代码中使用 Boost.Log 我为我的日志输出注册了一个格式化程序

register_simple_formatter_factory<LogLevel, char>("Severity");

这在一段时间内按预期工作,但现在我尝试在不同的平台上构建并遇到链接器错误

undefined reference to `void boost::log::v2s_mt_posix::register_formatter_factory<char>(boost::log::v2s_mt_posix::attribute_name const&, boost::shared_ptr<boost::log::v2s_mt_posix::formatter_factory<char> > const&)'

我的猜测是,这是因为软件包存储库中安装的库是使用 BOOST_LOG_WITHOUT_DEFAULT_FACTORIES 定义的构建的。根据 the Boost.Log documentation 这意味着

the user will have to register all attributes in the library before parsing any filters or formatters from strings.

但是我该怎么做呢?具体要用什么来替换对 register_simple_formatter_factory 的调用才能达到相同的效果。

My guess is that this is because the installed library from the package repository was build with BOOST_LOG_WITHOUT_DEFAULT_FACTORIES defined.

不,这不是 linking 错误的原因。禁用默认工厂不会删除工厂注册 API。

But how do I do this? What specifically do I have to replace my call to register_simple_formatter_factory with to have the same effect.

您不需要用任何东西替换 register_simple_formatter_factory。相反,您必须为您使用的每个属性调用 register_simple_formatter_factoryregister_formatter_factory,即使您以前不必这样做。过滤器相同(register_filter_factoryregister_simple_filter_factory)。在以前使用默认工厂的地方,您现在必须手动创建和注册工厂。

关于 linking 错误,请先检查 this。确保 link 库中的名称空间名称与 linker 错误中的名称相匹配。如果不是,定义配置宏来纠正不匹配。

此外,请确保您 link 使用 boost_log_setup,而不仅仅是 boost_logboost_log_setup 应该在 linker 命令行中的 boost_log 之前。