A More Involved Example |
Top Previous Next |
Here's a more involved .cpp program that demonstates the logging commands. You will find this demo project in the DebugLibrary source code directory, along with project files for ms .net2003,vc6, and dev-c++.
//--------------------------------------------------------------------------- // JrDebug demo that demonstrates various features //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- // System includes #include <iostream> #include <stdlib.h> #include <assert.h> #include <exception> #include <stdexcept> //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- // Include the Debugging Library files // Note that only our main .cpp needs to include the jrdebug_main.cpp file // alternatively, you could add this cpp to your project or makefile. #include "../../jrdebug_main.cpp" //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- int main(int argc, char *argv[]) { // main function
// automatically parse commandline options and exit if appropriate if (JrDebugParseCommandlineArgc("Demo3",&argc,argv,"-db")) return 0;
// you want very brief human readable output? (this does not display well in gui tool!) // JrDebug::SetBriefOutput(true);
// if you like you can manually turn on file writing of debug statements // JrDebug::WriteToFilename("demologoutput.log",false);
// for fun we let the tool announce itself JrDebugAnnounce("Test Application",true,argc,argv);
// simple loop for (int count=0;count<10;++count) { dbprintf("Performing iteration #%d",count); // an assert that passes and won't be displayed assert(25<100); }
// turn on display of even asserts that pass and do a passing assert; pass more than 3 args to cause assert failure JrDebugSetDisplayPassingAsserts(true); assert(argc<4);
// here are two more complicated debug statements (first one just specifies type and severity level; second specifies also a subtype and the current 'activity') dbprintf(JrdWarning,25,"This is a debug warning statement."); dbprintf(JrdNote,"useless","testing",10,"This is another debug statement.");
// here is another loop but with activity push/pop - this will report timings of activities JrDebugActivityPush("Looping"); int count3,count4,count5; for (int count2=0;count2<10;++count2) { JrDebugActivityPush("iterate"); // take some time for (count3=0;count3<3000;++count3) { // now here is a manual push pop, and further more, we use a * in front of description to avoid display (only used if other messages displayed inside) JrDebugActivityPush("*innerloop"); for (count4=0;count4<3000;++count4) { // just eat up some cpu cycles count5=count3-count2; count5++; count5--; count5=count3-count2; } JrDebugActivityPop("*innerloop"); } JrDebugActivityPop("iterate"); } JrDebugActivityPop("Looping");
// the JrdAlarm type always triggers a windows messagebox if compiled on windows // dbprintf(JrdAlarm,1,"This is an alarm.");
// want to try some stream style output? debugout << "here is stream debug mesg (argc="<<argc<<")."; debugout(JrdTest,"stream","testing",100) << "here is another more specific stream debug message";
dbprintf("Program has finished, now testing an exception.");
// how about a debuglogged exception // JrDebug::EnableExceptionLogging(); // // // for fun we can also turn on an alarm filter to generate an additional pop up messagebox for severity levels >=10 // // // JrDebug::SetAlarmFilterLevel(5); // // the debuglogged repalcement for: throw exception(); try { JrDebugThrow(std::exception(),"testing an exception throw"); } catch (...) { }
// new test to verify we dont get double printf-style arge replacements std::string troublestring ="C:\\Mydrive%d%s."; dbprintf("Here is my troublesomestring '%s'.",troublestring.c_str());
// exit std::cout << "Hit 'Enter' key to exit."<<std::endl; std::cin.get(); return 0; } //---------------------------------------------------------------------------
|