在 Boost.Log 中正确重载运算符 <<
Properly overload operator << in Boost.Log
Note
The library uses basic_formatting_ostream
stream type for record formatting, so when customizing attribute value formatting
rules the operator<<
must use basic_formatting_ostream
instead of
std::ostream
.
但是,在整个文档中,我所看到的只是在 std::ostream
上重载 operator <<
,而不是示例代码中的 basic_formatting_ostream
。例如,参见自定义类型 severity_level
here.
的重载
根据我的测试,std::ostream
和 basic_formatting_ostream
上的重载都工作正常。所以,我想知道重载一个而不是另一个的优点是什么。
仅 operator << (std::ostream&, ...)
重载没有问题,因为 formatting_ostream
有
template< typename CharT, typename TraitsT, typename AllocatorT, typename T >
inline basic_formatting_ostream< CharT, TraitsT, AllocatorT >&
operator<< (basic_formatting_ostream< CharT, TraitsT, AllocatorT >& strm, T const& value)
{
strm.stream() << value;
return strm;
}
其中 stream()
returns std::ostream&
。如果你用第一个参数 formatting_ostream
重载 operator <<
,那么它只能用于 boost::log
,如果你重载 std::ostream&
,那么它可以用于 boost::log
] 和另一个输出。
引用自头文件:
* This stream wrapper is used by the library for log record formatting. It implements the standard string stream interface
* with a few differences:
*
* \li It does not derive from standard types <tt>std::basic_ostream</tt>, <tt>std::basic_ios</tt> and <tt>std::ios_base</tt>,
* although it tries to implement their interfaces closely. There are a few small differences, mostly regarding <tt>rdbuf</tt>
* and <tt>str</tt> signatures, as well as the supported insertion operator overloads. The actual wrapped stream can be accessed
* through the <tt>stream</tt> methods.
* \li By default, \c bool values are formatted using alphabetical representation rather than numeric.
* \li The stream supports writing strings of character types different from the stream character type. The stream will perform
* character code conversion as needed using the imbued locale.
* \li The stream operates on an external string object rather than on the embedded one. The string can be attached or detached
* from the stream dynamically.
*
* Although <tt>basic_formatting_ostream</tt> does not derive from <tt>std::basic_ostream</tt>, users are not required to add
* special overloads of \c operator<< for it since the stream will by default reuse the operators for <tt>std::basic_ostream</tt>.
* However, one can define special overloads of \c operator<< for <tt>basic_formatting_ostream</tt> if a certain type needs
* special formatting when output to log.
如果您只重载 operator<<(std::ostream)
,它将适用于每个流输出,包括到 basic_formatting_ostream
的输出。如果你只重载 operator<<(basic_formatting_ostream)
它只会输出到那种类型的流。
但是,您可能希望重载两者,例如,如果您希望向日志提供不同的或更多的信息(例如对象的地址)。
Note
The library uses
basic_formatting_ostream
stream type for record formatting, so when customizing attribute value formatting rules theoperator<<
must usebasic_formatting_ostream
instead ofstd::ostream
.
但是,在整个文档中,我所看到的只是在 std::ostream
上重载 operator <<
,而不是示例代码中的 basic_formatting_ostream
。例如,参见自定义类型 severity_level
here.
根据我的测试,std::ostream
和 basic_formatting_ostream
上的重载都工作正常。所以,我想知道重载一个而不是另一个的优点是什么。
仅 operator << (std::ostream&, ...)
重载没有问题,因为 formatting_ostream
有
template< typename CharT, typename TraitsT, typename AllocatorT, typename T >
inline basic_formatting_ostream< CharT, TraitsT, AllocatorT >&
operator<< (basic_formatting_ostream< CharT, TraitsT, AllocatorT >& strm, T const& value)
{
strm.stream() << value;
return strm;
}
其中 stream()
returns std::ostream&
。如果你用第一个参数 formatting_ostream
重载 operator <<
,那么它只能用于 boost::log
,如果你重载 std::ostream&
,那么它可以用于 boost::log
] 和另一个输出。
引用自头文件:
* This stream wrapper is used by the library for log record formatting. It implements the standard string stream interface
* with a few differences:
*
* \li It does not derive from standard types <tt>std::basic_ostream</tt>, <tt>std::basic_ios</tt> and <tt>std::ios_base</tt>,
* although it tries to implement their interfaces closely. There are a few small differences, mostly regarding <tt>rdbuf</tt>
* and <tt>str</tt> signatures, as well as the supported insertion operator overloads. The actual wrapped stream can be accessed
* through the <tt>stream</tt> methods.
* \li By default, \c bool values are formatted using alphabetical representation rather than numeric.
* \li The stream supports writing strings of character types different from the stream character type. The stream will perform
* character code conversion as needed using the imbued locale.
* \li The stream operates on an external string object rather than on the embedded one. The string can be attached or detached
* from the stream dynamically.
*
* Although <tt>basic_formatting_ostream</tt> does not derive from <tt>std::basic_ostream</tt>, users are not required to add
* special overloads of \c operator<< for it since the stream will by default reuse the operators for <tt>std::basic_ostream</tt>.
* However, one can define special overloads of \c operator<< for <tt>basic_formatting_ostream</tt> if a certain type needs
* special formatting when output to log.
如果您只重载 operator<<(std::ostream)
,它将适用于每个流输出,包括到 basic_formatting_ostream
的输出。如果你只重载 operator<<(basic_formatting_ostream)
它只会输出到那种类型的流。
但是,您可能希望重载两者,例如,如果您希望向日志提供不同的或更多的信息(例如对象的地址)。