ibanix has asked for the wisdom of the Perl Monks concerning the following question:

Greetings again, Monks,

I've got some code with a pretty simple debug routine. Not relevent parts of code not included.
# Uncomment to enable writing debuging info to logfile my $debug = 1; # Open a debug logfile if ($debug) { my $logfile = "movelog.debug.log"; open(DEBUG, ">>$logfile"); print DEBUG localtime().": Startup.\n"; } if ($debug) { print DEBUG locasltime." : This is some example line...\ +n"; }
This works well enough for me, but there are probally better ways to do it. Now to the real question:

How can I modify this for multiple levels of debug without having to radically alter my debug statements?

I'm thinking something like:
my $debug = 2; # This is level 2 information if (something) { print DEBUG "LEVEL 2: foo\n"; } # This is level 1 information if (something) { print DEBUG "LEVEL 1: moo\n": }
Where setting level 'n' would trigger all level 'n' and lower, from n-1 to level 0, debug output.

All advice appreciated! :-)

Cheers,
ibanix

$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Replies are listed 'Best First'.
Re: Making debug a little more intelligent
by adrianh (Chancellor) on Dec 10, 2002 at 23:58 UTC

    You might want to take a look at one of the existing logging modules on CPAN. For example, Log::Log4perl, is fairly simple and gives you lots of flexibility.

    If you don't go that route I would consider using the constant module, rather than the lexical $debug. For example:

    use constant DEBUGGING => 2; ... print DEBUG "foo\n" if DEBUGGING >=2; ... print DEBUG "moo\n" if DEBUGGING;

    The advantage of using the constant module is that the compiler can optimise out the debugging statements if DEBUGGING is false - potentially saving time and space.

Re: Making debug a little more intelligent
by elusion (Curate) on Dec 10, 2002 at 23:35 UTC
    Here's what I'd use:
    my $debug = 2; sub debug { my ($level, $msg) = @_; return if $level > $debug; print DEBUG "LEVEL $level: $msg"; } debug 2, "foo\n" if something; debug 1, "moo\n" if something;

    elusion : http://matt.diephouse.com

Re: Making debug a little more intelligent
by traveler (Parson) on Dec 10, 2002 at 22:57 UTC
    I generally use something like
    print LOG "message" if $debug <= 2;
    Of course, I open the log and set the $| =1 in BEGIN. In one system, I do not open it if $debug == 0 and I close it/open it if the user changes $debug to/from 0 at runtime (which happens to be through a GUI).

    HTH, --traveler

Re: Making debug a little more intelligent
by Mr. Muskrat (Canon) on Dec 10, 2002 at 23:00 UTC

    I'd use:

    my $debug = 2; # This is level 3 information if ($debug >= 3) { print DEBUG "LEVEL 3: bar\n"; } # This is level 2 information if ($debug >= 2) { print DEBUG "LEVEL 2: foo\n"; } # This is level 1 information if ($debug >= 1) { print DEBUG "LEVEL 1: moo\n"; }

      I was going to do that, but it seemed so... like C. :-P

      That was a joke.

      $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
      $debug and print 1 == $debug ? "LEVEL 1: moo\n" : 2 == $debug ? "LEVEL 2: foo\n" : "LEVEL 3: bar\n";

      Makeshifts last the longest.

Re: Making debug a little more intelligent
by vek (Prior) on Dec 11, 2002 at 13:57 UTC
    I usually use an environment variable to control debugging. This means I can do this in my code:
    my $DEBUG = $ENV{DEBUG} || 0;
    That way I don't have to worry about actually modifying the code in order to change how much debug info is logged.

    -- vek --