Click here to Skip to main content
11,437,397 members (34,518 online)
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 Pin
Samuel Batista9-Oct-08 13:57
memberSamuel Batista9-Oct-08 13:57 
GeneralClosing Console Pin
berlimbo24-Jan-08 21:04
memberberlimbo24-Jan-08 21:04 
GeneralProblem with Show() Pin
moshezel23-Oct-07 9:42
membermoshezel23-Oct-07 9:42 
Hi,

Very nice class indeed!

I have encountered one small problem: I have created the console, and I wanted to immediately hide it. The problem was that the Create() function returned so fast, that the Show() function (that immediately followed it) got hWnd = NULL, so the console did not disappear...

The way I worked around it was:
1. I have added a boolean to the class: bool m_ConsoleActive;
2. In Create() I have set it to true, and in Close() to false.
3. In Show() I've modified the code to be like this:
void CConsole::Show(bool bShow)
{
CHECK(hConsole);

// get out console's HWND and show/hide the console
HWND hWnd;
if (m_ConsoleActive){
hWnd = GetHWND();
while (!hWnd){
hWnd = GetHWND();
}
if(hWnd != NULL)
ShowWindow(hWnd, bShow ? SW_SHOW : SW_HIDE);
}
}

I know that the console exist, since m_ConsoleActive is true, and now I am polling on GetHWND() until it gives me a valid handle. Now I can safely hide the window.
(On the way I also fixed the bug in the ShowWindow, mentioned above...)

For me - it works. You can use it too.

thx.

Moshe.
GeneralRe: Problem with Show() Pin
moshezel23-Oct-07 9:52
membermoshezel23-Oct-07 9:52 
Generalthere is a big bug: Pin
koda_xii25-May-07 1:29
memberkoda_xii25-May-07 1:29 
GeneralConsole input Pin
drutherf17-May-06 7:07
memberdrutherf17-May-06 7:07 
Generalfunny bug Pin
curious_ambitious16-Dec-05 2:09
membercurious_ambitious16-Dec-05 2:09 
GeneralRe: funny bug Pin
Greeeg19-Dec-05 6:27
memberGreeeg19-Dec-05 6:27 
GeneralNice... but how to save the debug message Pin
xnetec15-Dec-05 4:32
memberxnetec15-Dec-05 4:32 
GeneralRe: Nice... but how to save the debug message Pin
Greeeg19-Dec-05 6:38
memberGreeeg19-Dec-05 6:38 
GeneralNice.. Pin
MoFo2-Mar-04 4:40
memberMoFo2-Mar-04 4:40 
GeneralNice Pin
Mike Klimentiev27-Aug-02 10:46
memberMike Klimentiev27-Aug-02 10:46 
GeneralRe: Nice Pin
Gregor S.29-Aug-02 8:18
memberGregor S.29-Aug-02 8:18 
GeneralRe: Nice Pin
Anonymous8-Nov-02 1:09
sussAnonymous8-Nov-02 1:09 
Generalconsole window & stdio Pin
Paolo Messina22-Aug-02 1:47
memberPaolo Messina22-Aug-02 1:47 
GeneralRe: console window & stdio Pin
Gregor S.22-Aug-02 7:53
memberGregor S.22-Aug-02 7:53 
GeneralRe: console window & stdio Pin
Forogar31-Jan-03 7:38
memberForogar31-Jan-03 7:38 
GeneralRe: console window & stdio Pin
moshezel23-Oct-07 9:33
membermoshezel23-Oct-07 9:33 
GeneralSuggestion Pin
Jon Taylor21-Aug-02 0:28
memberJon Taylor21-Aug-02 0:28 
GeneralRe: Suggestion Pin
Gregor S.21-Aug-02 3:16
memberGregor S.21-Aug-02 3:16 
GeneralRe: Suggestion Pin
Jon Taylor21-Aug-02 22:41
memberJon Taylor21-Aug-02 22:41 
GeneralRe: Suggestion Pin
Gregor S.22-Aug-02 7:59
memberGregor S.22-Aug-02 7:59 
GeneralRe: Suggestion Pin
Jon Taylor29-Aug-02 4:51
memberJon Taylor29-Aug-02 4:51 
GeneralRe: Suggestion Pin
Gregor S.29-Aug-02 8:14
memberGregor S.29-Aug-02 8:14 
GeneralRe: Suggestion Pin
Jaime Olivares18-May-05 6:09
memberJaime Olivares18-May-05 6: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 | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 20 Aug 2002
Article Copyright 2002 by Greeeg
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid