imbue(boost::locale::gene-6ren">
gpt4 book ai didi

c++ - Boost.Log 忽略重载的流插入运算符

转载 作者:行者123 更新时间:2023-11-28 06:57:25 26 4
gpt4 key购买 nike

我有一个类,我想以某种方式出现在日志中,所以我重载了它的 <<运算符(operator):

class CWindowClassId
{
public:
// ...
friend std::wostream& operator<< (std::wostream& os, CWindowClassId const& classId);
}

在日志流中插入上面的类:

// ...
CWindowClassId classId(hWindow);
BOOST_LOG_TRIVIAL(debug) << "Window created, class = " << classId;

导致编译错误:

Error   1   error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const Sandbox::CWindowClassId' (or there is no acceptable conversion)    C:\local\boost_1_55_0\boost\log\utility\formatting_ostream.hpp  710

我知道错误在于我重载了 <<对于宽字符串。当我使用 ostream 时一切都很好而不是 wostream , 但我真的想使用宽字符串版本。

我已尝试为接收器设置语言环境:

shared_ptr<log::sinks::synchronous_sink<log::sinks::text_file_backend> > sink = log::add_file_log("log.txt");
sink->imbue(boost::locale::generator()("en_US.UTF-8"));

并且有BOOST_LOG_USE_WCHAR_T在任何与日志相关的包含之前定义。

我能做些什么来使日志记录与宽字符串一起工作 <<运营商?

我正在使用 Boost 1.55.0。

最佳答案

我认为您不能使用 BOOST_LOG_TRIVIAL 来做到这一点。 trivial logger 使用底层的boost::sources::logger 而不是boost::sources::wlogger,你可以从trivial.hpp 和trivial.cpp 中看到boost 源文件,除非我们修改源代码,否则我看不出如何更改它。如果您使用 wlogger,它会起作用。这是一个例子:

#include <iostream>
#include <string>
#include <boost/log/sources/logger.hpp>
#include <boost/log/common.hpp>

namespace src = boost::log::sources;

class CWindowClassId
{
public:
// ...
friend std::wostream& operator<< (std::wostream& os, CWindowClassId const& classId)
{
os << classId.ws;
return os;
}
public:
std::wstring ws;
};

int main(int argc, char* argv[])
{
src::wlogger lg;
CWindowClassId id;
id.ws = L"wide char";
BOOST_LOG(lg) << "Hello, World! This is a wide character message."<<id;
return 0;
}

关于c++ - Boost.Log 忽略重载的流插入运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22984225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com