blib/lib/DBIx/LogProfile.pm

filter: Statementstmt 25.7% 17 / 66 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 5 / 10 Podpod 66.6% 2 / 3 totaltotal 22.2% 24 / 108 SCAR 50.5
CC
16
Cov
18%
flush_to_logger
90.0
new
42.0
on_destroy
6.0
CRAP 155.7
↑ Summary
1 package DBIx::LogProfile;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use parent 'DBI::Profile';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = 0.01;
8  
9 sub new {
10  
11 0 ▶ my $pkg = shift;
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
12  
13 0 my $self = $pkg->SUPER::new(
14 Log => 'Any',
15 OrderByDesc => 'longest_duration',
16 Limit => undef,
17 Level => 'trace',
18 @_,
19 );
20  
21 # sanity check the method name. if something like debugf is
22 # specified, strange things may happen, but not dangerous.
23 0 ▶ die unless $self->{Level} =~ /^[a-z]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Level"} =~ /^[a-z]+$/ 0 0 0
24  
25 0 ▶ if ($self->{Log} eq 'Any') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'Log'} eq 'Any') { } 0 0 0
elsif ($self->{'Log'} eq 'Log4perl') { } 0 0 0
26  
27 } elsif ($self->{Log} eq 'Log4perl') {
28 0 eval "require Log::Log4perl";
29 0 eval "require Log::Log4perl::MDC";
30  
31 } else {
32 0 die "Bad Log parameter `$self->{Log}`. Must be Any or Log4perl."
33  
34 }
35  
36 0 DBI->trace_msg("$self: @{[ %$self ]}\n", 0)
37 0 ▶ if $self->{Trace} && $self->{Trace} >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Trace"} and $self->{"Trace"} >= 2 0 0 0
Condition0%0/3
$self->{"Trace"} and $self->{"Trace"} >= 2
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"Trace"} and $self->{"Trace"} >= 2
$self->{"Trace"} $self->{"Trace"} >= 2
Truth table0%0/3
A and B
A= $self->{"Trace"}B= $self->{"Trace"} >= 2
ABresult
0X0
100
111
38  
39 0 return $self;
40 }
41  
42 sub flush_to_logger {
43 0 ▶ my $self = shift;
Subroutine flush_to_logger: not called CC=9 SCAR=45.0
Pod documented
44 0 my $class = ref $self;
45 0 my $data = $self->{Data};
46 0 my $method = $self->{Level};
47  
48 0 my @fields = qw/
49 count
50 total_duration
51 first_duration
52 shortest_duration
53 longest_duration
54 time_of_first_sample
55 time_of_last_sample
56 /;
57  
58 my @nodes = map {
59 0 my ($statistics, @keys) = @$_;
60  
61 0 my %h;
62 0 @h{ @fields } = @$statistics;
63  
64 0 $h{path} = join ':', @{ $self->{Path} };
65  
66 0 for my $i ( 1 .. @keys ) {
67 0 $h{ "key$i" } = $keys[ $i - 1 ];
68 }
69  
70 0 \%h;
71  
72 } $self->as_node_path_list();
73  
74 0 my @sorted;
75
76 0 ▶ if ($self->{OrderByDesc} !~ /^key/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'OrderByDesc'} =~ /^key/) { } 0 0 0
77 @sorted = sort {
78 0 $b->{ $self->{OrderByDesc} }
79 <=>
80 $a->{ $self->{OrderByDesc} }
81 0 } @nodes;
82  
83 } else {
84 @sorted = sort {
85 0 $b->{ $self->{OrderByDesc} }
86 cmp
87 $a->{ $self->{OrderByDesc} }
88
89 0 } @nodes;
90
91 }
92  
93 0 my $counter = 0;
94  
95 0 eval {
96  
97 0 for my $h (@sorted) {
98  
99 0 ▶ if ($self->{Log} eq 'Log4perl') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'Log'} eq 'Log4perl') { } 0 0 0
elsif ($self->{'Log'} eq 'Any') { } 0 0 0
100 0 my $ctx = Log::Log4perl::MDC->get_context();
101  
102 0 local @{ $ctx }{ keys %$h } = values %$h;
103  
104 0 Log::Log4perl->get_logger()->$method(__PACKAGE__);
105  
106 } elsif ($self->{Log} eq 'Any') {
107 0 Log::Any->get_logger()->$method(__PACKAGE__, $h);
108  
109 }
110  
111 0 ▶ if (defined $self->{Limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Limit"}) 0 0 0
112 0 ▶ last if ++$counter >= $self->{Limit};
Branch: 1 branch
BranchTrueFalseTotal
if ++$counter >= $self->{"Limit"} 0 0 0
113 }
114 }
115  
116 };
117  
118 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
119  
120 0 Log::Any->get_logger()->errorf(
121 "%s caught exception: %s",
122 __PACKAGE__,
123 $@
124 );
125  
126 }
127  
128 0 $self->empty();
129  
130 }
131  
132 sub on_destroy {
133 0 ▶ my ($self) = @_;
Subroutine on_destroy: not called CC=2 SCAR=17.9
Pod undocumented
134 0 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 0 0
135 0 $self->flush_to_logger();
136 }
137  
138 sub DESTROY {
139 0 ▶ on_destroy(@_);
Subroutine DESTROY: not called CC=1 SCAR=6.9
140 }
141  
142 END {
143  
144 DBI->visit_handles(sub {
145 1 ▶ my ($dbh, $info) = @_;
Subroutine __ANON__: not called CC=2 SCAR=10.5
146 1 ▶ return unless UNIVERSAL::isa($dbh->{Profile}, __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($dbh->{"Profile"}, __PACKAGE__) 0 0 0
147 0 $dbh->{Profile}->flush_to_logger();
148 1 ▶ });
Subroutine END: called CC=1 SCAR=0.0
149  
150 };
151  
152 1 1;
153  
154 __END__
↑ Summary