File Coverage

blib/lib/Blosxom/Debug.pm
Criterion Covered Total %
statement 17 17 100.0
branch 3 4 75.0
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 25 27 92.5


line stmt bran cond sub pod time code
1             # Blosxom debug module and source filter
2             # Author(s): Gavin Carr
3             # Version: 0.001000
4              
5             package Blosxom::Debug;
6              
7 7     7   8468 use strict;
  7         12  
  7         356  
8 7     7   12845 use Filter::Simple;
  7         230748  
  7         53  
9              
10 7     7   461 use vars qw($VERSION);
  7         13  
  7         1232  
11              
12             $VERSION = 0.001000;
13              
14             # Source filter magic - uncomment calls to debug()
15             FILTER_ONLY
16             code => sub { s/^(\s*)#\s*(debug\()/$1$2/mg };
17              
18             my %debug_level = ();
19             sub import {
20             my $self = shift;
21             my %arg = @_;
22             my $package = caller;
23             $debug_level{$package} = $arg{debug_level} || 0;
24             {
25             # Export local debug() into plugin's namespace (unless one already exists)
26 7     7   45 no strict 'refs';
  7         13  
  7         1511  
27             *{"${package}::debug"} = \&debug unless defined &{"${package}::debug"};
28             }
29             }
30              
31             # Debug logger - warn @msg iff $level <= calling package's $debug_level
32             sub debug {
33 24     24 0 5568 my ($level, @msg) = @_;
34              
35 24         57 my $msg = join(' ', @msg);
36 24 50       105 $msg .= "\n" unless substr($msg, -1) eq "\n";
37              
38 24         46 my $package = caller;
39             # warn "[debug] level $level, package $package, debug_level $debug_level{$package}\n";
40 24 100       153 warn "$package debug $level: $msg" if $level <= $debug_level{$package};
41             }
42              
43             1;
44              
45             __END__