C++ 流重定向输出到调试

#include 
#include 
#include 
#include 
//using namespace std;

template < class CharT, class TraintT = std::char_traits > 
class basic_debugbuf : public std::basic_stringbuf 
{
    ~basic_debugbuf()
    {
        sync();
    }

protected:
    int sync()
    {
        WriteDebugString(str().c_str());
        str(std::basic_string());    

        return 0;
    }

    void WriteDebugString(const CharT * msg)  {}
};

template <>
void basic_debugbuf::WriteDebugString(const char *msg)
{
    ::OutputDebugStringA(msg);
}

template<>
void basic_debugbuf::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 ()) {}
    ~basic_debugostream() { delete rdbuf(); }
};

typedef basic_debugostream DebugStream;
typedef basic_debugostream WDebugStream;

你可能感兴趣的:(自斧之楥)