Visual C++ Custom Debug Monitor

Environment: Visual C++ 6 (SP3)

Many times I wanted to have an easy way to get the same functionality in Microsoft Visual C++ that Visual Basic provides, i.e. to insert into the source code some debug messages and have them displayed at runtime into a separated, dedicated window (...the "Immediate" window in Visual Basic).

I have checked the existing solutions/workarounds (like MsgTracer, TraceWindow, ...), but none seemed enough flexible and very easy to use. So, I have decided to implement my own solution.

I have created a small utility, named Debug.exe, which is a mainly a messaging monitor window, where all the debugging messages sent from your application are displayed at runtime. (You can download the full source code here)

In order to use this utility, you need to follow these easy steps:

  1. Add CDebug.cpp and CDebug.h to your Visual C++ project.
  2. Declare a global instance of the CDebug class:
  3. CDebug Debug;
    
  4. Wherever you need in your source code to send (debug) messages, use one of the following functions:
  5. Debug.printf0(/* message */)
    Debug.printf1(/* message */)
    Debug.printf2(/* message */)
    Debug.printf3(/* message */) 
    
    - the last digit (0, 1, 2 or 3) means the level of the message (you can customize what levels you want to intercept in the Debug window).
    - the syntax/arguments are the same as for the printf function... It will also accept a CString as first argument, in addition to LPTSTR and LPCTSTR.
  6. Run Debug.exe and customize, through "Options" how do you want to handle the arriving messages. (One nice idea might be to add the Debug.exe utility to the "Tools" IDE menu)
  7. Run your Visual C++ application.

Example Usage

//YourApp.cpp implementation file

#ifndef CDEBUG
 #include "CDebug.h"
#endif
extern CDebug Debug;

//....your code...

//...A function that uses the Debug facility...
//At runtime, the Debug.exe utility should be running in order
//to catch these messages...
BOOL CWOA_GridCtrl::IsRunMode()
{
 BOOL bIsRunMode;
 bIsRunMode = AmbientUserMode();

 Debug.Printf1("IsRunMode returns ", bIsRunMode);
 return bIsRunMode;
}

Downloads

Download source - 45 Kb


Comments

  • Re:one problem

    Posted by Legacy on 10/08/2003 12:00am

    Originally posted by: Mark

    You can easily wrap his call in a macro that will null out if CDEBUG is not defined so it will not load down a release.

    Mark

    Reply
  • One little Bug

    Posted by Legacy on 10/21/2001 12:00am

    Originally posted by: Feng.Zh

    I have download the program and run it in my computer.

    I have found that the program will be terminated by some error.

    At last,I see that the function OnCopyData(...)
    some codes error.

    // char szHWND[16];
    // sprintf(szHWND, "%x", (LONG) pWnd->m_hWnd);
    // SetDlgItemText(IDC_EDIT_HWND, szHWND);

    because the size of szHWND is small.

    I'm intersted in VC.Would you like to exchange some ideas
    with me?

    Yours.

    Reply
  • one problem

    Posted by Legacy on 04/25/2001 12:00am

    Originally posted by: Yevgeniy Marchenko

    There is one but big disadvantage in the suggested approach. The Debug object and all calling for its monitor function will be in either release and debug versions.

    Sincerely, Yevgeniy.

    Reply
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 …

  • 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