Solved

Capture debug-output to file of printf/cout statements in VC++

Posted on 2001-08-03
19
1,588 Views
Last Modified: 2013-12-14
Hi,

How can I capture the output of cout and printf statements of a console program when run as a debugged process in MS Visual C++? Usually from the command prompt I'd use the pipeline symbol for this. How can I accomplish the same when I choose Build->Execute, or Build->Debug or F5 and the like?
It doesn't work to set the program arguments with a pipeline symbol...

Any help appreciated

Rgds,
12Bsure corp.
0
Comment
Question by:12bsure
  • 8
  • 7
  • 3
  • +1
19 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
Comment Utility
You can use the 'pipe' trick fro DevStudio also by setting 'Executable for debug session' under 'Project Settings'|'Debug' to

'command.com /c yourexe.exe | ...'

If you want to do that programmatically (at least for 'printf()' and friends) by using 'freopen()':

/* FREOPEN.C: This program reassigns stderr to the file
 * named FREOPEN.OUT and writes a line to that file.
 */

#include <stdio.h>
#include <stdlib.h>

FILE *stream;

void main( void )
{
   /* Reassign "stderr" to "freopen.out": */
   stream = freopen( "freopen.out", "w", stderr );

   if( stream == NULL )
      fprintf( stdout, "error on freopen\n" );
   else
   {
      fprintf( stream, "This will go to the file 'freopen.out'\n" );
      fprintf( stdout, "successfully reassigned\n" );
      fclose( stream );
   }
   system( "type freopen.out" );
}


0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Depending on your needs you might consider a completely different approach.  You can use the windows API function OutputDebugString() to output to the debugger.  In VC it outputs to the debug window.
0
 

Author Comment

by:12bsure
Comment Utility
I like the freopen approach, though this project is too dependent on stdout that I cannot use it.

I actually hoped for something like the 'command.com /c yourexe.exe | ...' approach. I thought that in that way, VC would debug command.com instead of yourexe.exe.

I am trying a few possibilities now, but I haven't yet succeeded. Here is what my command in the "Executable for debug session" looks like:
cmd /c D:\MyDebgPath\MyProject.exe  -f D:\OtherPath\config.cfg | D:\OtherPath\tee.exe D:\LogPath\mylogfile.log

Here, tee.exe is the little program that writes the logfile and prints to the screen.
0
 

Author Comment

by:12bsure
Comment Utility
Forgot to tell you what went wrong: When I hit F5 the IDE asks for a process to debug...
0
 
LVL 2

Expert Comment

by:SamratAshok
Comment Utility
There used to be system API called "popen" which basically would open a application
and return you piped pointer to its standard input and standard output.

I had used it successfully on HP UX earlier, but my last attempt on Win 95 (First Release)
failed miserably. I had neither the need nor requirement to use it since.

Check out the documentation and function
meanwhile I will find to see if I can find some working sample
from my archives.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> I actually hoped for something like
>> the 'command.com /c yourexe.exe | ...'
>> approach. I thought that
>> in that way, VC would debug command.com
>> instead of yourexe.exe.
You should be able to do that.  In the project settings under the debug tab, make command.cpm the executable for debug session.  place a path to you exe and the pipe in the program arguments.

That should work.  However, the debuger will try to debu comamnd.com, not your app, so you can't set breakpoints in your app etc etc etc.  But in terms of redirecting output, that will work.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>so you can't set breakpoints in your app

This can be remedied by placing a '__asm{ int3};' at the beginning of the code; the debugger will stop there and allow the setting of  breakpoints.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
One other thing:

>>I like the freopen approach, though this project is too
>>dependent on stdout that I cannot use it.

So, why not

#include <stdio.h>

#define printf _MyPrintf // no args, pure text replacement!

