| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | $self->{file} = $file; | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | # load event-log file | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | $self->{stash} = LoadFile($file) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$type and $note
MC/DC0%0/2
$type and $note
$type
$note
Truth table0%0/3
A and B
A= $typeB= $note
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | unless $type && $note; | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $self->{stash} = {} unless defined $self->{stash}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | $self->{stash}->{$type} = [] unless defined $self->{stash}->{$type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | my $frame = $type eq 'info' || $type eq 'error' || $type eq 'warn' ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$type eq "info" or $type eq "error" or $type eq "warn"
$type eq "info" or $type eq "error"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | $self->{stash}->{$type}->[$entry]->{metadata} = $data | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if scalar keys %{$data}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if (@_ && $self->verbose) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ and $self->verbose
MC/DC0%0/2
@_ and $self->verbose
@_
$self->verbose
Truth table0%0/3
A and B
A= @_B= $self->verbose
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my $stash = @_ > 1 ? {@_} : $_[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ($stash) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if (ref $stash eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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'}
MC/DC0%0/2
shift() || $self->{'file'}
shift()
$self->{'file'}
Truth table0%0/3
A || B
A= shift()B= $self->{'file'}
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | $self->{file} = $file; | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ($file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | # write event-log file | ||||||||||||||||||||||||||||||||||||||||||
| 135 | DumpFile($file, $self->{stash}) | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||