| 1 | 1 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Class::Scaffold::Log; | ||||||||||||||||||||||||||||
| 6 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | $Class::Scaffold::Log::VERSION = '1.102280'; | |||||||||||||||||||||||||||
| 8 | } | ||||||||||||||||||||||||||||
| 9 | # ABSTRACT: Logging utilities | ||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Time::HiRes 'gettimeofday'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use parent 'Class::Scaffold::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | __PACKAGE__->mk_singleton(qw(instance)) | |||||||||||||||||||||||||||
| 15 | ->mk_scalar_accessors(qw(filename max_level)) | ||||||||||||||||||||||||||||
| 16 | ->mk_boolean_accessors(qw(pid timestamp))->mk_concat_accessors(qw(output)); | ||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use constant DEFAULTS => (max_level => 1,); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 21 | 0 | $self->SUPER::init(@_); | |||||||||||||||||||||||||||
| 22 | 0 | $self->clear_pid; | |||||||||||||||||||||||||||
| 23 | 0 | $self->set_timestamp; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub precdate { | ||||||||||||||||||||||||||
|
Subroutine
precdate: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 0 | my @hires = gettimeofday; | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 29 | 0 | sprintf "%04d%02d%02d.%02d%02d%02d", | |||||||||||||||||||||||||||
| 30 | $_[5] + 1900, $_[4] + 1, @_[ 3, 2, 1, 0 ]; | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | ->(localtime($hires[0])) . (@_ ? sprintf(".%06d", $hires[1]) : ""); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub logdate { substr(precdate(1), 0, 18) } | ||||||||||||||||||||||||||
|
Subroutine
logdate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # like get_set_std, but also generate handle from filename unless defined | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub handle { | ||||||||||||||||||||||||||
|
Subroutine
handle: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | $self = Class::Scaffold::Log->instance unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # in test mode, ignore what we're given - always log to STDOUT. | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ($self->delegate->test_mode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | return $self->{handle} ||= IO::File->new(">&STDOUT") | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"handle"} ||= "IO::File"->new(">&STDOUT")
MC/DC0%0/2
$self->{"handle"} ||= "IO::File"->new(">&STDOUT")
$self->{"handle"}
"IO::File"->new(">&STDOUT")
Truth table0%0/3
A ||= B
A= $self->{"handle"}B= "IO::File"->new(">&STDOUT")
| |||||||||||||||||||||||||||||
| 44 | or die "can't open STDOUT: $!\n"; | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | $self->{handle} = shift; | |||||||||||||||||||||||||||
| 48 | } else { | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ($self->filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $self->{handle} ||= IO::File->new(sprintf(">>%s", $self->filename)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"handle"} ||= "IO::File"->new(sprintf(">>%s", $self->filename))
MC/DC0%0/2
$self->{"handle"} ||= "IO::File"->new(sprintf(">>%s", $self->filename))
$self->{"handle"}
"IO::File"->new(sprintf(">>%s", $self->filename))
Truth table0%0/3
A ||= B
A= $self->{"handle"}B= "IO::File"->new(sprintf(">>%s", $self->filename))
| |||||||||||||||||||||||||||||
| 51 | or die sprintf("can't append to %s: %s\n", $self->filename, $!); | ||||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $self->{handle} ||= IO::File->new(">&STDERR") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"handle"} ||= "IO::File"->new(">&STDERR")
MC/DC0%0/2
$self->{"handle"} ||= "IO::File"->new(">&STDERR")
$self->{"handle"}
"IO::File"->new(">&STDERR")
Truth table0%0/3
A ||= B
A= $self->{"handle"}B= "IO::File"->new(">&STDERR")
| |||||||||||||||||||||||||||||
| 54 | or die "can't open STDERR: $!\n"; | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 0 | $self->{handle}->autoflush(1); | |||||||||||||||||||||||||||
| 57 | 0 | return $self->{handle}; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # called like printf | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub __log { | ||||||||||||||||||||||||||
|
Subroutine
__log: not called CC=14 SCAR=53.5
| |||||||||||||||||||||||||||||
| 63 | 0 | my ($self, $level, $format, @args) = @_; | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | $self = Class::Scaffold::Log->instance unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Check for max_level before stringifying $format so we don't | ||||||||||||||||||||||||||||
| 67 | # unnecessarily trigger a potentially lazy string. | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | return if $level > $self->max_level; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # in case someone passes us an object that needs to be stringified so we | ||||||||||||||||||||||||||||
| 71 | # can compare it with 'ne' further down (e.g., an exception object): | ||||||||||||||||||||||||||||
| 72 | 0 | $format = "$format"; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | return unless defined $format and $format ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $format and $format ne ""
MC/DC0%0/2
defined $format and $format ne ""
defined $format
$format ne ""
Truth table0%0/3
A and B
A= defined $formatB= $format ne ""
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # make sure there's exactly one newline at the end | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | 1 while chomp $format; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | $format .= "\n"; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | $format = sprintf "(%08d) %s", $$, $format if $self->pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $format = sprintf "%s %s", $self->logdate, $format if $self->timestamp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | my $msg = sprintf $format => @args; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Open and close the file for each line that is logged. That doesn't cost | ||||||||||||||||||||||||||||
| 83 | # much and makes it possible to move the file away for backup, rotation | ||||||||||||||||||||||||||||
| 84 | # or whatver. | ||||||||||||||||||||||||||||
| 85 | 0 | my $fh; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ($self->delegate->test_mode) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 87 | 0 | print $msg; | |||||||||||||||||||||||||||
| 88 | ▶ | } elsif (defined($self->filename) && length($self->filename)) { | |||||||||||||||||||||||||||
|
Condition0%0/3
defined $self->filename and length $self->filename
MC/DC0%0/2
defined $self->filename and length $self->filename
defined $self->filename
length $self->filename
Truth table0%0/3
A and B
A= defined $self->filenameB= length $self->filename
| |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | open $fh, '>>', $self->filename | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | or die sprintf "can't open %s for appending: %s", $self->filename, $!; | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | print $fh $msg | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | or die sprintf "can't print to %s: %s", $self->filename, $!; | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | close $fh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | or die sprintf "can't close %s: %s", $self->filename, $!; | ||||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||
| 96 | 0 | warn $msg; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 0 | $self->output($msg); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub info { | ||||||||||||||||||||||||||
|
Subroutine
info: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 103 | 0 | $self->__log(1, @_); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub debug { | ||||||||||||||||||||||||||
|
Subroutine
debug: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | 0 | $self->__log(2, @_); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub deep_debug { | ||||||||||||||||||||||||||
|
Subroutine
deep_debug: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 113 | 0 | $self->__log(3, @_); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # log a final message, close the log and croak. | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub fatal { | ||||||||||||||||||||||||||
|
Subroutine
fatal: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 0 | my ($self, $format, @args) = @_; | |||||||||||||||||||||||||||
| 119 | 0 | my $message = sprintf($format, @args); | |||||||||||||||||||||||||||
| 120 | 0 | $self->info($message); | |||||||||||||||||||||||||||
| 121 | 0 | croak($message); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 1 | 1; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||||||||||