| 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 IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Time::HiRes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = "0.12"; | |||||||||||||||||||||||||||
| 11 | 1 | our %opts = ( | |||||||||||||||||||||||||||
| 12 | file => $file, | ||||||||||||||||||||||||||||
| 13 | trace => 0, | ||||||||||||||||||||||||||||
| 14 | timing => 0, | ||||||||||||||||||||||||||||
| 15 | replace_placeholders => 1, | ||||||||||||||||||||||||||||
| 16 | fh => undef, | ||||||||||||||||||||||||||||
| 17 | exclude => undef, | ||||||||||||||||||||||||||||
| 18 | format => "sql", | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | my $orig_execute = \&DBI::st::execute; | |||||||||||||||||||||||||||
| 22 | 4 | ▶ | *DBI::st::execute = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 4 | my ($sth, @args) = @_; | |||||||||||||||||||||||||||
| 24 | 4 | my $log = pre_query("execute", $sth->{Database}, $sth, $sth->{Statement}, \@args); | |||||||||||||||||||||||||||
| 25 | 4 | my $retval = $orig_execute->($sth, @args); | |||||||||||||||||||||||||||
| 26 | 4 | post_query($log); | |||||||||||||||||||||||||||
| 27 | 4 | return $retval; | |||||||||||||||||||||||||||
| 28 | 1 | }; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | my $orig_selectall_arrayref = \&DBI::db::selectall_arrayref; | |||||||||||||||||||||||||||
| 31 | 1 | ▶ | *DBI::db::selectall_arrayref = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 1 | my ($dbh, $query, $yup, @args) = @_; | |||||||||||||||||||||||||||
| 33 | 1 | my $log = pre_query("selectall_arrayref", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 34 | 1 | my $retval = $orig_selectall_arrayref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 35 | 1 | post_query($log); | |||||||||||||||||||||||||||
| 36 | 1 | return $retval; | |||||||||||||||||||||||||||
| 37 | 1 | }; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | my $orig_selectcol_arrayref = \&DBI::db::selectcol_arrayref; | |||||||||||||||||||||||||||
| 40 | 1 | ▶ | *DBI::db::selectcol_arrayref = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 1 | my ($dbh, $query, $yup, @args) = @_; | |||||||||||||||||||||||||||
| 42 | 1 | my $log = pre_query("selectcol_arrayref", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 43 | 1 | my $retval = $orig_selectcol_arrayref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 44 | 1 | post_query($log); | |||||||||||||||||||||||||||
| 45 | 1 | return $retval; | |||||||||||||||||||||||||||
| 46 | 1 | }; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | my $orig_selectall_hashref = \&DBI::db::selectall_hashref; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | *DBI::db::selectall_hashref = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | 0 | my ($dbh, $query, $yup, @args) = @_; | |||||||||||||||||||||||||||
| 51 | 0 | my $log = pre_query("selectall_hashref", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 52 | 0 | my $retval = $orig_selectall_hashref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 53 | 0 | post_query($log); | |||||||||||||||||||||||||||
| 54 | 0 | return $retval; | |||||||||||||||||||||||||||
| 55 | 1 | }; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | my $orig_selectrow_arrayref = \&DBI::db::selectrow_arrayref; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | *DBI::db::selectrow_arrayref = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 59 | 0 | my ($dbh, $query, $yup, @args) = @_; | |||||||||||||||||||||||||||
| 60 | 0 | my $log = pre_query("selectrow_arrayref", $dbh, $sth, $query, \@args); | |||||||||||||||||||||||||||
| 61 | 0 | my $retval = $orig_selectrow_arrayref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 62 | 0 | post_query($log); | |||||||||||||||||||||||||||
| 63 | 0 | return $retval; | |||||||||||||||||||||||||||
| 64 | 1 | }; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | my $orig_selectrow_array = \&DBI::db::selectrow_array; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | *DBI::db::selectrow_array = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 68 | 0 | my ($dbh, $query, $yup, @args) = @_; | |||||||||||||||||||||||||||
| 69 | 0 | my $log = pre_query("selectrow_array", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 70 | 0 | my $retval = $orig_selectrow_array->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 71 | 0 | post_query($log); | |||||||||||||||||||||||||||
| 72 | 0 | return $retval; | |||||||||||||||||||||||||||
| 73 | 1 | }; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | my $orig_selectrow_hashref = \&DBI::db::selectrow_hashref; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | *DBI::db::selectrow_hashref = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | 0 | my ($dbh, $query, $yup, @args) = @_; | |||||||||||||||||||||||||||
| 78 | 0 | my $log = pre_query("selectrow_hashref", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 79 | 0 | my $retval = $orig_selectrow_hashref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 80 | 0 | post_query($log); | |||||||||||||||||||||||||||
| 81 | 0 | return $retval; | |||||||||||||||||||||||||||
| 82 | 1 | }; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | my $orig_do = \&DBI::db::do; | |||||||||||||||||||||||||||
| 85 | 3 | ▶ | *DBI::db::do = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 86 | 3 | my ($dbh, $query, $yup, @args) = @_; | |||||||||||||||||||||||||||
| 87 | 3 | my $log = pre_query("do", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 88 | 3 | my $retval = $orig_do->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 89 | 2 | post_query($log); | |||||||||||||||||||||||||||
| 90 | 2 | return $retval; | |||||||||||||||||||||||||||
| 91 | 1 | }; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=6 SCAR=22.3
| |||||||||||||||||||||||||||||
| 95 | 2 | my ($package, %args) = @_; | |||||||||||||||||||||||||||
| 96 | 2 | for my $key (keys %args) { | |||||||||||||||||||||||||||
| 97 | 3 | $opts{$key} = $args{$key}; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 2 | ▶ | if (!$opts{file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | $opts{fh} = \*STDERR; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | else { | ||||||||||||||||||||||||||||
| 103 | 2 | my $file2 = $opts{file}; | |||||||||||||||||||||||||||
| 104 | 2 | ▶ | if ($file2 =~ m{^~/}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 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]
| |||||||||||||||||||||||||||||
| 106 | 0 | $file2 =~ s{^~/}{$home/}; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 2 | ▶ | open $opts{fh}, ">>", $file2 or die "Can't open $opts{file}: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | # autoflush so that tailing to watch queries being performed works | ||||||||||||||||||||||||||||
| 110 | # as you'd expect | ||||||||||||||||||||||||||||
| 111 | 2 | $opts{fh}->autoflush(1); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 9 | ▶ | sub pre_query { | ||||||||||||||||||||||||||
|
Subroutine
pre_query: called CC=25 SCAR=34.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | 9 | my ($name, $dbh, $sth, $query, $args) = @_; | |||||||||||||||||||||||||||
| 117 | 9 | my $log = {}; | |||||||||||||||||||||||||||
| 118 | 9 | my $mcount = 0; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # Some DBI functions are composed of other DBI functions, so make sure we | ||||||||||||||||||||||||||||
| 121 | # are only logging the top level one. For example $dbh->do() will call | ||||||||||||||||||||||||||||
| 122 | # $dbh->execute() internally, so we need to make sure a DBI::Log function | ||||||||||||||||||||||||||||
| 123 | # logs the $dbh->do() and not the internal $dbh->execute(). If multiple | ||||||||||||||||||||||||||||
| 124 | # functions were called, we return and flag this log entry to be skipped in | ||||||||||||||||||||||||||||
| 125 | # the post_query() part. | ||||||||||||||||||||||||||||
| 126 | 9 | ▶ | for (my $i = 0; my @caller = caller($i); $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 22 | my ($package, $file, $line, $sub) = @caller; | |||||||||||||||||||||||||||
| 128 | 22 | ▶ | if ($package eq "DBI::Log") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 12 | $mcount++; | |||||||||||||||||||||||||||
| 130 | 12 | ▶ | if ($mcount > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 3 | $log->{skip} = 1; | |||||||||||||||||||||||||||
| 132 | 3 | return $log; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 6 | my @callers; | |||||||||||||||||||||||||||
| 137 | 6 | ▶ | for (my $i = 0; my @caller = caller($i); $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 13 | push @callers, \@caller; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Order the call stack based on the highest level calls first, then the | ||||||||||||||||||||||||||||
| 142 | # lower level calls. Once you reach a package that is excluded, do not show | ||||||||||||||||||||||||||||
| 143 | # any more lines in the stack trace. By default, it will exclude anything | ||||||||||||||||||||||||||||
| 144 | # past the DBI::Log package, but if user provides an exclude option, it will | ||||||||||||||||||||||||||||
| 145 | # stop there. | ||||||||||||||||||||||||||||
| 146 | 6 | my @filtered_callers; | |||||||||||||||||||||||||||
| 147 | 6 | CALLER: for my $caller (reverse @callers) { | |||||||||||||||||||||||||||
| 148 | 13 | my ($package, $file, $line, $long_sub) = @$caller; | |||||||||||||||||||||||||||
| 149 | 13 | ▶ | if ($package eq "DBI::Log") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 6 | last CALLER; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 7 | ▶ | if ($opts{exclude}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | for my $item (@{$opts{exclude}}) { | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ($package =~ /^$item(::|$)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | last CALLER; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 7 | push @filtered_callers, $caller; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 6 | ▶ | if (!$opts{trace}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 6 | @filtered_callers = ($filtered_callers[-1]); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 6 | my @stack; | |||||||||||||||||||||||||||
| 167 | 6 | for my $caller (@filtered_callers) { | |||||||||||||||||||||||||||
| 168 | 6 | my ($package, $file, $line, $long_sub) = @$caller; | |||||||||||||||||||||||||||
| 169 | 6 | my $sub = $long_sub; | |||||||||||||||||||||||||||
| 170 | 6 | $sub =~ s/.*:://; | |||||||||||||||||||||||||||
| 171 | 6 | ▶ | $sub = $name if $long_sub =~ /^DBI::Log::__ANON__/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 6 | push @stack, { | |||||||||||||||||||||||||||
| 173 | sub => $sub, | ||||||||||||||||||||||||||||
| 174 | file => $file, | ||||||||||||||||||||||||||||
| 175 | line => $line, | ||||||||||||||||||||||||||||
| 176 | }; | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 6 | ▶ | 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"
| |||||||||||||||||||||||||||||
| 180 | 1 | $sth = $query; | |||||||||||||||||||||||||||
| 181 | 1 | $query = $query->{Statement}; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 6 | ▶ | if ($dbh && $opts{replace_placeholders}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$dbh and $opts{"replace_placeholders"}
MC/DC0%0/2
$dbh and $opts{"replace_placeholders"}
$dbh
$opts{"replace_placeholders"}
Truth table33%1/3
A and B
A= $dbhB= $opts{"replace_placeholders"}
| |||||||||||||||||||||||||||||
| 185 | # When you use $sth->bind_param(1, "value") the params can be found in | ||||||||||||||||||||||||||||
| 186 | # $sth->{ParamValues} and they override arguments sent in to | ||||||||||||||||||||||||||||
| 187 | # $sth->execute() | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 6 | my @args_copy = @$args; | |||||||||||||||||||||||||||
| 190 | 6 | my %values; | |||||||||||||||||||||||||||
| 191 | 6 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 192 | 2 | %values = %{$sth->{ParamValues}}; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | 6 | for my $key (keys %values) { | |||||||||||||||||||||||||||
| 195 | 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+$/
| |||||||||||||||||||||||||||||
| 196 | 0 | $args_copy[$key - 1] = $values{$key}; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 6 | for my $i (0 .. @args_copy - 1) { | |||||||||||||||||||||||||||
| 201 | 4 | my $value = $args_copy[$i]; | |||||||||||||||||||||||||||
| 202 | 4 | $value = $dbh->quote($value); | |||||||||||||||||||||||||||
| 203 | 4 | $query =~ s{\?}{$value}e; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 6 | $query =~ s/^\s*\n|\s*$//g; | |||||||||||||||||||||||||||
| 208 | 6 | $log->{time_started} = Time::HiRes::time(); | |||||||||||||||||||||||||||
| 209 | 6 | $log->{query} = $query; | |||||||||||||||||||||||||||
| 210 | 6 | $log->{stack} = \@stack; | |||||||||||||||||||||||||||
| 211 | 6 | ▶ | if ($opts{format} eq "json") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | # For JSON output we don't want to output anything yet, so post_query() | ||||||||||||||||||||||||||||
| 213 | # can emit the whole JSON object, just remember them. | ||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | else { | ||||||||||||||||||||||||||||
| 216 | 5 | my $mesg; | |||||||||||||||||||||||||||
| 217 | 5 | $mesg .= "-- " . scalar(localtime()) . "\n"; | |||||||||||||||||||||||||||
| 218 | 5 | for my $caller (@stack) { | |||||||||||||||||||||||||||
| 219 | 5 | $mesg .= "-- $caller->{sub} $caller->{file} $caller->{line}\n"; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | 5 | $mesg .= "$query\n"; | |||||||||||||||||||||||||||
| 222 | 5 | print {$opts{fh}} $mesg; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 6 | return $log; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 8 | ▶ | sub post_query { | ||||||||||||||||||||||||||
|
Subroutine
post_query: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 229 | 8 | my ($log) = @_; | |||||||||||||||||||||||||||
| 230 | 8 | ▶ | return if $log->{skip}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 5 | $log->{time_ended} = Time::HiRes::time(); | |||||||||||||||||||||||||||
| 232 | 5 | $log->{time_taken} = sprintf "%.3f", $log->{time_ended} - $log->{time_started}; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 5 | ▶ | if ($opts{format} eq "json") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | # print all the info as JSON | ||||||||||||||||||||||||||||
| 236 | 1 | print {$opts{fh}} to_json($log) . "\n"; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | else { | ||||||||||||||||||||||||||||
| 239 | # For SQL output format, pre_query already printed most of the info, we | ||||||||||||||||||||||||||||
| 240 | # just need to add the time taken - and that only if we're doing | ||||||||||||||||||||||||||||
| 241 | # timings... | ||||||||||||||||||||||||||||
| 242 | 4 | ▶ | if ($opts{timing}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 0 | print {$opts{fh}} "-- $log->{time_taken}s\n"; | |||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | 4 | print {$opts{fh}} "\n"; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 10 | ▶ | sub to_json { | ||||||||||||||||||||||||||
|
Subroutine
to_json: called CC=20 SCAR=32.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 250 | 10 | my ($val, $depth) = @_; | |||||||||||||||||||||||||||
| 251 | 10 | ▶ | $depth ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$depth ||= 0
MC/DC100%1/1
$depth ||= 0
$depth
Truth table100%2/2
A
A= $depth
| |||||||||||||||||||||||||||||
| 252 | 10 | my $pretty = 0; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 10 | my $out; | |||||||||||||||||||||||||||
| 255 | 10 | ▶ | if (!defined $val) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 256 | 0 | $out = "null"; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | elsif (ref $val eq "HASH") { | ||||||||||||||||||||||||||||
| 259 | 2 | $out = "{"; | |||||||||||||||||||||||||||
| 260 | 2 | ▶ | $out .= "\n" if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 2 | my $i = 0; | |||||||||||||||||||||||||||
| 262 | 2 | for my $key (sort keys %$val) { | |||||||||||||||||||||||||||
| 263 | 8 | my $val2 = $val->{$key}; | |||||||||||||||||||||||||||
| 264 | 8 | ▶ | if ($i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 6 | ▶ | $out .= $pretty ? ",\n" : ", "; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | 8 | ▶ | $out .= " " x ($depth + 1) if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 8 | $out .= "\"$key\": " . to_json($val2, $depth + 1); | |||||||||||||||||||||||||||
| 269 | 8 | $i++; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | 2 | ▶ | $out .= "\n" if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 2 | ▶ | $out .= " " x ($depth) if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 2 | $out .= "}"; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | elsif (ref $val eq "ARRAY") { | ||||||||||||||||||||||||||||
| 276 | 1 | $out = "["; | |||||||||||||||||||||||||||
| 277 | 1 | ▶ | $out .= "\n" if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 1 | for my $i (0 .. @$val - 1) { | |||||||||||||||||||||||||||
| 279 | 1 | my $val2 = $val->[$i]; | |||||||||||||||||||||||||||
| 280 | 1 | ▶ | if ($i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 0 | ▶ | $out .= $pretty ? ",\n" : ", "; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | 1 | ▶ | $out .= " " x ($depth + 1) if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 1 | $out .= to_json($val2, $depth + 1); | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | 1 | ▶ | $out .= "\n" if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 1 | ▶ | $out .= " " x ($depth) if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | 1 | $out .= "]"; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | elsif ($val =~ /^(-?\d+(\.\d*)?(e[+-]?\d+)?)$/i) { | ||||||||||||||||||||||||||||
| 291 | 4 | $out = $val; | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | else { | ||||||||||||||||||||||||||||
| 294 | # Make the value suitable to use in a JSON string - no newlines, escape | ||||||||||||||||||||||||||||
| 295 | # control characters and double quotes. | ||||||||||||||||||||||||||||
| 296 | 3 | $val =~ s/"/\\"/g; | |||||||||||||||||||||||||||
| 297 | 3 | $val =~ s/\n/ /g; | |||||||||||||||||||||||||||
| 298 | 0 | $val =~ s/([\x00-\x1F])/sprintf("\\u%04x", ord($1))/eg; | |||||||||||||||||||||||||||
| 299 | 3 | $out = "\"$val\""; | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 10 | return $out; | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 1 | 1; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | __END__ | ||||||||||||||||||||||||||||