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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds