Click here to Skip to main content
Click here to Skip to main content

cout for MFC/Windows/Non Console applications

By , 5 Oct 2000
 

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 your are testing. For example,

	char szName[] = "Dhananjay Gune";
	
	printf("Name = %s\n", szName);

And the output would be:

	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,

	char szName[] = "Dhananjay Gune";
	int a = 0, b = 1;
	
	cout(GetCurrentThreadId());
	cout(a == b);
	cout(szName);
	cout("Done");

And the output would be:

	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

About the Author

Dhananjay Gune
Architect
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberLaurent Cozic17 Jun '12 - 16:43 
Generalit will be better if you use LPCTSTR instand of CStringmembercode_discuss22 Nov '07 - 19:36 
GeneralMaking cout much more generalmemberAdi Shavit28 Dec '04 - 3:37 
GeneralVery Cool and a suggection!memberBehzad Ebrahimi1 Aug '04 - 4:54 
GeneralThank youmembercwilper10 Nov '03 - 20:57 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 6 Oct 2000
Article Copyright 2000 by Dhananjay Gune
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid