如何在DLL边界内使用Boost.Log? [英] How can I use Boost.Log across DLL boundaries?

查看:238
本文介绍了如何在DLL边界内使用Boost.Log?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试整合 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).

Boost .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

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:


如果您的应用程序包含多个模块(例如,exe和
一个或几个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.

这篇关于如何在DLL边界内使用Boost.Log?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