File Coverage

blib/lib/SlapbirdAPM/Trace.pm
Criterion Covered Total %
statement 21 55 38.1
branch 0 8 0.0
condition 0 9 0.0
subroutine 7 14 50.0
pod 0 3 0.0
total 28 89 31.4


line stmt bran cond sub pod time code
1             package SlapbirdAPM::Trace;
2              
3 1     1   5 use strict;
  1         1  
  1         28  
4 1     1   4 use warnings;
  1         1  
  1         164  
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           $logger->($args{name}, $args{args}, $args{'sub'});
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   6 no strict 'refs';
  1         1  
  1         36  
37 1     1   3 no warnings;
  1         3  
  1         145  
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           my $src;
53              
54 1     1   5 no strict 'refs';
  1         1  
  1         29  
55              
56             # get the calling package symbol table name
57             {
58 1     1   6 no strict 'refs';
  1         1  
  1         63  
  0            
59 0           $src = \%{$traced . '::'};
  0            
60             }
61              
62             # loop through all symbols in calling package, looking for subs
63 0           for my $symbol (keys %$src) {
64              
65             # get all code references, make sure they're valid
66 0           my $sub = *{$traced . '::' . $symbol}{CODE};
  0            
67 0 0 0       next unless defined $sub and defined &$sub;
68              
69             {
70 1     1   3 no warnings;
  1         1  
  1         151  
  0            
71 0           *{${traced} . '::' . $symbol} = sub {
72 0     0     return _log_call(name => "${traced}::$symbol", args => [@_], 'sub' => $sub);
73 0           };
74             };
75             }
76             }
77             }
78              
79             1;