Last active
June 23, 2022 05:15
-
-
Save takashyx/937f3a794ad36cd98ec3 to your computer and use it in GitHub Desktop.
Redirect std::cout to Visual Studio Debug Output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <Windows.h> | |
class debugStreambuf : public std::streambuf { | |
public: | |
virtual int_type overflow(int_type c = EOF) { | |
if (c != EOF) { | |
TCHAR buf[] = { c, '\0' }; | |
OutputDebugString(buf); | |
} | |
return c; | |
} | |
}; | |
class Cout2VisualStudioDebugOutput { | |
debugStreambuf dbgstream; | |
std::streambuf *default_stream; | |
public: | |
Cout2VisualStudioDebugOutput() { | |
default_stream = std::cout.rdbuf(&dbgstream); | |
} | |
~Cout2VisualStudioDebugOutput() { | |
std::cout.rdbuf(default_stream); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "Cout2VisualStudioDebugOutput.h" | |
main () { | |
Cout2VisualStudioDebugOutput c2v; | |
cout << "Hello Debug Output!" << endl; | |
return; | |
} |
Thanks for sharing. This allows tons of lines of cout debug messages to show up in the VS debug window without re-writing them all.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will not work proper in unicode settings