forked from CobaltFusion/DebugViewPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbgstream.h
115 lines (91 loc) · 2.68 KB
/
dbgstream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// (C) Copyright Gert-Jan de Vos and Jan Wilmans 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Repository at: https://github.com/djeedjay/DebugViewPP/
// OutputDebugString as an ostream:
// cdbg << "Hello " << name << std::endl;
// wcdbg << L"Hello " << wname << std::endl;
//
// cnull and wcnull are do-nothing stremas that support macro based logging on/off selection:
//
// #ifdef NDEBUG
// # define CDBG cnull
// # define WCDBG wcnull
// #else
// # define CDBG cdbg
// # define WCDBG wcdbg
// #endif
#ifndef DBGSTREAM_H
#define DBGSTREAM_H
#pragma once
#include <streambuf>
#include <string>
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
namespace dbgstream {
template <class Elem, class Tr = std::char_traits<Elem>, class Alloc = std::allocator<Elem> >
class basic_debugbuf : public std::basic_streambuf<Elem, Tr>
{
protected:
virtual int sync() override
{
output(m_buf.c_str());
m_buf.clear();
return 0;
}
using int_type = std::basic_streambuf<Elem, Tr>::int_type;
virtual int_type overflow(int_type c) override
{
if (c == std::basic_streambuf<Elem, Tr>::traits_type::eof())
return 0;
m_buf += std::basic_streambuf<Elem, Tr>::traits_type::to_char_type(c);
if (c == '\n')
sync();
return c;
}
private:
std::basic_string<Elem, Tr, Alloc> m_buf;
static void output(const char* msg)
{
OutputDebugStringA(msg);
}
static void output(const wchar_t* msg)
{
OutputDebugStringW(msg);
}
};
template <class Elem, class Tr = std::char_traits<Elem> >
class basic_dbgstream : public std::basic_ostream<Elem, Tr>
{
public:
basic_dbgstream() : std::basic_ostream<Elem, Tr>(&buf)
{
}
private:
basic_debugbuf<Elem, Tr> buf;
};
template <class Elem, class Tr = std::char_traits<Elem>, class Alloc = std::allocator<Elem> >
class basic_nullbuf : public std::basic_streambuf<Elem, Tr>
{
};
template <class Elem, class Tr = std::char_traits<Elem> >
class basic_nullstream : public std::basic_ostream<Elem, Tr>
{
public:
basic_nullstream() : std::basic_ostream<Elem, Tr>(&buf)
{
}
private:
basic_nullbuf<Elem, Tr> buf;
};
typedef basic_nullstream<char> nullstream;
typedef basic_nullstream<wchar_t> wnullstream;
typedef basic_dbgstream<char> dbgstream;
typedef basic_dbgstream<wchar_t> wdbgstream;
} // namespace dbgstream
__declspec(selectany) dbgstream::nullstream cnull;
__declspec(selectany) dbgstream::wnullstream wcnull;
__declspec(selectany) dbgstream::dbgstream cdbg;
__declspec(selectany) dbgstream::wdbgstream wcdbg;
#endif // DBGSTREAM_H