Scrappy/Logger.pm

filter: Statementstmt 31.3% 21 / 67 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 46.1% 6 / 13 Podpod 100% 7 / 7 totaltotal 23.7% 34 / 143 CC 23 SCAR 50.3
CC
23
Cov
18%
event
306.0
write
20.0
load
12.0
CRAP 152.6
« Scrappy/Scraper/UserAgent.pm ↑ Summary Scrappy/Scraper.pm »
1 # ABSTRACT: Scrappy Scraper Event Logging
2 # Dist::Zilla: +PodWeaver
3  
4 package Scrappy::Logger;
5  
6 10 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 $Scrappy::Logger::VERSION = '0.94112090';
8 }
9  
10 # load OO System
11 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # load other libraries
14 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use DateTime::Format::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 $YAML::Syck::ImplicitTyping = 1;
19  
20 10 has 'auto_save' => (is => 'rw', isa => 'Bool', default => 1);
21 10 has file => (is => 'rw', isa => 'Str');
22 10 has verbose => (is => 'rw', isa => 'Int', default => 0);
23  
24  
25 0 ▶sub load {
Subroutine load: not called CC=3 SCAR=24.8
Pod documented
26 0 my $self = shift;
27 0 my $file = shift;
28  
29 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 0 0 0
30  
31 0 $self->{file} = $file;
32  
33 # load event-log file
34 0 ▶ $self->{stash} = LoadFile($file)
Branch: 1 branch
BranchTrueFalseTotal
$self->{"stash"} = LoadFile($file) or croak("Log file $file does not exist or is not read/writable") 0 0 0
35 or croak("Log file $file does not exist or is not read/writable");
36 }
37  
38 0 return $self->{stash};
39 }
40  
41  
42 0 ▶sub timestamp {
Subroutine timestamp: not called CC=2 SCAR=17.9
Pod documented
43 0 my $self = shift;
44 0 my $date = shift;
45  
46 0 ▶ if ($date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date) { } 0 0 0
47  
48 # $date =~ s/\_/ /g;
49 0 return DateTime::Format::SQLite->parse_datetime($date)
50 ; # datetime object
51 }
52 else {
53 0 $date =
54 DateTime::Format::SQLite->format_datetime(DateTime->now); # string
55  
56 # $date =~ s/ /_/g;
57 0 return $date;
58 }
59 }
60  
61  
62 0 ▶sub info {
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
63 0 return shift->event('info', @_);
64 }
65  
66  
67 0 ▶sub warn {
Subroutine warn: not called CC=1 SCAR=6.9
Pod documented
68 0 return shift->event('warn', @_);
69 }
70  
71  
72 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
73 0 return shift->event('error', @_);
74 }
75  
76  
77 0 ▶sub event {
Subroutine event: not called CC=17 SCAR=57.2
Pod documented
78 0 my $self = shift;
79 0 my $type = shift;
80 0 my $note = shift;
81  
82 0 ▶ croak("Can't record an event without an event-type and notation")
Branch: 1 branch
BranchTrueFalseTotal
unless $type and $note 0 0 0
Condition0%0/3
$type and $note
!ll&&!rl&&r
000
MC/DC0%0/2
$type and $note
$type $note
Truth table0%0/3
A and B
A= $typeB= $note
ABresult
0X0
100
111
83 unless $type && $note;
84  
85 0 ▶ $self->{stash} = {} unless defined $self->{stash};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"stash"} 0 0 0
86  
87 0 ▶ $self->{stash}->{$type} = [] unless defined $self->{stash}->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"stash"}{$type} 0 0 0
88  
89 0 ▶ my $frame = $type eq 'info' || $type eq 'error' || $type eq 'warn' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'info' || $type eq 'error' || $type eq 'warn' ? : 0 0 0
Condition0%0/6
$type eq "info" or $type eq "error" or $type eq "warn"
l!l&&r!l&&!r
000
$type eq "info" or $type eq "error"
l!l&&r!l&&!r
000
MC/DC0%0/3
$type eq "info" or $type eq "error" or $type eq "warn"
$type eq "info" $type eq "error" $type eq "warn"
Truth table0%0/4
A or B or C
A= $type eq "info"B= $type eq "error"C= $type eq "warn"
ABCresult
1XX1
01X1
0011
0000
90 0 my @trace = caller($frame);
91 0 my $entry = scalar @{$self->{stash}->{$type}};
92 0 my $time = $self->timestamp;
93 0 my $data = {};
94 0 ▶ $data = {
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
95 '// package' => $trace[0],
96 '// filename' => $trace[1],
97 '// line' => $trace[2],
98 '// occurred' => $time,
99 '// notation' => $note,
100 }
101 if $self->verbose;
102  
103 $self->{stash}->{$type}->[$entry] = {eventlog => "[$time] [$type] $note"}
104 0 ▶ unless defined $self->{stash}->{$type}->[$entry];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"stash"}{$type}[$entry] 0 0 0
105  
106 $self->{stash}->{$type}->[$entry]->{metadata} = $data
107 0 ▶ if scalar keys %{$data};
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$data;} 0 0 0
108  
109 0 ▶ if (@_ && $self->verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $self->verbose) 0 0 0
Condition0%0/3
@_ and $self->verbose
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and $self->verbose
@_ $self->verbose
Truth table0%0/3
A and B
A= @_B= $self->verbose
ABresult
0X0
100
111
110 0 ▶ my $stash = @_ > 1 ? {@_} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
111 0 ▶ if ($stash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stash) 0 0 0
112 0 ▶ if (ref $stash eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $stash eq "HASH") 0 0 0
113 0 for (keys %{$stash}) {
114 $self->{stash}->{$type}->[$entry]->{metadata}->{$_} =
115 0 $stash->{$_};
116 }
117 }
118 }
119 }
120  
121 0 $self->write;
122 0 return $self->{stash}->{$type}->[$entry];
123 }
124  
125  
126 0 ▶sub write {
Subroutine write: not called CC=4 SCAR=30.0
Pod documented
127 0 my $self = shift;
128 0 ▶ my $file = shift || $self->{file};
Condition0%0/3
shift() || $self->{'file'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->{'file'}
shift() $self->{'file'}
Truth table0%0/3
A || B
A= shift()B= $self->{'file'}
ABresult
1X1
011
000
129  
130 0 $self->{file} = $file;
131  
132 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 0 0 0
133  
134 # write event-log file
135 DumpFile($file, $self->{stash})
136 0 ▶ or
Branch: 1 branch
BranchTrueFalseTotal
DumpFile($file, $self->{"stash"}) or croak("event-log file $file does not exist or is not read/writable") 0 0 0
137 croak("event-log file $file does not exist or is not read/writable");
138 }
139  
140 0 return $self->{stash};
141 }
142  
143 10 1;
144  
145 __END__
« Scrappy/Scraper/UserAgent.pm ↑ Summary Scrappy/Scraper.pm »