int _MyPrintf( const char *format , ... )
{
        int nRet;
     static     char     s_acOutput          [     BUFSIZE     +     MAX_PATH];
     static     char     s_acBuf               [     UFSIZE];

     va_list               args;

     va_start     (     args,     pszFormat);

     wvsprintf     (     s_acBuf,     pszFormat,     args);

     wsprintf     (     s_acOutput,
                         "%s%s",
                         s_acBuf,
                         strchr     (     s_acBuf,     '\n')     ?     "\0"     :     "\r\n"
                    );

     OutputDebugString     (     s_acOutput);
     n Ret = printf ( "%s", s_acOutput);
     fprintf ( pFile, "%s", s_acOutput);

     va_end     (     args);

       return ( nRet);
}




0
 

Author Comment

by:12bsure
Comment Utility
The way I look at it now is the following:

First option:
Manually set debug points each time you debug your app (jkr). Seems tedious in large applications.

Second option
Debug command.com (actually I use cmd.exe from Win2k) (nietod)

Third option
Create my own printf function and use it wisely. I'll have to make two different versions for debugging and for release in case of output strings. (jkr)


Clarifying the problem:
The release version will have a trace option by using "| tee.exe mylog.log". The debug version must have the same functionality, writing to the log, and *must* do that through tee.exe. I said I like the third option, but in my case, I cannot alter the requirements for this project. I can't even alter the command line arguments of it! (you might argue: why don't you just give the filename as a commandline argument).

The log-file is in a single run several Mb big in size. I want to be both able to view the log later on *and* debug like I'm used to with setting breakpoints in the IDE *and* having a log-file when run with the pipeline symbol on the command line by our customers *and* (of course :) with the least amount of effort.

But if the IDE does not support capturing output, I'll think of a way of doing that using the third option. With some compiler directives I should be able to make something nice of it.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
If you go with the third option, I strongly recommend you don't use a printf() style approach.  The point is to catch errors varaible argument functions are bad at catching errors in parameters passed to them!   Instead take a more C++ approach and create a C++ stream object that outputs the debug information using operator <<.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>I strongly recommend you don't use a printf() style
>>approach

I wouldn't have recommended it either, but if there's already a lot of existing 'printf()' output, I'd rather do it that way than rewriting half of the app...
0
 

Author Comment

by:12bsure
Comment Utility
I more a less thought of cout instead of printf. Although I talk about printf/cout and the like in my comments, most projects I do (though it depends on the coding team and the styles of the individuals) use the cout approach. I usually find it easier to use.

Reading back this discussion it appears to me that there's no way in debugging a process from the IDE *and* catch its stdout output. Probably unless I use OutputDebugString and the DebugView tool from Russinovich.

Unless anyone can prove otherwise (and I still hope so!), I hereby declare the points for jkr for his fast and most thoughtful solution ;-)  (execution of verdict in a day or so)

Rgds
12Bsure
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>Probably unless I use OutputDebugString and the
>>DebugView tool from Russinovich.

That's actually my favourite tool for that purpose...
0
 

Author Comment

by:12bsure
Comment Utility
I guess nothing more will come up here. Let's conclude that VC cannot capture debug output to a file easily or automatically.

Thanks for all your efforts!
0
 

Author Comment

by:12bsure
Comment Utility
PS, the excellent is of course for the effort and because it's a good solution on its own. Also, it answers the question correctly, there just doesn't seem to be a better way :(

Thanks, jkr.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>Let's conclude that VC cannot capture debug output to a
>>file easily or automatically.

Well, that's not a VC issue - if you want to invest some time, everything you want to do is achievable, but if you're about to change an existing project with minimal impact, there are of course limitations...
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Thsnx, 12bsure!
0
 

Author Comment

by:12bsure
Comment Utility
> if you're about to change an existing project with minimal impact, there are of course limitations...

I agree. But that's not my goal. I'd like to capture any console output for any program when the program is run from within VC as a debug, and probably also as a release built. For that matter, it is a VC-problem, and I doubt if an add-in or something the like could achieve that kind of functionality.
0
 

Author Comment

by:12bsure
Comment Utility
btw, quite long ago I've seen threads on EE on the subject of capturing the text in a console window. It appeared not to be an easy task. Maybe things have changed in Win2k (my usual dev. platform), but I haven't looked into it yet.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now