如何使用Boost.Log跨DLL边界? [英] How can I use Boost.Log across DLL boundaries?
问题描述
我尝试整合 Boost.Log 在一个相当大的应用程序中,该应用程序由从DLL动态加载插件的主应用程序组成。最初的想法是传递日志源到插件,以便他们可以添加日志消息。但是,只要DLL中的代码尝试将消息记录到提供的源,应用程序就会崩溃,并出现访问冲突。
I am trying to integrate Boost.Log in a fairly large application that is composed of a main application that dynamically loads plugins from DLLs. The initial idea was to pass a logging source to plugins so that they can add log messages. However, as soon as code from a DLL tries to log a message to the provided source, the application crashes with an access violation.
以下最小示例说明了此问题:
The following minimal example illustrates the problem:
int main(int argc, char* argv[])
{
boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level> logger;
// This is okay
BOOST_LOG_SEV(logger, boost::log::trivial::info) << "From main()";
// This crashes
logFromDll(logger);
return 0;
}
其中 logFromDll
在单独的(DLL)项目中定义:
Where logFromDll
is defined in a separate (DLL) project:
Dll.cpp
TESTDLL_API void logFromDll(boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level> &logger)
{
BOOST_LOG_SEV(logger, boost::log::trivial::info) << "From dll";
}
如上所述,此操作在 logFromDll
(使用visual studio 2010编译)。
As stated above, this crashes with an access violation in logFromDll
(compiled with visual studio 2010).
.Log提供了一种机制,可用于全局存储:
Boost.Log provides a mechanism for "global storage" of logging sources:
声明一个全局日志记录器后,可以肯定有一个线程安全的
从应用程序代码的任何位置访问此记录器实例。
库还保证全局记录器实例将
独特,甚至跨模块边界。
Having declared a global logger, one can be sure to have a thread-safe access to this logger instance from any place of the application code. The library also guarantees that a global logger instance will be unique even across module boundaries.
像我需要的。因此,我设置了以下示例:
Sounds exactly like what I need. So I set up the following example:
Logger.h
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level>)
Main.cpp
int main(int argc, char* argv[])
{
boost::log::add_console_log
(
std::clog,
boost::log::keywords::format =
(
boost::log::expressions::stream << "[Custom format] " << boost::log::expressions::smessage
)
);
BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::info) << "From main()";
logFromDll();
return 0;
}
Dll.cpp
$ b
Dll.cpp
TESTDLL_API void logFromDll()
{
BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::info) << "From dll";
}
这不会崩溃,但会产生以下输出:
This does not crash, but yields the following output:
[Custom format] From main()
[2014-06-19 10:22:28.435366] [0x00000233] [info] From dll
也就是说,我在main.cpp中设置的自定义格式化仅适用于我日志从主项目。任何来自DLL项目的日志记录都使用默认格式进行格式化。
That is, the custom formatting that I set up in main.cpp is only applied when I log from the main project. Any logging from the DLL project is formatted using the default format.
因此,如何以所有(格式化)选项设置的方式跨DLL边界执行日志记录在主项目中正确应用?
So, how can I perform logging across DLL boundaries in a way that all (formatting) options I set up in the main project are applied correctly?
推荐答案
我已经弄清楚了什么问题。 Boost.Log的文档声明:
I've figured out what the problem was. The documentation for Boost.Log states that:
如果您的应用程序包含多个模块 b $ b一个或多个dll的)使用Boost.Log,库必须被建立为
a共享对象。如果你有一个单一的可执行文件或单个模块
使用Boost.Log,您可以将库构建为静态
库。
If your application consists of more than one module (e.g. an exe and one or several dll's) that use Boost.Log, the library must be built as a shared object. If you have a single executable or a single module that works with Boost.Log, you may build the library as a static library.
我使用Boost.Log作为静态库。通过共享链接建立提升,并在我的项目中使用它解决了这个问题。
I was using Boost.Log as a static library. Building boost with shared linkage and using that in my project resolved the problem.
这篇关于如何使用Boost.Log跨DLL边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!