c++ - 使用来自 const 成员函数的每个类 boost::log::sources::logger

标签 c++ boost boost-logging

我想要每个类都有一个日志源,例如 docs 中的 basic_logger 示例

这可行,但在从 const 成员函数记录某些内容时,我收到一个编译器错误,提示常量性,如下所示:

#include "boost/log/sources/channel_logger.hpp"
#include "boost/log/common.hpp"
#include "boost/log/utility/init/to_console.hpp"
#include "boost/log/utility/init/common_attributes.hpp"

class Test
{
    public:
        // this works, not a const function
        void test1()
        {
            BOOST_LOG(m_logger) << "Test 1";
        }

        // this will not compile, const function
        void test2() const            
        {
            BOOST_LOG(m_logger) << "Test 2";
        }
    private:
        boost::log::sources::channel_logger<std::string> m_logger;
};

int main()
{
    boost::log::add_common_attributes();
    boost::log::init_log_to_console();

    Test t;
    t.test1();
    t.test2();
    return 1;
}

在此示例中,Test::test2 给出编译错误,因为它的 const 和 m_logger 不是。 Test::test1 工作正常。

如何在没有 const_castmutable 等的情况下以干净的方式解决这个问题

最佳答案

如果日志存储在类中,那么您当然不能在 const 上下文中修改它。

我知道您要求的是其他东西,但老实说,我认为 mutable 是这里合适的解决方案。这正是 mutable 存在的用例。

关于c++ - 使用来自 const 成员函数的每个类 boost::log::sources::logger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5059471/

相关文章:

c++ - 如何将一个boost log core公开并注册到另一个

c++ - 如何获取变量以打印字符串

c++ - c++中的图像哈希,具有相似的图像特征

c++ - boost::mutex 和 boost::timed_mutex 的性能差异

c++ - 如何在 C++ 中使用键值语义查找结构成员?

boost - Yocto 1.6 工具链中没有 libboost_log

c++ - 初始化 boost 日志接收器时将函数对象作为过滤器传递

c++ - This的地址在执行期间发生变化。为什么?

c++ - 模板、静态变量等的冲突声明

c++ - Boost Bind、Boost Function、Boost Signals 和 C++ 函数指针如何相互关联?