输出用户定义的结构使用boost ::日志



我正在尝试输出 boost :: asio :: streambuf 对象的内容 boost :: log 库。我定义了运算符<< 以下方式:

ostream& operator<<(ostream& ostr, const boost::asio::streambuf& buffer)
{
    for (size_t i = 0; i < buffer.size(); ++i)
    {
        ostr << hex << (int) buffer_cast<const char*>(buffer.data())[i] << " ";
    }
    return ostr;
}

但是在尝试输出缓冲区的内容后:

 BOOST_LOG_TRIVIAL(trace) << buffer;

我有以下错误:

在包含的文件中 /home/bobeff/work/asio_netcomm_poc/third_party/lib/boost/boost/boost/log/sources/record_ostream.hpp:31:0, 来自/home/bobeff/work/asio_netcomm_poc/third_party/lib/boost/boost/log/log/trivial.hpp:23, 来自/home/bobeff/work/asio_netcomm_poc/server/src/server.cpp:14: /home/bobeff/work/asio_netcomm_poc/third_party/lib/boost/boost/boost/log/log/utility/formatting_ostream.hpp: 在实例化'TypeName boost :: log :: v2_mt_posix :: aux :: enable_if_formatting_ostream :: type boost :: log :: v2_mt_posix :: operator&lt;&lt;&lt; [with streamt = boost :: log :: v2_mt_posix :: basic_formatting_ostream;t = boost :: asio :: basic_streambuf&lt;>;打字 boost :: log :: v2_mt_posix :: aux :: enable_if_formatting_ostream :: type = = boost :: log :: v2_mt_posix :: basic_formatting_ostream&amp;]': /home/bobeff/work/asio_netcomm_poc/third_party/lib/lib/boost/boost/boost/log/sources/record_ostream.hpp:212:51: 需要'typeName boost :: log :: v2_mt_posix :: aux :: enable_if_record_ostream :: type boost :: log :: v2_mt_posix :: operator&lt;&lt;&lt; [带有streamt = boost :: log :: v2_mt_posix :: basic_record_ostream;t = boost :: asio :: basic_streambuf&lt;>;typename boost :: log :: v2_mt_posix :: aux :: enable_if_record_ostream :: type = = boost :: log :: v2_mt_posix :: basic_record_ostream&amp;]' /home/bobeff/work/asio_netcomm_poc/server/src/server.cpp:88:47:
从这里需要 /home/bobeff/work/asio_netcomm_poc/third_party/lib/boost/boost/boost/log/log/utility/formatting_ostream.hpp:840:19: 错误:无法绑定 'boost :: log :: v2_mt_posix :: basic_formatting_ostream :: ostream_type {aka std :: basic_ostream}'lvalue to'std :: basic_ostream&amp;&amp;'' strm.stream()&lt;&lt;价值; ^ in from/usr/include/c /4.8/iostream:39:0, 来自/home/bobeff/work/asio_netcomm_poc/server/src/server.cpp:1: /USR/include/c /4.8/ostream:602:5:错误:初始化参数1 'std :: basic_ostream&lt; _chart,_traits>&amp; std ::操作员&lt;&lt;(std :: basic_ostream&lt; _chart,_traits>&amp;&amp;&amp;,const _tp&amp;) [with _chart = char;_traits = std :: char_traits;_tp = BOOST :: ASIO :: BASIC_STREAMBUF&lt;>]' 运算符&lt;&lt;(basic_ostream&lt; _chart,_traits>&amp;&amp; ___,const _tp&amp&amp&amp; __x) ^

输出缓冲区内容的正确方法是什么?

ADL找不到您的operator<<。请参阅此答案的第一部分。

最新更新