File Coverage

blib/lib/App/HTTP_Proxy_IMP/Debug.pm
Criterion Covered Total %
statement 23 41 56.1
branch 1 6 16.6
condition n/a
subroutine 8 12 66.6
pod 0 2 0.0
total 32 61 52.4


line stmt bran cond sub pod time code
1 1     1   7 use strict;
  1         2  
  1         33  
2 1     1   4 use warnings;
  1         2  
  1         36  
3             package App::HTTP_Proxy_IMP::Debug;
4 1     1   563 use Time::HiRes 'gettimeofday';
  1         1428  
  1         4  
5              
6             # let Net::Inspect::Debug output via local _out function
7             use Net::Inspect::Debug
8 1         7 output => \&_out,
9 1     1   186 qw($DEBUG $DEBUG_RX);
  1         3  
10              
11             my @context;
12             {
13             package App::HTTP_Proxy_IMP::Debug::Context;
14 0     0   0 sub new { return bless {},shift };
15 0     0   0 sub DESTROY { pop @context }
16             }
17              
18             sub debug {
19 2 50   2 0 7 if (@context) {
20 0         0 my $msg = shift;
21 1 0   1   192 $msg = do { no warnings; sprintf($msg,@_) } if @_;
  1         2  
  1         265  
  0         0  
  0         0  
22 0         0 my %args;
23 0         0 %args = ( %args, @$_ ) for(@context);
24 0 0       0 if ( my $id = delete $args{id} ) {
25 0         0 $msg = "$id $msg";
26             }
27 0         0 $msg .= " $_=$args{$_}" for( sort keys %args );
28 0         0 @_ = $msg;
29             }
30 2         9 goto &Net::Inspect::Debug::debug;
31             }
32              
33             sub debug_context {
34 0     0 0   push @context, \@_;
35 0           App::HTTP_Proxy_IMP::Debug::Context->new;
36             }
37              
38              
39             # let Net::IMP::Debug use debugging from Net::Inspect::Debug
40             use Net::IMP::Debug
41 1         11 var => \$DEBUG,
42 1     1   8 sub => \&debug;
  1         3  
43              
44             # re-export $DEBUG debug $DEBUG_RX we got from Net::Inspect::Debug
45 1     1   64 use base 'Exporter';
  1         2  
  1         379  
46             our @EXPORT = qw($DEBUG debug);
47             our @EXPORT_OK = qw($DEBUG_RX debug_context);
48              
49             # local output
50             sub _out {
51 0     0     my ($prefix,$msg) = @_;
52 0           $msg =~s{\n}{\n | }g; # prefix continuation lines
53 0           $msg =~s{(\\|[^[:print:][:space:]])}{ sprintf("\\%03o",ord($1)) }esg;
  0            
54 0           printf STDERR "%.2f %s %s\n", 0+gettimeofday(), $prefix,$msg;
55             }
56              
57             1;