Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC

cout for MFC/Windows/Non Console Applications

Rate me:
Please Sign up or sign in to vote.
3.08/5 (10 votes)
5 Oct 2000 72.4K   780   18   5
A trace macro which traces to a new console of the Windows application.

Introduction

Typically, when an MFC program is being debugged, macros like TRACE or variants thereof are used. These traces appear in the output window of the debugger. To see the traces, you have to be debugging (i.e., F5 in MSDEV). If you are executing it, i.e., Ctrl-F5, you cannot see those traces. If you still have to trace it, you probably use message boxes and that's irritating. This tracer lets you create a console window for your windows application and traces everything on that window, hence the name cout. Moreover, when you trace, you probably put some text about what you are testing. For example:

C++
char szName[] = "Dhananjay Gune";

printf("Name = %s\n", szName);

And the output would be:

C++
Name = Dhananjay Gune

"cout" has the following features:

  • It's a DEBUG/RELEASE macro
  • It can trace in RELEASE build, too
  • It is a nice shorthand
  • It automatically traces the expression you want to trace
  • This tracer is different
  • It can trace only one expression at a time

For example:

C++
char szName[] = "Dhananjay Gune";
int a = 0, b = 1;

cout(GetCurrentThreadId());
cout(a == b);
cout(szName);
cout("Done");

And the output would be:

C++
GetCurrentThreadId() = 1234
a == b = 0
szName = Dhananjay Gune
Done

For applications which do not have consoles, you have to put the CreateConsole() macro in your main/WinMain/InitInstance/CWinApp constructor. You should also put DeleteConsole() before your program exits. There is no need to do so, however, because the system will FreeConsole() anyway when the program exits.

Another free function that comes along with this is getLastErrorText() which is self explanatory and is partly taken from the MSDN.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Architect Home Depot
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Laurent Cozic17-Jun-12 16:43
Laurent Cozic17-Jun-12 16:43 
Generalit will be better if you use LPCTSTR instand of CString Pin
code_discuss22-Nov-07 19:36
code_discuss22-Nov-07 19:36 
GeneralMaking cout much more general Pin
Adi Shavit28-Dec-04 3:37
Adi Shavit28-Dec-04 3:37 
GeneralVery Cool and a suggection! Pin
Behzad Ebrahimi1-Aug-04 4:54
Behzad Ebrahimi1-Aug-04 4:54 
GeneralThank you Pin
cwilper10-Nov-03 20:57
cwilper10-Nov-03 20:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.