Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CConsole - A Simple Console for Debug Output

0.00/5 (No votes)
19 Aug 2002 4  
Show debug information in an easy to handle console

Sample Image - debugcon.gif

Introduction

This is my first article on CodeProject. Probably the next time, I will know what to do better if this article is poor ;-) It's not an advanced article, but I hope some people will find it useful anyway. If you find any mistakes or know ways to improve my coding style, then let me know! There's still a lot I need to learn, and the best way is to do it on CodeProject.com.

Overview

A few days ago, I was searching for a way to provide simple debug information in my Release builds, so that people who download my program have the ability to watch the debug output as well and send me the logs if something's going wrong in my program. There are quite a lot of ways to achieve this. One would be to create a nonmodal window with a listview control placed on it to write the debug information to. But I found some very interesting API calls which deal with consoles. That's why I wrote my own simple wrapper class for some console functions like AllocConsole, WriteConsole and so on. My main goal was to keep the wrapper class as simple as possible, so that everybody could easily use this class in his/her applications.

The CConsole Class

The CConsole wrapper class currently consists of 11 functions:

bool   Create(const char* szTitle, bool bNoClose = false);
void   Color(WORD wColor = NULL);
void   Output(const char* szOutput = NULL, ...);
void   SetTitle(const char* szTitle);
char*  GetTitle();
HWND   GetHWND();
HANDLE GetHandle();
void   ShowConsole(bool bShow = true);
void   DisableClose();
void   Clear();
void   Close();

I'm going to explain them in detail now:

bool   Create(const char* szTitle = NULL, bool bNoClose = false);

This function will create the console and display it on the screen. Parameters:

  • const char* szTitle = NULL - The title for the console window, can be set to NULL
  • bool bNoClose = false - If set to true, the [x] button of the console window will be disabled so that it is no longer possible to close the console and thus terminate the whole application. You may disable the x-button at any time by calling DisableClose()
  • Return Value: true if successful, false otherwise
void   Color(WORD wColor = NULL);

This function changes the color of the output text and console background. Parameters:

  • WORD wColor = NULL - Allows any combination of FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_BLUE, FOREGROUND_INTENSITY and BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE. For more information, see SetConsoleTextAttributes() in the MSDN library.
    If left blank, the colors will be reset to the defaults (white text on black background).
void   Output(const char* szOutput = NULL, ...);

This function writes output to the console. You may enter a simple string or even a formatted string as known from printf(). (e.g. con.Output("This is some %s with number %i", string, 14) will result in This is some string with number 14. NOTE that you must put a newline \n character at the end of the string if you want to proceed with a new line. Alternatively, you can call Output() with the parameter left blank. This will write a new line as well.

void   SetTitle(const char* szTitle);

You may set the title of the console at any time like that: SetTitle("New Title");

char*  GetTitle();
HWND   GetHWND();
HANDLE GetHandle();

These functions allow you retrieve some properties of the console like its title, window handle or the console handle.

void   Show(bool bShow = true);

Call this function if you want to show/hide the console. Set bShow to true to show, false to hide the console.
NOTE: This does NOT delete the console. If you want to delete the console, call Close().

void   DisableClose();

This will disable the [x] button of the console, so that you can't close it (and thus closing the whole application).

void   Clear();

Use this function if you want to clear the debug output. Note that there is no chance to bring back the previous output if you call this function!

void   Close();

This function will destroy the console. All output will be gone and calling any other functions from CConsole will not be possible unless you set up a new console with CConsole::Create().

How to Use It

Add a member variable CConsole m_Console or something like that to your main application.
Next, Create() the console. For example, this can be done in the OnInitDialog() handler in MFC apps or any other initialization code:

BOOL CConsoleDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	(...)

	// Create a console with title "Debug output window" and a disabled close button
	if(!m_Console.Create("Debug output window", true))
	    // insert code for the case the console could not be created

	return TRUE;
}

Once the console is set up, you may call the other functions:

// some button is pressed
void CConsoleDlg::OnButtonTest()
{
	// set the color of the output to an intense red font and blue background
	// this is really not advisable ;-)
	m_Console.Color(FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE);

	// write some output to the console
	// without '\n' at the end of the string this will NOT write a new line
	m_Console.Output("Test string %s number %i",szSomeString, count");

	// insert new line
	m_Console.Output();

	// set color to default:
	m_Console.Color();

	// write output WITH new line
	m_Console.Output("Line2\n");

	// set title of console
	m_Console.SetTitle("New Title");

	// hide the console
	m_Console.ShowConsole(false);

	// and so on...
}

Don't forget to close your console with m_Console.Close() when your app is about to terminate.

About the Code

This class is completely written by myself, EXCEPT the code for clearing the console. This was taken from the Microsoft Knowledge base. You can find the article here. The class should work both in MFC and non-MFC applications. Feel free to do what you want with this code, modify it, extend it to your needs. But do not remove my name from the top of the source files.

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.

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