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

CConsole - a simple console for debug output

, 19 Aug 2002
Rate this:
Please Sign up or sign in to vote.
Show debug information in an easy to handle console
<!-- Download Links --> <!-- Article image -->

Sample Image - debugcon.gif

<!-- Add the rest of your HTML here -->

Introduction

At first I want to apologize for my not very good English. Furthermore this is my first article on CodeProject. Probably the next time I will know what to do better if this article is poor Wink | ;-) It's not an advanced article, but I hope some people will find it useful anyway. If you find any mistakes or know ways how 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 ways how 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 terminating the whole application. You may disable the x-button at any time by calling DisableClose()
  • Return Value: true if successfull, 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 functions 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 consle 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 defaut:
	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 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

Share

About the Author

Greeeg

Germany Germany
No Biography provided

Comments and Discussions

 
GeneralDebug Console Use PinmemberSamuel Batista9-Oct-08 12:57 
GeneralClosing Console Pinmemberberlimbo24-Jan-08 20:04 
GeneralProblem with Show() Pinmembermoshezel23-Oct-07 8:42 
GeneralRe: Problem with Show() Pinmembermoshezel23-Oct-07 8:52 
Generalthere is a big bug: Pinmemberkoda_xii25-May-07 0:29 
GeneralConsole input Pinmemberdrutherf17-May-06 6:07 
Generalfunny bug Pinmembercurious_ambitious16-Dec-05 1:09 
GeneralRe: funny bug PinmemberGreeeg19-Dec-05 5:27 
GeneralNice... but how to save the debug message Pinmemberxnetec15-Dec-05 3:32 
GeneralRe: Nice... but how to save the debug message PinmemberGreeeg19-Dec-05 5:38 
GeneralNice.. PinmemberMoFo2-Mar-04 3:40 
GeneralNice PinmemberMike Klimentiev27-Aug-02 9:46 
GeneralRe: Nice PinmemberGregor S.29-Aug-02 7:18 
GeneralRe: Nice PinsussAnonymous8-Nov-02 0:09 
Generalconsole window & stdio PinmemberPaolo Messina22-Aug-02 0:47 
GeneralRe: console window & stdio PinmemberGregor S.22-Aug-02 6:53 
GeneralRe: console window & stdio PinmemberForogar31-Jan-03 6:38 
GeneralRe: console window & stdio Pinmembermoshezel23-Oct-07 8:33 
GeneralSuggestion PinmemberJon Taylor20-Aug-02 23:28 
GeneralRe: Suggestion PinmemberGregor S.21-Aug-02 2:16 
GeneralRe: Suggestion PinmemberJon Taylor21-Aug-02 21:41 
GeneralRe: Suggestion PinmemberGregor S.22-Aug-02 6:59 
GeneralRe: Suggestion PinmemberJon Taylor29-Aug-02 3:51 
GeneralRe: Suggestion PinmemberGregor S.29-Aug-02 7:14 
GeneralRe: Suggestion PinmemberJaime Olivares18-May-05 5:09 

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.140821.2 | Last Updated 20 Aug 2002
Article Copyright 2002 by Greeeg
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid