File Coverage

blib/lib/DBIx/DataStore/Debug.pm
Criterion Covered Total %
statement 14 45 31.1
branch 3 20 15.0
condition 2 6 33.3
subroutine 4 7 57.1
pod 0 3 0.0
total 23 81 28.4


line stmt bran cond sub pod time code
1             package DBIx::DataStore::Debug;
2             $DBIx::DataStore::Debug::VERSION = '0.097';
3 2     2   9 use strict;
  2         3  
  2         59  
4 2     2   14 use warnings;
  2         4  
  2         867  
5              
6             sub import {
7 2     2   5 my ($pkg, $debug_level) = @_;
8              
9 2 50 33     28 $debug_level = 0 unless defined $debug_level && $debug_level =~ /^\d+$/o && $debug_level > 0;
      33        
10              
11 2 50       97 unless (eval "defined DBIx::DataStore::DEBUG();") {
12 2     0 0 71 eval("sub DEBUG () { return $debug_level; }"); # give ourselves an easy-to-reference local copy
  0         0  
13 2     29 0 68 eval("sub DBIx::DataStore::DEBUG () { return $debug_level; }");
  29         130  
14 2     0 0 81 eval("sub DBIx::DataStore::dslog { DBIx::DataStore::Debug::_logger(\@_) }");
  0            
15             }
16              
17 2 50       27 _logger(q{Debug mode enabled at level}, $debug_level) if $debug_level > 0;
18             }
19              
20             sub _logger {
21 0 0   0     my @args = scalar(@_) > 0 ? @_ : ();
22              
23 0           my @c = caller(1);
24              
25 0           my $out = sprintf("[%s] %s,%d", scalar(localtime()), $c[1], $c[2]);
26              
27             # at debugging level 5 and higher we dump the full stack (and abandon single-line output)
28 0 0         if (DEBUG() >= 5) {
29 0           $out .= "\n";
30 0           my $i = 0;
31 0           my (@stack);
32 0           while (@c = caller($i)) {
33 0           push(@stack, [@c]);
34 0           $i++;
35             }
36 0 0         if (scalar(@stack) > 0) {
37             # drop in some column headings, just so output is unambiguous (at the end, since we reverse the stack
38             # prior to printing it out)
39 0           push(@stack, [qw( Package Filename Line Subroutine Hasargs Wantarray Evaltext Isrequire Hints Bitmask )]);
40             # get column widths
41 0           my @w = (0) x scalar(@{$stack[0]});
  0            
42 0           for ($i = 0; $i < scalar(@w); $i++) {
43 0           for (my $j = 0; $j < scalar(@stack); $j++) {
44 0 0         my $l = defined $stack[$j]->[$i] ? length($stack[$j]->[$i]) : 0;
45 0 0         $w[$i] = $l if $l > $w[$i];
46             }
47             }
48              
49 0           $out .= " ** STACK TRACE\n";
50              
51 0           foreach (reverse @stack) {
52 0           @c = @{$_};
  0            
53 0           $out .= sprintf(" + %-$w[0]s %-$w[1]s %$w[2]s %-$w[3]s\n", @c[0..3]);
54             }
55              
56 0           $out .= " ** MESSAGE\n";
57             }
58             } else {
59 0           $out .= ": ";
60             }
61              
62 0 0         $out .= scalar(@args) > 0 ? qq{@args\n} : qq{ -- NO MESSAGE -- \n};
63              
64 0 0         $out .= " ** END\n" if DEBUG() >= 5;
65              
66 0           print STDERR $out;
67              
68 0           return;
69             }
70              
71             1;