-
Notifications
You must be signed in to change notification settings - Fork 10
/
debug_utils.h
92 lines (73 loc) · 2.4 KB
/
debug_utils.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
/*
minqlbot - A Quake Live server administrator bot.
Copyright (C) 2015 Mino <mino@minomino.org>
This file is part of minqlbot.
minqlbot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
minqlbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with minqlbot. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <ostream>
#include <sstream>
#include <string>
#include <Windows.h>
// Code based on http://www.codeproject.com/Articles/1053/Using-an-output-stream-for-debugging?msg=2841638#xx2841638xx
namespace debug_utils {
template <class CharT, class TraitsT = std::char_traits<CharT> >
class basic_debugbuf :
public std::basic_stringbuf<CharT, TraitsT>
{
public:
virtual ~basic_debugbuf() {
sync();
}
protected:
int sync() {
output_debug_string(str().c_str());
str(std::basic_string<CharT>()); // Clear the string buffer
return 0;
}
void output_debug_string(const CharT *text) { }
};
template<class CharT, class TraitsT = std::char_traits<CharT> >
class basic_dostream :
public std::basic_ostream<CharT, TraitsT>
{
public:
basic_dostream() : std::basic_ostream<CharT, TraitsT>
(new basic_debugbuf<CharT, TraitsT>()) { }
~basic_dostream() {
delete rdbuf();
}
};
template<>
void basic_debugbuf<char>::output_debug_string(const char *text) {
::OutputDebugStringA(text);
}
template<>
void basic_debugbuf<wchar_t>::output_debug_string(const wchar_t *text) {
::OutputDebugStringW(text);
}
typedef basic_dostream<char> dostream;
typedef basic_dostream<wchar_t> wdostream;
} // Namespace debug_utils
// Global debug stream.
#define DBG_PREFIX "MINQLBOT: "
#ifdef UNICODE
extern debug_utils::wdostream dbg_stream;
#define DOUT dbg_stream << DBG_PREFIX
#define DERR dbg_stream << DBG_PREFIX << "ERROR @ " << __FUNCTION__ << ": "
#define DOUT_NP dbg_stream
#else
extern debug_utils::dostream dbg_stream;
#define DOUT dbg_stream << DBG_PREFIX
#define DERR dbg_stream << DBG_PREFIX << "ERROR @ " << __FUNCTION__ << ": "
#define DOUT_NP dbg_stream
#endif