mirror of
https://github.com/boostorg/beast.git
synced 2025-08-02 14:24:31 +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
|
#ifndef BEAST_UNIT_TEST_DSTREAM_HPP
|
||||||
#define BEAST_UNIT_TEST_DSTREAM_HPP
|
#define BEAST_UNIT_TEST_DSTREAM_HPP
|
||||||
|
|
||||||
#include <boost/utility/base_from_member.hpp>
|
#include <ios>
|
||||||
#include <iostream>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <sstream>
|
#include <streambuf>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
@@ -30,15 +29,18 @@
|
|||||||
namespace beast {
|
namespace beast {
|
||||||
namespace unit_test {
|
namespace unit_test {
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
template<class CharT, class Traits, class Allocator>
|
template<class CharT, class Traits, class Allocator>
|
||||||
class dstream_buf
|
class dstream_buf
|
||||||
: public std::basic_stringbuf<CharT, Traits, Allocator>
|
: public std::basic_stringbuf<CharT, Traits, Allocator>
|
||||||
{
|
{
|
||||||
|
using ostream = std::basic_ostream<CharT, Traits>;
|
||||||
|
|
||||||
bool dbg_;
|
bool dbg_;
|
||||||
|
ostream& os_;
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
void write(T const*) = delete;
|
void write(T const*) = delete;
|
||||||
@@ -47,21 +49,21 @@ class dstream_buf
|
|||||||
{
|
{
|
||||||
if(dbg_)
|
if(dbg_)
|
||||||
OutputDebugStringA(s);
|
OutputDebugStringA(s);
|
||||||
else
|
os_ << s;
|
||||||
std::cout << s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(wchar_t const* s)
|
void write(wchar_t const* s)
|
||||||
{
|
{
|
||||||
if(dbg_)
|
if(dbg_)
|
||||||
OutputDebugStringW(s);
|
OutputDebugStringW(s);
|
||||||
else
|
os_ << s;
|
||||||
std::wcout << s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
dstream_buf()
|
explicit
|
||||||
: dbg_(IsDebuggerPresent() != FALSE)
|
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
|
} // 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<
|
template<
|
||||||
class CharT,
|
class CharT,
|
||||||
class Traits = std::char_traits<CharT>,
|
class Traits = std::char_traits<CharT>,
|
||||||
class Allocator = std::allocator<CharT>
|
class Allocator = std::allocator<CharT>
|
||||||
>
|
>
|
||||||
class basic_dstream
|
class basic_dstream
|
||||||
: private boost::base_from_member<
|
: public std::basic_ostream<CharT, Traits>
|
||||||
detail::dstream_buf<CharT, Traits, Allocator>>
|
|
||||||
, public std::basic_ostream<CharT, Traits>
|
|
||||||
{
|
{
|
||||||
|
detail::dstream_buf<
|
||||||
|
CharT, Traits, Allocator> buf_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
basic_dstream()
|
/** Construct a stream.
|
||||||
: std::basic_ostream<CharT, Traits>(&this->member)
|
|
||||||
|
@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 dstream = basic_dstream<char>;
|
||||||
using dwstream = basic_dstream<wchar_t>;
|
using dwstream = basic_dstream<wchar_t>;
|
||||||
|
|
||||||
} // test
|
#else
|
||||||
|
|
||||||
|
using dstream = std::ostream&;
|
||||||
|
using dwstream = std::wostream&;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} // unit_test
|
||||||
} // beast
|
} // beast
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -99,7 +99,8 @@ int main(int ac, char const* av[])
|
|||||||
po::store(po::parse_command_line(ac, av, desc), vm);
|
po::store(po::parse_command_line(ac, av, desc), vm);
|
||||||
po::notify(vm);
|
po::notify(vm);
|
||||||
|
|
||||||
dstream log;
|
dstream log{std::cerr};
|
||||||
|
std::unitbuf(log);
|
||||||
|
|
||||||
if(vm.count("help"))
|
if(vm.count("help"))
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user