File Coverage

blib/lib/SlapbirdAPM/Dancer2/Trace.pm
Criterion Covered Total %
statement 21 56 37.5
branch 0 10 0.0
condition 0 9 0.0
subroutine 7 14 50.0
pod 0 3 0.0
total 28 92 30.4


line stmt bran cond sub pod time code
1             package SlapbirdAPM::Dancer2::Trace;
2              
3 1     1   6 use strict;
  1         1  
  1         48  
4 1     1   4 use warnings;
  1         1  
  1         179  
5              
6             my $logger;
7              
8             # set a callback sub for logging
9             sub callback {
10 0 0   0 0   shift if @_ > 1;
11              
12 0           my $coderef = shift;
13 0 0 0       unless ( ref($coderef) eq 'CODE' and defined(&$coderef) ) {
14 0           require Carp;
15 0           Carp::croak("$coderef is not a code reference!");
16             }
17              
18 0           $logger = $coderef;
19             }
20              
21             # where logging actually happens
22             sub _log_call {
23 0     0     my %args = @_;
24 0           return $logger->(%args);
25             }
26              
27             sub trace_pkgs {
28 0     0 0   my $class = shift;
29 0           _wrap_symbols(@_);
30             }
31              
32             sub trace_subs {
33 0     0 0   my ( $class, @tracers ) = @_;
34              
35 0           for (@tracers) {
36 1     1   4 no strict 'refs';
  1         2  
  1         39  
37 1     1   5 no warnings;
  1         3  
  1         178  
38 0           my $sub = *{$_}{CODE};
  0            
39 0 0 0       next unless defined $sub and defined &$sub;
40 0           *{$_} = sub {
41 0     0     return _log_call( name => "$_", args => [@_], 'sub' => $sub );
42 0           };
43             }
44             }
45              
46             sub _wrap_symbols {
47 0     0     my (@traces) = @_;
48              
49 0           my %seen = ( map { ( $_ => 1 ) } @traces );
  0            
50              
51 0           while ( my $traced = shift @traces ) {
52 0 0         next if $traced eq __PACKAGE__;
53              
54 0           my $src;
55              
56 1     1   5 no strict 'refs';
  1         1  
  1         23  
57              
58             # get the calling package symbol table name
59             {
60 1     1   3 no strict 'refs';
  1         1  
  1         83  
  0            
61 0           $src = \%{ $traced . '::' };
  0            
62             }
63              
64             # loop through all symbols in calling package, looking for subs
65 0           for my $symbol ( keys %$src ) {
66              
67             # get all code references, make sure they're valid
68 0           my $sub = *{ $traced . '::' . $symbol }{CODE};
  0            
69 0 0 0       next unless defined $sub and defined &$sub;
70              
71             {
72 1     1   5 no warnings;
  1         1  
  1         127  
  0            
73 0           *{ ${traced} . '::' . $symbol } = sub {
74 0     0     return _log_call(
75             name => "${traced}::$symbol",
76             args => [@_],
77             sub => $sub
78             );
79 0           };
80             };
81             }
82             }
83             }
84              
85             1;