Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2001-08-03
19
Medium Priority
?
1,692 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 200 total points
ID: 6348462
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
ID: 6348535
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
ID: 6348685
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:12bsure
ID: 6348699
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
ID: 6348762
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
ID: 6348806
>> 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
ID: 6348833
>>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
ID: 6348859
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
ID: 6349428
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
 
LVL 22

Expert Comment

by:nietod
ID: 6349670
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
ID: 6349678
>>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
ID: 6350535
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
ID: 6350709
>>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
ID: 6353955
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
ID: 6353966
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
ID: 6353968
>>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
ID: 6353969
Thsnx, 12bsure!
0
 

Author Comment

by:12bsure
ID: 6353982
> 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
ID: 6353984
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month12 days, 14 hours left to enroll

971 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