1//
2// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7// Official repository: https://github.com/boostorg/beast
8//
9
10#ifndef BOOST_BEAST_UNIT_TEST_DSTREAM_HPP
11#define BOOST_BEAST_UNIT_TEST_DSTREAM_HPP
12
13#include <boost/config.hpp>
14#include <ios>
15#include <memory>
16#include <ostream>
17#include <sstream>
18#include <streambuf>
19#include <string>
20
21#ifdef BOOST_WINDOWS
22#include <boost/winapi/basic_types.hpp>
23#include <boost/winapi/debugapi.hpp>
24#endif
25
26namespace boost {
27namespace beast {
28namespace unit_test {
29
30#ifdef BOOST_WINDOWS
31
32namespace detail {
33
34template<class CharT, class Traits, class Allocator>
35class dstream_buf
36 : public std::basic_stringbuf<CharT, Traits, Allocator>
37{
38 using ostream = std::basic_ostream<CharT, Traits>;
39
40 ostream& os_;
41 bool dbg_;
42
43 template<class T>
44 void write(T const*) = delete;
45
46 void write(char const* s)
47 {
48 if(dbg_)
49 boost::winapi::OutputDebugStringA(s);
50 os_ << s;
51 }
52
53 void write(wchar_t const* s)
54 {
55 if(dbg_)
56 boost::winapi::OutputDebugStringW(s);
57 os_ << s;
58 }
59
60public:
61 explicit
62 dstream_buf(ostream& os)
63 : os_(os)
64 , dbg_(boost::winapi::IsDebuggerPresent() != 0)
65 {
66 }
67
68 ~dstream_buf()
69 {
70 sync();
71 }
72
73 int
74 sync() override
75 {
76 write(this->str().c_str());
77 this->str("");
78 return 0;
79 }
80};
81
82} // detail
83
84/** std::ostream with Visual Studio IDE redirection.
85
86 Instances of this stream wrap a specified `std::ostream`
87 (such as `std::cout` or `std::cerr`). If the IDE debugger
88 is attached when the stream is created, output will be
89 additionally copied to the Visual Studio Output window.
90*/
91template<
92 class CharT,
93 class Traits = std::char_traits<CharT>,
94 class Allocator = std::allocator<CharT>
95>
96class basic_dstream
97 : public std::basic_ostream<CharT, Traits>
98{
99 detail::dstream_buf<
100 CharT, Traits, Allocator> buf_;
101
102public:
103 /** Construct a stream.
104
105 @param os The output stream to wrap.
106 */
107 explicit
108 basic_dstream(std::ostream& os)
109 : std::basic_ostream<CharT, Traits>(&buf_)
110 , buf_(os)
111 {
112 if(os.flags() & std::ios::unitbuf)
113 std::unitbuf(*this);
114 }
115};
116
117using dstream = basic_dstream<char>;
118using dwstream = basic_dstream<wchar_t>;
119
120#else
121
122using dstream = std::ostream&;
123using dwstream = std::wostream&;
124
125#endif
126
127} // unit_test
128} // beast
129} // boost
130
131#endif
132

source code of boost/libs/beast/include/boost/beast/_experimental/unit_test/dstream.hpp