| 1 | package DBI::Log; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | no strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Time::HiRes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = "0.10"; | |||||||||||||||||||||||||||
| 10 | 1 | our %opts = ( | |||||||||||||||||||||||||||
| 11 | file => $file, | ||||||||||||||||||||||||||||
| 12 | trace => 0, | ||||||||||||||||||||||||||||
| 13 | timing => 0, | ||||||||||||||||||||||||||||
| 14 | fh => undef, | ||||||||||||||||||||||||||||
| 15 | exclude => undef, | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | my $orig_execute = \&DBI::st::execute; | |||||||||||||||||||||||||||
| 19 | *DBI::st::execute = sub { | ||||||||||||||||||||||||||||
| 20 | 4 | ▶ | my ($sth, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 4 | my $log = pre_query("execute", $sth->{Database}, $sth, $sth->{Statement}, \@args); | |||||||||||||||||||||||||||
| 22 | 4 | my $retval = $orig_execute->($sth, @args); | |||||||||||||||||||||||||||
| 23 | 4 | post_query($log); | |||||||||||||||||||||||||||
| 24 | 4 | return $retval; | |||||||||||||||||||||||||||
| 25 | 1 | }; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | my $orig_selectall_arrayref = \&DBI::db::selectall_arrayref; | |||||||||||||||||||||||||||
| 28 | *DBI::db::selectall_arrayref = sub { | ||||||||||||||||||||||||||||
| 29 | 1 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1 | my $log = pre_query("selectall_arrayref", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 31 | 1 | my $retval = $orig_selectall_arrayref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 32 | 1 | post_query($log); | |||||||||||||||||||||||||||
| 33 | 1 | return $retval; | |||||||||||||||||||||||||||
| 34 | 1 | }; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | my $orig_selectcol_arrayref = \&DBI::db::selectcol_arrayref; | |||||||||||||||||||||||||||
| 37 | *DBI::db::selectcol_arrayref = sub { | ||||||||||||||||||||||||||||
| 38 | 1 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 1 | my $log = pre_query("selectcol_arrayref", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 40 | 1 | my $retval = $orig_selectcol_arrayref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 41 | 1 | post_query($log); | |||||||||||||||||||||||||||
| 42 | 1 | return $retval; | |||||||||||||||||||||||||||
| 43 | 1 | }; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | my $orig_selectall_hashref = \&DBI::db::selectall_hashref; | |||||||||||||||||||||||||||
| 46 | *DBI::db::selectall_hashref = sub { | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | 0 | my $log = pre_query("selectall_hashref", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 49 | 0 | my $retval = $orig_selectall_hashref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 50 | 0 | post_query($log); | |||||||||||||||||||||||||||
| 51 | 0 | return $retval; | |||||||||||||||||||||||||||
| 52 | 1 | }; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | my $orig_selectrow_arrayref = \&DBI::db::selectrow_arrayref; | |||||||||||||||||||||||||||
| 55 | *DBI::db::selectrow_arrayref = sub { | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 57 | 0 | my $log = pre_query("selectrow_arrayref", $dbh, $sth, $query, \@args); | |||||||||||||||||||||||||||
| 58 | 0 | my $retval = $orig_selectrow_arrayref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 59 | 0 | post_query($log); | |||||||||||||||||||||||||||
| 60 | 0 | return $retval; | |||||||||||||||||||||||||||
| 61 | 1 | }; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | my $orig_selectrow_array = \&DBI::db::selectrow_array; | |||||||||||||||||||||||||||
| 64 | *DBI::db::selectrow_array = sub { | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 66 | 0 | my $log = pre_query("selectrow_array", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 67 | 0 | my $retval = $orig_selectrow_array->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 68 | 0 | post_query($log); | |||||||||||||||||||||||||||
| 69 | 0 | return $retval; | |||||||||||||||||||||||||||
| 70 | 1 | }; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | my $orig_selectrow_hashref = \&DBI::db::selectrow_hashref; | |||||||||||||||||||||||||||
| 73 | *DBI::db::selectrow_hashref = sub { | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 75 | 0 | my $log = pre_query("selectrow_hashref", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 76 | 0 | my $retval = $orig_selectrow_hashref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 77 | 0 | post_query($log); | |||||||||||||||||||||||||||
| 78 | 0 | return $retval; | |||||||||||||||||||||||||||
| 79 | 1 | }; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | my $orig_do = \&DBI::db::do; | |||||||||||||||||||||||||||
| 82 | *DBI::db::do = sub { | ||||||||||||||||||||||||||||
| 83 | 2 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 2 | my $log = pre_query("do", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 85 | 2 | my $retval = $orig_do->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 86 | 1 | post_query($log); | |||||||||||||||||||||||||||
| 87 | 1 | return $retval; | |||||||||||||||||||||||||||
| 88 | 1 | }; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub import { | ||||||||||||||||||||||||||||
| 92 | 1 | ▶ | my ($package, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=6 SCAR=22.9
| |||||||||||||||||||||||||||||
| 93 | 1 | for my $key (keys %args) { | |||||||||||||||||||||||||||
| 94 | 1 | $opts{$key} = $args{$key}; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 1 | ▶ | if (!$opts{file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | $opts{fh} = \*STDERR; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||
| 100 | 1 | my $file2 = $opts{file}; | |||||||||||||||||||||||||||
| 101 | 1 | ▶ | if ($file2 =~ m{^~/}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | my $home = $ENV{HOME} || (getpwuid($<))[7]; | ||||||||||||||||||||||||||
|
Condition0%0/3
$ENV{'HOME'} || (getpwuid $<)[7]
MC/DC0%0/2
$ENV{'HOME'} || (getpwuid $<)[7]
$ENV{'HOME'}
(getpwuid $<)[7]
Truth table0%0/3
A || B
A= $ENV{'HOME'}B= (getpwuid $<)[7]
| |||||||||||||||||||||||||||||
| 103 | 0 | $file2 =~ s{^~/}{$home/}; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 1 | ▶ | open $opts{fh}, ">>", $file2 or die "Can't open $opts{file}: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub pre_query { | ||||||||||||||||||||||||||||
| 110 | 8 | ▶ | my ($name, $dbh, $sth, $query, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
pre_query: called CC=22 SCAR=32.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 8 | my $log = {}; | |||||||||||||||||||||||||||
| 112 | 8 | my $mcount = 0; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Some DBI functions are composed of other DBI functions, so make sure we | ||||||||||||||||||||||||||||
| 115 | # are only logging the top level one. For example $dbh->do() will call | ||||||||||||||||||||||||||||
| 116 | # $dbh->execute() internally, so we need to make sure a DBI::Log function | ||||||||||||||||||||||||||||
| 117 | # logs the $dbh->do() and not the internal $dbh->execute(). If multiple | ||||||||||||||||||||||||||||
| 118 | # functions were called, we return and flag this log entry to be skipped in | ||||||||||||||||||||||||||||
| 119 | # the post_query() part. | ||||||||||||||||||||||||||||
| 120 | 8 | ▶ | for (my $i = 0; my @caller = caller($i); $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 20 | my ($package, $file, $line, $sub) = @caller; | |||||||||||||||||||||||||||
| 122 | 20 | ▶ | if ($package eq "DBI::Log") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 11 | $mcount++; | |||||||||||||||||||||||||||
| 124 | 11 | ▶ | if ($mcount > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 3 | $log->{skip} = 1; | |||||||||||||||||||||||||||
| 126 | 3 | return $log; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 5 | my @callers; | |||||||||||||||||||||||||||
| 131 | 5 | ▶ | for (my $i = 0; my @caller = caller($i); $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 11 | push @callers, \@caller; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # Order the call stack based on the highest level calls first, then the | ||||||||||||||||||||||||||||
| 136 | # lower level calls. Once you reach a package that is excluded, do not show | ||||||||||||||||||||||||||||
| 137 | # any more lines in the stack trace. By default, it will exclude anything | ||||||||||||||||||||||||||||
| 138 | # past the DBI::Log package, but if user provides an exclude option, it will | ||||||||||||||||||||||||||||
| 139 | # stop there. | ||||||||||||||||||||||||||||
| 140 | 5 | my @filtered_callers; | |||||||||||||||||||||||||||
| 141 | 5 | CALLER: for my $caller (reverse @callers) { | |||||||||||||||||||||||||||
| 142 | 11 | my ($package, $file, $line, $sub) = @$caller; | |||||||||||||||||||||||||||
| 143 | 11 | ▶ | if ($package eq "DBI::Log") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 5 | last CALLER; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 6 | ▶ | if ($opts{exclude}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | for my $item (@{$opts{exclude}}) { | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ($package =~ /^$item(::|$)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | last CALLER; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 6 | push @filtered_callers, $caller; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 5 | ▶ | if (!$opts{trace}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 5 | @filtered_callers = ($filtered_callers[-1]); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 5 | my $stack = ""; | |||||||||||||||||||||||||||
| 161 | 5 | for my $caller (@filtered_callers) { | |||||||||||||||||||||||||||
| 162 | 5 | my ($package, $file, $line, $sub) = @$caller; | |||||||||||||||||||||||||||
| 163 | 5 | my $short_sub = $sub; | |||||||||||||||||||||||||||
| 164 | 5 | $short_sub =~ s/.*:://; | |||||||||||||||||||||||||||
| 165 | 5 | ▶ | $short_sub = $name if $sub eq "DBI::Log::__ANON__"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 5 | $stack .= "-- $short_sub $file $line\n"; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 5 | ▶ | if (ref($query) && ref($query) eq "DBI::st") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $query and ref $query eq "DBI::st"
MC/DC50%1/2
ref $query and ref $query eq "DBI::st"
ref $query
ref $query eq "DBI::st"
Truth table66%2/3
A and B
A= ref $queryB= ref $query eq "DBI::st"
| |||||||||||||||||||||||||||||
| 170 | 1 | $sth = $query; | |||||||||||||||||||||||||||
| 171 | 1 | $query = $query->{Statement}; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 5 | ▶ | if ($dbh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | # When you use $sth->bind_param(1, "value") the params can be found in | ||||||||||||||||||||||||||||
| 176 | # $sth->{ParamValues} and they override arguments sent in to | ||||||||||||||||||||||||||||
| 177 | # $sth->execute() | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 5 | my @args_copy = @$args; | |||||||||||||||||||||||||||
| 180 | 5 | my %values; | |||||||||||||||||||||||||||
| 181 | 5 | ▶ | if ($sth && $sth->{ParamValues}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$sth and $sth->{"ParamValues"}
MC/DC50%1/2
$sth and $sth->{"ParamValues"}
$sth
$sth->{"ParamValues"}
Truth table66%2/3
A and B
A= $sthB= $sth->{"ParamValues"}
| |||||||||||||||||||||||||||||
| 182 | 2 | %values = %{$sth->{ParamValues}}; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 5 | for my $key (keys %values) { | |||||||||||||||||||||||||||
| 185 | 0 | ▶ | if (defined $key && $key =~ /^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $key and $key =~ /^\d+$/
MC/DC0%0/2
defined $key and $key =~ /^\d+$/
defined $key
$key =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $keyB= $key =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 186 | 0 | $args_copy[$key - 1] = $values{$key}; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 5 | for my $i (0 .. @args_copy - 1) { | |||||||||||||||||||||||||||
| 191 | 4 | my $value = $args_copy[$i]; | |||||||||||||||||||||||||||
| 192 | 4 | $value = $dbh->quote($value); | |||||||||||||||||||||||||||
| 193 | 4 | $query =~ s{\?}{$value}e; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 5 | $query =~ s/^\s*\n|\s*$//g; | |||||||||||||||||||||||||||
| 198 | 5 | $info = "-- " . scalar(localtime()) . "\n"; | |||||||||||||||||||||||||||
| 199 | 5 | print {$opts{fh}} "$info$stack$query\n"; | |||||||||||||||||||||||||||
| 200 | 5 | $log->{time1} = Time::HiRes::time(); | |||||||||||||||||||||||||||
| 201 | 5 | return $log; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | sub post_query { | ||||||||||||||||||||||||||||
| 205 | 7 | ▶ | my ($log) = @_; | ||||||||||||||||||||||||||
|
Subroutine
post_query: called CC=3 SCAR=12.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 206 | 7 | ▶ | return if $log->{skip}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 4 | ▶ | if ($opts{timing}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 0 | $log->{time2} = Time::HiRes::time(); | |||||||||||||||||||||||||||
| 209 | 0 | my $diff = sprintf '%.3f', $log->{time2} - $log->{time1}; | |||||||||||||||||||||||||||
| 210 | 0 | print {$opts{fh}} "-- ${diff}s\n"; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | 4 | print {$opts{fh}} "\n"; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 1 | 1; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | __END__ | ||||||||||||||||||||||||||||