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