Using a Local Callback Monitor

Top  Previous  Next

This example shows how to implement a callback in your code so you get notified of all debug messages that your application sends.  You will find this demo project in the DebugLibrary source code directory, along with project files for ms .net2003,vc6, and dev-c++.

 

//---------------------------------------------------------------------------

// System includes for std::cout usage below

#include <iostream>

//---------------------------------------------------------------------------

 

//---------------------------------------------------------------------------

// Include the Debugging Library file (only our main should include this

//  file, additional .cpp files should include "jrdebug.h"

//  alternatively, you could add this cpp to your project or makefile.

#include "../../jrdebug_main.cpp"

//---------------------------------------------------------------------------

 

//---------------------------------------------------------------------------

// Helper callback object - can be used if user code want to intercept events

//  To use, derive your own version of this class, create an instance of it, and pass it to the static

//  function JrDebug::SetJrDebugCallbackPointer(class JrDebugCallback *p);

// Callback should return true to process as normal, or false to not send output as normal

class MyDebugCatcher : public JrDebugCallback {

public:

 virtual bool EventNotify(const char *fulltext) { std::cout << "Local debug catcher got: "<<fulltext;return true;};

};

//---------------------------------------------------------------------------

 

//---------------------------------------------------------------------------

int main(int argc, char *argv[])

{

 // set up local catcher

 JrDebug::SetBriefOutput(true);

 MyDebugCatcher mycatcher;

 JrDebug::SetCallbackPointer(&mycatcher);

 

 // C++ iostreams style logging:

 debugout << "Main started with argc = "<<argc;

 // printf style logging:

 dbprintf("Main started with argc=%d",argc);

 

 // return success

 std::cout << "Program has finished; if you were running the debug monitor you should have seen 2 messages."<<std::endl;

 return 0;

}

//---------------------------------------------------------------------------