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