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

cout for MFC/Windows/Non Console applications

, 5 Oct 2000
Rate this:
Please Sign up or sign in to vote.
A trace macro which traces to a new console of the Windows application.
<!-- Add the rest of your HTML here -->

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

Share

About the Author

Dhananjay Gune
Architect
United States United States
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 6 Oct 2000
Article Copyright 2000 by Dhananjay Gune
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid