From f8bb91862e9fb9a8eeb3d16e72812ad52095ab46 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Fri, 2 Sep 2016 13:54:27 -0400 Subject: [PATCH] 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 --- extras/beast/unit_test/dstream.hpp | 96 +++++++++++++----------------- extras/beast/unit_test/main.cpp | 3 +- 2 files changed, 44 insertions(+), 55 deletions(-) diff --git a/extras/beast/unit_test/dstream.hpp b/extras/beast/unit_test/dstream.hpp index 2bd8e590..b3d26ca5 100644 --- a/extras/beast/unit_test/dstream.hpp +++ b/extras/beast/unit_test/dstream.hpp @@ -8,11 +8,10 @@ #ifndef BEAST_UNIT_TEST_DSTREAM_HPP #define BEAST_UNIT_TEST_DSTREAM_HPP -#include -#include +#include #include #include -#include +#include #include #ifdef _MSC_VER @@ -30,15 +29,18 @@ namespace beast { namespace unit_test { -namespace detail { - #ifdef _MSC_VER +namespace detail { + template class dstream_buf : public std::basic_stringbuf { + using ostream = std::basic_ostream; + bool dbg_; + ostream& os_; template 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 dstream_buf - : public std::basic_stringbuf -{ - template - 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, class Allocator = std::allocator > class basic_dstream - : private boost::base_from_member< - detail::dstream_buf> - , public std::basic_ostream + : public std::basic_ostream { + detail::dstream_buf< + CharT, Traits, Allocator> buf_; + public: - basic_dstream() - : std::basic_ostream(&this->member) + /** Construct a stream. + + @param os The output stream to wrap. + */ + explicit + basic_dstream(std::ostream& os) + : std::basic_ostream(&buf_) + , buf_(os) { + if(os.flags() && std::ios::unitbuf) + std::unitbuf(*this); } }; using dstream = basic_dstream; using dwstream = basic_dstream; -} // test +#else + +using dstream = std::ostream&; +using dwstream = std::wostream&; + +#endif + +} // unit_test } // beast #endif diff --git a/extras/beast/unit_test/main.cpp b/extras/beast/unit_test/main.cpp index ffa3b289..1da1de32 100644 --- a/extras/beast/unit_test/main.cpp +++ b/extras/beast/unit_test/main.cpp @@ -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")) {