Write debug output to console window

Environment: any MSVC

It's very common that we have to test our program on another machine where the Visual C++ IDE is not installed. So there will be no debug window for output. Logging into a file or keep popping up MessageBoxs are not very practical ways. An easy solution is to show the output strings in a console window.

Put the following code at the beginning of your app file or in a separate file.


#ifdef _DEBUG
    FILE* __fStdOut = NULL;
    HANDLE __hStdOut = NULL;
#endif

// width and height is the size of console window, if you specify fname, 
// the output will also be writton to this file. The file pointer is automatically closed 
// when you close the app
void startConsoleWin(int width=80, int height=25, char* fname = NULL);

void startConsoleWin(int width, int height, char* fname)
{
#ifdef _DEBUG
	AllocConsole();
	SetConsoleTitle("Debug Window");
	__hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

	COORD co = {width,height};
	SetConsoleScreenBufferSize(__hStdOut, co);

	if(fname)
		__fStdOut = fopen(fname, "w");
#endif
}

// Use wprintf like TRACE0, TRACE1, ... (The arguments are the same as printf)
int wprintf(char *fmt, ...)
{
#ifdef _DEBUG
	char s[300];
	va_list argptr;
	int cnt;

	va_start(argptr, fmt);
	cnt = vsprintf(s, fmt, argptr);
	va_end(argptr);

	DWORD cCharsWritten;

	if(__hStdOut)
		WriteConsole(__hStdOut, s, strlen(s), &cCharsWritten, NULL);

	if(__fStdOut)
		fprintf(__fStdOut, s);

	return(cnt);
#else
	return 0;
#endif
}

In CWinApp::InitInstance() call startConsoleWin() and a console window will popup. Then use wprintf at places where you used to use TRACE. The output strings will then appear in the console window.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • It's no secret what keeps CIOs up at night. Mobile, cloud, data, security, and social have become the "five imperatives," the drivers of business progress, innovation, and competitive differentiation. Business leaders around the world want to hear how other companies are succeeding. How are they applying the latest technologies? How did they get started? What outcomes are they achieving? Read this online magazine for success stories from organizations like the NBA, Pfizer, and San Jose State University as they …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds