| 1 | 1 | ▶ | use strict; use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | package Devel::PerlLog; | ||||||||||||||
| 3 | 1 | our $VERSION = '0.04'; | |||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | ▶ | use Fcntl qw(:flock SEEK_END); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use Time::HiRes; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | my $log_path; | |||||||||||||
| 9 | my $log_handle; | ||||||||||||||
| 10 | 1 | my %data; | |||||||||||||
| 11 | 1 | my @plugins; | |||||||||||||
| 12 | 1 | my %group = ( | |||||||||||||
| 13 | all => [qw(argv cwd pid)], | ||||||||||||||
| 14 | ); | ||||||||||||||
| 15 | |||||||||||||||
| 16 | 2 | ▶ | sub write_log { | ||||||||||||
|
Subroutine
write_log: called CC=6 SCAR=27.6
Pod
undocumented
| |||||||||||||||
| 17 | 2 | my ($text) = @_; | |||||||||||||
| 18 | 2 | ▶ | if ($log_path ne 'STDOUT') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 19 | 0 | ▶ | flock $log_handle, LOCK_EX | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 20 | or die "Cannot lock '$log_path':\n$!\n"; | ||||||||||||||
| 21 | 0 | ▶ | seek $log_handle, 0, SEEK_END | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 22 | or die "Cannot seek '$log_path':\n$!\n"; | ||||||||||||||
| 23 | } | ||||||||||||||
| 24 | 2 | print $log_handle $text; | |||||||||||||
| 25 | 2 | ▶ | if ($log_path ne 'STDOUT') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | 0 | ▶ | flock $log_handle, LOCK_UN | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | or die "Cannot unlock '$log_path':\n$!\n"; | ||||||||||||||
| 28 | } | ||||||||||||||
| 29 | } | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 1 | ▶ | sub import { | ||||||||||||
|
Subroutine
import: called CC=11 SCAR=40.8
| |||||||||||||||
| 32 | 1 | my ($class, @args) = @_; | |||||||||||||
| 33 | 1 | for my $arg (@args) { | |||||||||||||
| 34 | 0 | ▶ | if ($arg =~ m![\\\/\.]!) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 35 | 0 | ▶ | die "Devel::PerlLog log path already set to '$log_path'" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | if $log_path; | ||||||||||||||
| 37 | 0 | $log_path = $arg; | |||||||||||||
| 38 | 0 | ▶ | open $log_handle, '>>', $log_path | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | or die "Can't open '$log_path' for append:\n$!"; | ||||||||||||||
| 40 | } | ||||||||||||||
| 41 | elsif ($arg =~ m!^(all)$!) { | ||||||||||||||
| 42 | die "No support for Devel::PerlLog '$arg'" | ||||||||||||||
| 43 | 0 | ▶ | unless $group{$arg}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | 0 | $class->add(@{$group{$arg}}); | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | else { | ||||||||||||||
| 47 | 0 | $class->add($arg); | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | } | ||||||||||||||
| 50 | 1 | ▶ | $log_handle ||= \*STDOUT; | ||||||||||||
|
Condition50%1/2
$log_handle ||= \*STDOUT
MC/DC0%0/1
$log_handle ||= \*STDOUT
$log_handle
Truth table50%1/2
A
A= $log_handle
| |||||||||||||||
| 51 | 1 | ▶ | $log_path ||= 'STDOUT'; | ||||||||||||
|
Condition50%1/2
$log_path ||= "STDOUT"
MC/DC0%0/1
$log_path ||= "STDOUT"
$log_path
Truth table50%1/2
A
A= $log_path
| |||||||||||||||
| 52 | 1 | for my $plugin (@plugins) { | |||||||||||||
| 53 | 0 | my $method = "do_$plugin"; | |||||||||||||
| 54 | 0 | ▶ | die "No support for Devel::PerlLog '$plugin'" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | unless $class->can($method); | ||||||||||||||
| 56 | 0 | $class->$method; | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | 1 | my $time = localtime; | |||||||||||||
| 59 | 1 | write_log "# $time ($$) Perl BEGIN:\n"; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | END { | ||||||||||||||
| 63 | require YAML::XS; | ||||||||||||||
| 64 | $YAML::XS::Head = 0; | ||||||||||||||
| 65 | my $time = localtime; | ||||||||||||||
| 66 | write_log "# $time ($$) Perl END:\n"; | ||||||||||||||
| 67 | my @keys = keys %data; | ||||||||||||||
| 68 | return unless @keys; | ||||||||||||||
| 69 | my $dump = YAML::XS::Dump(\%data); | ||||||||||||||
| 70 | $dump =~ s/\A---\s*//; | ||||||||||||||
| 71 | write_log $dump; | ||||||||||||||
| 72 | } | ||||||||||||||
| 73 | |||||||||||||||
| 74 | 0 | ▶ | sub add { | ||||||||||||
|
Subroutine
add: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 75 | 0 | my ($class, @names) = @_; | |||||||||||||
| 76 | 0 | for my $name (@names) { | |||||||||||||
| 77 | 0 | @plugins = grep { $_ ne $name } @plugins; | |||||||||||||
| 78 | 0 | push @plugins, $name; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | #------------------------------------------------------------------------------ | ||||||||||||||
| 83 | |||||||||||||||
| 84 | 0 | ▶ | sub do_argv { | ||||||||||||
|
Subroutine
do_argv: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 85 | 0 | $data{argv} = join ', ', @ARGV; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 0 | ▶ | sub do_cwd { | ||||||||||||
|
Subroutine
do_cwd: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 89 | 0 | require Cwd; | |||||||||||||
| 90 | 0 | $data{cwd} = Cwd::cwd(); | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 0 | ▶ | sub do_pid { | ||||||||||||
|
Subroutine
do_pid: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 94 | 0 | $data{pid} = $$; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 1 | 1; | |||||||||||||