How to deal with Debug information in a large program.

Discussion in 'Perl Misc' started by Franklin Lee, Jul 1, 2003.

  1. Franklin Lee

    Franklin Lee Guest

    Hi All,

    I'm in trouble with Debug information in a large program.
    Some info I just want to print in degug phase.
    Now I do as follow:
    ###########################################################
    ....
    use vars qw($Debug);
    $Debug ||= 0;

    ....
    #Define option -D
    $Debug=1 if(defined($opt{'D'});

    ....
    #Print Debug info when necessary
    if($Debug) {
    print("Debug:xxxxxx\n");
    }
    ############################################################

    Do you think it's a good way to deal with Debug info.
    Or do you have a better way to deal with it?

    Any suggestions are welcome!

    Thank you!

    Franklin
     
    Franklin Lee, Jul 1, 2003
    #1
    1. Advertising

  2. Franklin Lee

    Rich Guest

    Franklin Lee wrote:

    > Hi All,
    >
    > I'm in trouble with Debug information in a large program.
    > Some info I just want to print in degug phase.
    > Now I do as follow:
    > ###########################################################
    > ...
    > use vars qw($Debug);
    > $Debug ||= 0;
    >
    > ...
    > #Define option -D
    > $Debug=1 if(defined($opt{'D'});
    >
    > ...
    > #Print Debug info when necessary
    > if($Debug) {
    > print("Debug:xxxxxx\n");
    > }
    > ############################################################
    >
    > Do you think it's a good way to deal with Debug info.
    > Or do you have a better way to deal with it?
    >
    > Any suggestions are welcome!


    This will make your life hell in the long run, because it's an all or
    nothing approach. As your codebase grows, you'll have more and more
    debugging messages - many of which will be useless when you're trying to
    debug specific problems.

    Look at Log::Log4perl:

    "Logging beats a debugger if you want to know what's going on in your code
    during runtime. However, traditional logging packages are too static and
    generate a flood of log messages in your log files that won't help you.

    Log::Log4perl is different. It allows you to control the amount of logging
    messages generated at three different levels:

    At a central location in your system (either in a configuration file or in
    the startup code) you specify which components (classes, functions) of your
    system should generate logs.

    You specify how detailed the logging of these components should be by
    specifying logging levels.

    You also specify which so-called appenders you want to feed your log
    messages to ("Print it to the screen and also append it to /tmp/my.log")
    and which format ("Write the date first, then the file name and line
    number, and then the log message") they should be in.

    This is a very powerful and flexible mechanism. You can turn on and off your
    logs at any time, specify the level of detail and make that dependent on
    the subsystem that's currently executed."


    So, in my code I have lines such as:

    Log::Log4perl->get_logger(__PACKAGE__)->debug
    ("conditionally creating home directory: $home") if DEBUG;

    In my debug module (heavily edited):

    our @EXPORT_OK = qw( DEBUG );

    BEGIN
    {
    if ( $ENV{RIDAS_DEBUG} )
    {
    *DEBUG = sub () {1};
    }
    else
    {
    *DEBUG = sub () {0};
    }
    }

    This now gives me control of the amount of debugging info emitted on a
    module by module basis, specified by a simple Log4perl configuration file.

    Having the "if DEBUG" conditional allows debugging calls to be optimized
    away at compile time when not required, so performance does not suffer.

    Sure, this seems like a hastle to begin with, and the Log::Log4perl docs are
    heavy in places, but its a well designed framework, based off the widely
    used and respected Java log4j project.

    That's my suggestion anyway - it's worked very well for me, but others are
    sure to have equally good ways of doing things :)

    Cheers,
    --
    Rich
     
    Rich, Jul 1, 2003
    #2
    1. Advertising

  3. Franklin Lee

    Bob Walton Guest

    Franklin Lee wrote:

    > Hi All,
    >
    > I'm in trouble with Debug information in a large program.
    > Some info I just want to print in degug phase.
    > Now I do as follow:
    > ###########################################################
    > ...
    > use vars qw($Debug);
    > $Debug ||= 0;
    >
    > ...
    > #Define option -D
    > $Debug=1 if(defined($opt{'D'});
    >
    > ...
    > #Print Debug info when necessary
    > if($Debug) {
    > print("Debug:xxxxxx\n");
    > }
    > ############################################################
    >
    > Do you think it's a good way to deal with Debug info.
    > Or do you have a better way to deal with it?
    >
    > Any suggestions are welcome!

    ....
    > Franklin


    Another possible approach is:

    use Filter::Simple;

    to make a source code filter which will include or exclude debug
    statements, which would be statements that start with a flag like
    perhaps "#db ". The filter would simply remove the "#db " if debugging
    is desired; otherwise, those statements would just be commentary, and
    wouldn't normally be compiled or executed -- and you don't have to do
    the clumsy if($Debug){...} bit. One could get fancy and include debug
    levels with stuff like #db1 , #db2 , etc.

    --
    Bob Walton
     
    Bob Walton, Jul 2, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. z. f.
    Replies:
    0
    Views:
    721
    z. f.
    Feb 3, 2005
  2. Zach Wegner

    Re: Program to deal 5 cards

    Zach Wegner, Jun 27, 2003, in forum: C Programming
    Replies:
    0
    Views:
    819
    Zach Wegner
    Jun 27, 2003
  3. rbt

    deal or no deal

    rbt, Dec 22, 2005, in forum: Python
    Replies:
    7
    Views:
    600
    Duncan Smith
    Dec 28, 2005
  4. Replies:
    10
    Views:
    865
    Miki Tebeka
    Sep 28, 2012
  5. Shadowfirebird
    Replies:
    2
    Views:
    501
    Shadowfirebird
    Mar 8, 2011
Loading...

Share This Page