48

It seems rather tedious to output to debug window. Where can I find cout output if I am writing a non-console information ?

Like:

double i = a / b;
cout << b << endl;//I want to check out whether b is zero. It seems the output cannot be found anywhere.
TylerH
  • 20,799
  • 66
  • 75
  • 101
CDT
  • 10,165
  • 18
  • 66
  • 97
  • 1
    Perhals use a logging framework, such as NLog, that allows you to direct log output to any/all of file, console, viewer app, email and many others? – Eric J. May 23 '13 at 00:40
  • I was about to say that, but as alternate suggestions, Poco has one, and I believe Boost does as well. I wouldn't be able to speak from personal experience which are considered best. – chris May 23 '13 at 00:41
  • Take a look at this http://stackoverflow.com/questions/6338812/printing-to-the-console-vs-writing-to-a-file-speed – AliBZ May 23 '13 at 00:46
  • What is your real problem? Is it that you want to see the output of `cout` in a non-console application? Or is it, as indicated in your comment, that you want to see the value of `b`? – Benjamin Lindley May 23 '13 at 00:47
  • @BenjaminLindley Of course it is I want to see the output of cout. I want to see the value of `b` output by `cout`. – CDT May 23 '13 at 01:02
  • You could write a `stream` that calls `OutputDebugStringA`. It's not redirecting `cout`, but I believe it will assist you. See my answer below. – Thomas Matthews May 23 '13 at 01:12
  • Regardless of whether an alternate solution can be found for this specific debugging problem, I think it's an interesting question as asked. I'd love to see a real answer. – Mark Ransom May 23 '13 at 02:10
  • P.S. I believe the answer is here: http://stackoverflow.com/a/428976/5987 – Mark Ransom May 23 '13 at 02:14

8 Answers8

117

The question is very clear. How use std::cout to debug a non-console application in Visual Studio.

The answer is very clear: you cannot. That is, Visual Studio does not support std::cout as debug tool for non-console applications.

This is a serious limitation of Visual Studio, probably a failure to meet the C++ standard even. I find it very sad to see disinformative "answers" here trying to hide this defect of their precious Visual Studio.

Marius Amado-Alves
  • 1,365
  • 2
  • 8
  • 3
  • 1
    While this still is a valid point, for those interested in a convenient workaround: have a look at Trevor Hickeys answer down below. – Marco Kerwitz Aug 01 '15 at 21:45
  • 1
    Please see my answer. I've done it. Actually in the Debug Output window. No separate console window. – Luc Bloom Nov 15 '17 at 16:09
35

For a Windows solution, you can allocate a console, and bind cout/cin to it. For example:

AllocConsole();
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);  

Documentation:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms681944%28v=vs.85%29.aspx

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • 1
    But this opens an extra window. I want the output to be in the Debug Output pane of Visual Studio. – isgoed Jun 12 '19 at 17:54
20

SOLUTION: This answer solves the question and allows you to redirect console output to the Visual Studio Output window. First we need a class that overrides the default cout string stream:

class dbg_stream_for_cout
    : public std::stringbuf
{
public:
    ~dbg_stream_for_cout() { sync(); }
    int sync()
    {
        ::OutputDebugStringA(str().c_str());
        str(std::string()); // Clear the string buffer
        return 0;
    }
};
dbg_stream_for_cout g_DebugStreamFor_cout;

Then, somewhere you want to "activate" writing to the VS output window:

std::cout.rdbuf(&g_DebugStreamFor_cout); // Redirect std::cout to OutputDebugString!
Luc Bloom
  • 1,120
  • 12
  • 18
17

To output a string to the debug console, use OutputDebugStringA. See http://msdn.microsoft.com/en-us/library/windows/desktop/aa363362%28v=vs.85%29.aspx

To output variable values to the debug console, using std::ostringstream, the send the string to OutputDebugStringA.

Excessive output statements will cause the program to severly slow down. However, it is a good technique to catch things the debugger has a problem with, such as the actual child members when playing with base pointers.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • Only thing about this is OutputDebugStringA is a WINAPI. Which is fine if you are creating a Windows Console Application, but if you want pure C++ without Windows (yes, even using Visual Studio 2017!), you can't use this one. – JasonH Jan 16 '18 at 20:58
6

I'd like to give my 2 cents.

Given that the maybe is a VS issue about compliancy with the C++ standard or that we could use OutputDebugStringA, if you cannot modify your code base you may like the idea of simply redirect the std::cout to something else, like a file.

So without changing your code base you can do something like suggested here:How to redirect cin and cout to files?

Condensed:

  • add the include #include <fstream>
  • at the beginning of your app, in some init, before logging you can use:
std::ofstream out("out.txt");
std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
  • the at the end of your app/logging:

std::cout.rdbuf(coutbuf); //reset to standard output again

Hope this may help someone, Kudos to Nawaz that provided the answer in the other thread.

Stefano Buora
  • 1,052
  • 6
  • 12
4

Instead of using cout, create a log file and write anything you want into it.

Edit: Use this simple code for writing to a log file.

ofstream log;
log.open ("log.txt");
log << "Writing this to a file.\n";
log.close();
t. fochtman
  • 431
  • 3
  • 9
AliBZ
  • 4,039
  • 12
  • 45
  • 67
  • 4
    "Where can I find cout output if I am writing a non-console information ?" this doesn't answer the question. – Luchian Grigore May 23 '13 at 00:40
  • 2
    @LuchianGrigore: *"I want to check out whether b is zero."* -- It solves that problem. – Benjamin Lindley May 23 '13 at 00:41
  • @BenjaminLindley if I write the exact same question without the comment, it'd get closed as a duplicate of this, and my question wouldn't have been answered. – Luchian Grigore May 23 '13 at 00:45
  • @LuchianGrigore: Well, this question clearly needs some work. The OP's goal is not clear, so I've asked for clarification. But as it stands, this answer does address the apparent problem the OP is having. And I wouldn't be one of those close voters. – Benjamin Lindley May 23 '13 at 00:51
  • @CDT yes it is meant to work at runtime. The only problem is that the variable changes from `log` to `myfile` halfway through the example - it should be consistent. – Mark Ransom May 23 '13 at 02:08
0

You can use .Net functions such as System::Diagnostics::Debug::WriteLine("your message"). You can even add a condition to print only during the debug mode and not in the release mode. For example:

#ifdef DEBUG   
   System::Diagnostics::Debug::WriteLine("your message");
#endif   
-1

Yes, indeed, it's annoying.

Here's how I do it :

#define DBOUT( s )            \
{                             \
std::wostringstream os_;    \
   os_ << s;                   \
   OutputDebugStringW( os_.str().c_str() );  \
}
// example :    DBOUT("some text " << some_variable << "   some more text" << some_other_varaible << "\n");
  • 2
    Explaining the code is rather more beneficial than pasting just a code snippet. If you could explain your code by commenting or writing a little about it, it'd be great. – shmsr Aug 21 '20 at 19:38