mirror of
https://github.com/boostorg/beast.git
synced 2025-07-31 21:34:46 +02:00
Better dstream:
* non-Windows dstream is a simple reference alias * dstream constructor takes a target ostream argument * dstream inherits the unitbuf setting of the target stream
This commit is contained in:
@@ -8,11 +8,10 @@
|
||||
#ifndef BEAST_UNIT_TEST_DSTREAM_HPP
|
||||
#define BEAST_UNIT_TEST_DSTREAM_HPP
|
||||
|
||||
#include <boost/utility/base_from_member.hpp>
|
||||
#include <iostream>
|
||||
#include <ios>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <streambuf>
|
||||
#include <string>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
@@ -30,15 +29,18 @@
|
||||
namespace beast {
|
||||
namespace unit_test {
|
||||
|
||||
namespace detail {
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<class CharT, class Traits, class Allocator>
|
||||
class dstream_buf
|
||||
: public std::basic_stringbuf<CharT, Traits, Allocator>
|
||||
{
|
||||
using ostream = std::basic_ostream<CharT, Traits>;
|
||||
|
||||
bool dbg_;
|
||||
ostream& os_;
|
||||
|
||||
template<class T>
|
||||
void write(T const*) = delete;
|
||||
@@ -47,21 +49,21 @@ class dstream_buf
|
||||
{
|
||||
if(dbg_)
|
||||
OutputDebugStringA(s);
|
||||
else
|
||||
std::cout << s;
|
||||
os_ << s;
|
||||
}
|
||||
|
||||
void write(wchar_t const* s)
|
||||
{
|
||||
if(dbg_)
|
||||
OutputDebugStringW(s);
|
||||
else
|
||||
std::wcout << s;
|
||||
os_ << s;
|
||||
}
|
||||
|
||||
public:
|
||||
dstream_buf()
|
||||
: dbg_(IsDebuggerPresent() != FALSE)
|
||||
explicit
|
||||
dstream_buf(ostream& os)
|
||||
: os_(os)
|
||||
, dbg_(IsDebuggerPresent() != FALSE)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -79,66 +81,52 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
template<class CharT, class Traits, class Allocator>
|
||||
class dstream_buf
|
||||
: public std::basic_stringbuf<CharT, Traits, Allocator>
|
||||
{
|
||||
template<class T>
|
||||
void write(T const*) = delete;
|
||||
|
||||
void write(char const* s)
|
||||
{
|
||||
std::cout << s;
|
||||
}
|
||||
|
||||
void write(wchar_t const* s)
|
||||
{
|
||||
std::wcout << s;
|
||||
}
|
||||
|
||||
public:
|
||||
~dstream_buf()
|
||||
{
|
||||
sync();
|
||||
}
|
||||
|
||||
int
|
||||
sync() override
|
||||
{
|
||||
write(this->str().c_str());
|
||||
this->str("");
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
} // detail
|
||||
|
||||
/// A std::ostream that redirects output to the debugger if attached.
|
||||
/** std::ostream with Visual Studio IDE redirection.
|
||||
|
||||
Instances of this stream wrap a specified `std::ostream`
|
||||
(such as `std::cout` or `std::cerr`). If the IDE debugger
|
||||
is attached when the stream is created, output will be
|
||||
additionally copied to the Visual Studio Output window.
|
||||
*/
|
||||
template<
|
||||
class CharT,
|
||||
class Traits = std::char_traits<CharT>,
|
||||
class Allocator = std::allocator<CharT>
|
||||
>
|
||||
class basic_dstream
|
||||
: private boost::base_from_member<
|
||||
detail::dstream_buf<CharT, Traits, Allocator>>
|
||||
, public std::basic_ostream<CharT, Traits>
|
||||
: public std::basic_ostream<CharT, Traits>
|
||||
{
|
||||
detail::dstream_buf<
|
||||
CharT, Traits, Allocator> buf_;
|
||||
|
||||
public:
|
||||
basic_dstream()
|
||||
: std::basic_ostream<CharT, Traits>(&this->member)
|
||||
/** Construct a stream.
|
||||
|
||||
@param os The output stream to wrap.
|
||||
*/
|
||||
explicit
|
||||
basic_dstream(std::ostream& os)
|
||||
: std::basic_ostream<CharT, Traits>(&buf_)
|
||||
, buf_(os)
|
||||
{
|
||||
if(os.flags() && std::ios::unitbuf)
|
||||
std::unitbuf(*this);
|
||||
}
|
||||
};
|
||||
|
||||
using dstream = basic_dstream<char>;
|
||||
using dwstream = basic_dstream<wchar_t>;
|
||||
|
||||
} // test
|
||||
#else
|
||||
|
||||
using dstream = std::ostream&;
|
||||
using dwstream = std::wostream&;
|
||||
|
||||
#endif
|
||||
|
||||
} // unit_test
|
||||
} // beast
|
||||
|
||||
#endif
|
||||
|
@@ -99,7 +99,8 @@ int main(int ac, char const* av[])
|
||||
po::store(po::parse_command_line(ac, av, desc), vm);
|
||||
po::notify(vm);
|
||||
|
||||
dstream log;
|
||||
dstream log{std::cerr};
|
||||
std::unitbuf(log);
|
||||
|
||||
if(vm.count("help"))
|
||||
{
|
||||
|
Reference in New Issue
Block a user