#include <Windows.h>
#include <ostream>
#include <sstream>
#include <string>
//using namespace std;
template < class CharT, class TraintT = std::char_traits<CharT> >
class basic_debugbuf : public std::basic_stringbuf <CharT, TraintT>
{
~basic_debugbuf()
{
sync();
}
protected:
int sync()
{
WriteDebugString(str().c_str());
str(std::basic_string<CharT>());
return 0;
}
void WriteDebugString(const CharT * msg) {}
};
template <>
void basic_debugbuf<char>::WriteDebugString(const char *msg)
{
::OutputDebugStringA(msg);
}
template<>
void basic_debugbuf<wchar_t>::WriteDebugString(const wchar_t *msg)
{
::OutputDebugStringW(msg);
}
template< class CharT, class TraistT = std::char_traits< CharT> >
class basic_debugostream : public std::basic_ostream < CharT, TraistT>
{
public:
basic_debugostream() : std::basic_ostream < CharT, TraistT> (new basic_debugbuf<CharT, TraistT> ()) {}
~basic_debugostream() { delete rdbuf(); }
};
typedef basic_debugostream<char> DebugStream;
typedef basic_debugostream<wchar_t> WDebugStream;C++ 流重定向输出到调试
最新推荐文章于 2026-05-20 06:17:14 发布
本文介绍了一个使用C++实现的调试输出类,该类利用模板技术为字符和宽字符提供调试信息输出功能。通过继承标准字符串缓冲区并重写析构函数与sync方法,在程序结束时将调试信息输出到Windows环境中。
606

被折叠的 条评论
为什么被折叠?



