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
|
#ifndef _DEBUGSTREAM_H
#define _DEBUGSTREAM_H
#include <Windows.h>
#include <ostream>
#include <sstream>
#include <string>
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<>
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);
}
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();
}
};
typedef basic_dostream<char> dostream;
typedef basic_dostream<wchar_t> wdostream;
#endif // _DEBUGSTREAM_H
|