Skip to content

Instantly share code, notes, and snippets.

@takashyx
Last active June 23, 2022 05:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save takashyx/937f3a794ad36cd98ec3 to your computer and use it in GitHub Desktop.
Save takashyx/937f3a794ad36cd98ec3 to your computer and use it in GitHub Desktop.
Redirect std::cout to Visual Studio Debug Output
#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);
}
};
#include <iostream>
#include "Cout2VisualStudioDebugOutput.h"
main () {
Cout2VisualStudioDebugOutput c2v;
cout << "Hello Debug Output!" << endl;
return;
}
@serup
Copy link

serup commented Aug 31, 2018

This will not work proper in unicode settings

@cuisinart
Copy link

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