使用boost :: log

时间:2016-06-17 16:20:32

标签: c++ logging boost boost-log

我试图用 boost :: log 库输出 boost :: asio :: streambuf 对象的内容。我通过以下方式定义了 operator<< 重载:

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/log/sources/record_ostream.hpp:31:0,                    来自/home/bobeff/work/asio_netcomm_poc/third_party/lib/boost/boost/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/log/utility/formatting_ostream.hpp:   在实例化&#39; typename   boost :: log :: v2_mt_posix :: aux :: enable_if_formatting_ostream :: type boost :: log :: v2_mt_posix :: operator&lt;&lt;(StreamT&amp;,T&amp;)   [使用StreamT =   提高::登录:: v2_mt_posix :: basic_formatting_ostream; T =   提高:: ASIO :: basic_streambuf&LT;取代;类型名   boost :: log :: v2_mt_posix :: aux :: enable_if_formatting_ostream :: type =   提高::登录:: v2_mt_posix :: basic_formatting_ostream和放大器;&#39 ;:   /home/bobeff/work/asio_netcomm_poc/third_party/lib/boost/boost/log/sources/record_ostream.hpp:212:51:   需要来自&#39; typename   boost :: log :: v2_mt_posix :: aux :: enable_if_record_ostream :: type boost :: log :: v2_mt_posix :: operator&lt;&lt;(StreamT&amp;,T&amp;)   [使用StreamT = boost :: log :: v2_mt_posix :: basic_record_ostream; Ť   = boost :: asio :: basic_streambuf&lt;&gt ;; typename boost :: log :: v2_mt_posix :: aux :: enable_if_record_ostream :: type =   提高::登录:: v2_mt_posix :: basic_record_ostream和放大器;]&#39;   /home/bobeff/work/asio_netcomm_poc/server/src/server.cpp:88:47:
  从这里要求   /home/bobeff/work/asio_netcomm_poc/third_party/lib/boost/boost/log/utility/formatting_ostream.hpp:840:19:   错误:无法绑定   &#39;的boost ::登录:: v2_mt_posix :: basic_formatting_ostream :: ostream_type   {aka std :: basic_ostream}&#39; lvalue to&#39; std :: basic_ostream&amp;&amp;&#39;        strm.stream()&lt;&lt;值;                      ^在/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   &#39; std :: basic_ostream&lt; _CharT,_Traits&gt;&amp;   std :: operator&lt;&lt;(std :: basic_ostream&lt; _CharT,_Traits&gt;&amp;&amp;,const _Tp&amp;)   [与_CharT = char; _Traits = std :: char_traits; _Tp =   提高:: ASIO :: basic_streambuf&LT;&GT;]&#39;        运算符&lt;&lt;(basic_ostream&lt; _CharT,_Traits&gt;&amp;&amp; __os,const _Tp&amp; __x)        ^

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

1 个答案:

答案 0 :(得分:2)

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