topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday December 13, 2024, 3:40 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: JrDebugLogger - Sample  (Read 7615 times)

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,914
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
JrDebugLogger - Sample
« on: November 14, 2010, 10:46 AM »
Thought it might be useful to paste a complete sample of using JrDebugLogger in code.  I really don't use advanced functions so this is almost exactly what i use:

Code: C++ [Select]
  1. //---------------------------------------------------------------------------
  2. // System includes for std::cout usage below
  3. #include <iostream>
  4. //---------------------------------------------------------------------------
  5.  
  6. //---------------------------------------------------------------------------
  7. // Include the Debugging Library file (only our main should include this
  8. //  file, additional .cpp files should include "jrdebug.h"
  9. //  alternatively, you could add this cpp to your project or makefile.
  10. #include <jrdebug_main.cpp>
  11. //---------------------------------------------------------------------------
  12.  
  13.  
  14. //---------------------------------------------------------------------------
  15. int main(int argc, char *argv[])
  16. {
  17.         // set our own commandline options as one big string, not using actual comline args
  18.         JrDebugParseCommandlineArgString("-dbo text;debugout.txt;info",NULL,NULL);
  19.  
  20.         // a debug logging output line
  21.         debugout << "This is a debugging message (argc="<<argc<<")";
  22.  
  23.         // return success
  24.         std::cout << "Program finished."<<std::endl;
  25.         return 0;
  26. }
  27. //---------------------------------------------------------------------------


So basically here is what we have:

  • First, we include in the the jrdebug code with #include <jrdebug_main.cpp>.  Now actually the cleaner standard way is to add jrdebug_main.cpp to your project file, and then #include <jrdebug.h> in your files.  
  • Second, we have an initialization function for JrDebugLogger, which simulates some commandline options; there are a few helper functions depending on whether argc+argv are how you want to pass in commandline options, if at all.  Don't get scared by this, it's basically a really simple way to both set options and/or let the user set debug options via commandline.  In this case, i'm just setting some options manually and ignoring any user specified commandline options: JrDebugParseCommandlineArgString("-dbo text;debugout.txt;info",NULL,NULL);
  • This option string tells JrDebugLogger to log debug messages in text format, to the file debugout.txt, which will be in application directory if permissions allow, and display extra info about where the file was created.  If you also wanted to display debug messages to the console you could use "-dbo text;debugout.txt;stdout;info" instead.
  • Lastly, we send a debug message using stream syntax: debugout << "This is a debugging message (argc="<<argc<<")"; there is also a printf style debug output function alternative.

The generated debugout.txt file contents look like this:
Sat Nov 13 20:11:31 2010 -> Message: This is a debugging message (argc=1)


Of course there are tons of advanced extra things you can do, but that's the basics.

And again, the absolutely *KEY* feature of JrDebugLogger, and what makes it special and a bit tricky under the hood, is that when you build in Release mode, by default all debug code is made to completely compile out and have 0 impact on size and cpu of application.
« Last Edit: November 14, 2010, 10:57 AM by mouser »