| 1 | # This code is part of Perl distribution Log-Report version 1.46. | ||||||||||||||||||||||||||||||||||||||
| 2 | # The POD got stripped from this file by OODoc version 3.06. | ||||||||||||||||||||||||||||||||||||||
| 3 | # For contributors see file ChangeLog. | ||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||
| 5 | # This software is copyright (c) 2007-2026 by Mark Overmeer. | ||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||
| 8 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||
| 9 | # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later | ||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||
| 12 | package Log::Report::Dispatcher::File;{ | ||||||||||||||||||||||||||||||||||||||
| 13 | 4 | our $VERSION = '1.46'; | |||||||||||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use base 'Log::Report::Dispatcher'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use Log::Report 'log-report', import => [ qw/__x error fault trace/ ]; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use POSIX qw/strftime/; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 24 | 4 | ▶ | use Encode qw/find_encoding/; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | use Fcntl qw/:flock/; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | #-------------------- | ||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||
| 29 | sub init($) | ||||||||||||||||||||||||||||||||||||||
| 30 | 4 | ▶ | { my ($self, $args) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=14 SCAR=38.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | if(!$args->{charset}) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 33 | 4 | ▶ | { my $lc = $ENV{LC_CTYPE} || $ENV{LC_ALL} || $ENV{LANG} || ''; | ||||||||||||||||||||||||||||||||||||
|
Condition37%3/8
$ENV{'LC_CTYPE'} || $ENV{'LC_ALL'} || $ENV{'LANG'} || ''
$ENV{"LC_CTYPE"} or $ENV{"LC_ALL"} or $ENV{"LANG"}
$ENV{"LC_CTYPE"} or $ENV{"LC_ALL"}
MC/DC0%0/3
$ENV{'LC_CTYPE'} || $ENV{'LC_ALL'} || $ENV{'LANG'} || ''
$ENV{"LC_CTYPE"}
$ENV{"LC_ALL"}
$ENV{"LANG"}
Truth table25%1/4
A or B or C
A= $ENV{"LC_CTYPE"}B= $ENV{"LC_ALL"}C= $ENV{"LANG"}
| |||||||||||||||||||||||||||||||||||||||
| 34 | 4 | ▶ | my $cs = $lc =~ m/\.([\w-]+)/ ? $1 : ''; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 35 | 4 | ▶ | $args->{charset} = length $cs && find_encoding $cs ? $cs : undef; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $cs && &find_encoding(scalar($cs ? $cs : undef))
MC/DC0%0/2
length $cs && &find_encoding(scalar($cs ? $cs : undef))
length $cs
&find_encoding(scalar($cs ? $cs : undef))
Truth table33%1/3
A && B
A= length $csB= &find_encoding(scalar($cs ? $cs : undef))
| |||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||
| 38 | 4 | $self->SUPER::init($args); | |||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||
| 40 | 4 | my $name = $self->name; | |||||||||||||||||||||||||||||||||||||
| 41 | 4 | ▶ | $self->{to} = $args->{to} or error __x"dispatcher {name} needs parameter 'to'", name => $name; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 42 | 4 | ▶ | $self->{replace} = $args->{replace} || 0; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{'replace'} || 0
MC/DC0%0/1
$args->{'replace'} || 0
$args->{'replace'}
Truth table50%1/2
A
A= $args->{'replace'}
| |||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | my $format = $args->{format} || sub { '['.localtime()."] $_[0]" }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$args->{'format'} || sub {
'[' . (localtime()) . "] $_[0]";
}
MC/DC0%0/2
$args->{'format'} || sub {
'[' . (localtime()) . "] $_[0]";
}
$args->{'format'}
sub {
'[' . (localtime()) . "] $_[0]";
}
Truth table33%1/3
A || B
A= $args->{'format'}B= sub {
'[' . (localtime()) . "] $_[0]";
}
| |||||||||||||||||||||||||||||||||||||||
| 45 | $self->{LRDF_format} | ||||||||||||||||||||||||||||||||||||||
| 46 | = ref $format eq 'CODE' ? $format | ||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | : $format eq 'LONG' ? sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||
| 48 | 0 | my $msg = shift; | |||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my $domain = shift || '-'; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || '-'
MC/DC0%0/1
shift() || '-'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my $stamp = strftime "%Y-%m-%dT%H:%M:%S", gmtime; | |||||||||||||||||||||||||||||||||||||
| 51 | 0 | "[$stamp $$] $domain $msg"; | |||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | : error __x"unknown format parameter `{what}'", what => ref $format || $format; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
ref $format || $format
MC/DC0%0/2
ref $format || $format
ref $format
$format
Truth table0%0/3
A || B
A= ref $formatB= $format
| |||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||
| 55 | 4 | $self; | |||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||
| 58 | #-------------------- | ||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | sub filename() { $_[0]->{LRDF_filename} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
filename: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 61 | 9 | ▶ | sub format() { $_[0]->{LRDF_format} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
format: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||
| 64 | sub output($) | ||||||||||||||||||||||||||||||||||||||
| 65 | 9 | ▶ | { # fast simple case | ||||||||||||||||||||||||||||||||||||
|
Subroutine
output: called CC=7 SCAR=30.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 66 | 9 | ▶ | return $_[0]->{LRDF_output} if $_[0]->{LRDF_output}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||
| 68 | 4 | my ($self, $msg) = @_; | |||||||||||||||||||||||||||||||||||||
| 69 | 4 | my $name = $self->name; | |||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||
| 71 | 4 | my $to = $self->{to}; | |||||||||||||||||||||||||||||||||||||
| 72 | 4 | ▶ | unless(ref $to) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 73 | { # constant file name | ||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $self->{LRDF_filename} = $to; | |||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my $binmode = $self->{replace} ? '>' : '>>'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | 0 | open my $f, "$binmode:raw", $to; | |||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | unless($f) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 79 | { # avoid logging error to myself (issue #4) | ||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $msg = __x"cannot write log into {file} with mode '{binmode}'", binmode => $binmode, file => $to; | |||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if(my @disp = grep $_->name ne $name, Log::Report::dispatcher('list')) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 82 | 0 | { $msg->to($disp[0]->name); | |||||||||||||||||||||||||||||||||||||
| 83 | 0 | error $msg; | |||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||
| 85 | else | ||||||||||||||||||||||||||||||||||||||
| 86 | 0 | { die $msg; | |||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $f->autoflush; | |||||||||||||||||||||||||||||||||||||
| 91 | 0 | return $self->{LRDF_output} = $f; | |||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||
| 94 | 4 | ▶ | if(ref $to eq 'CODE') | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 95 | { # variable filename | ||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $fn = $self->{LRDF_filename} = $to->($self, $msg); | |||||||||||||||||||||||||||||||||||||
| 97 | 0 | return $self->{LRDF_output} = $self->{LRDF_out}{$fn}; | |||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||
| 100 | # probably file-handle | ||||||||||||||||||||||||||||||||||||||
| 101 | 4 | $self->{LRDF_output} = $to; | |||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | #-------------------- | ||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||
| 107 | sub rotate($) | ||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | { my ($self, $old) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
rotate: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||
| 110 | 0 | my $to = $self->{to}; | |||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | my $logs = ref $to eq 'CODE' ? $self->{LRDF_out} : +{ $self->{to} => $self->{LRDF_output} }; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | while(my ($log, $fh) = each %$logs) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | { ! ref $log | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 115 | or error __x"cannot rotate log file which was opened as file-handle"; | ||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | my $oldfn = ref $old eq 'CODE' ? $old->($log) : $old; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 118 | 0 | trace "rotating $log to $oldfn"; | |||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | rename $log, $oldfn | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 121 | or fault __x"unable to rotate logfile {fn} to {oldfn}", fn => $log, oldfn => $oldfn; | ||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $fh->close; # close after move not possible on Windows? | |||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | open my $f, '>>:raw', $log | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 126 | or fault __x"cannot write log into {file}", file => $log; | ||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $self->{LRDF_output} = $logs->{$log} = $f; | |||||||||||||||||||||||||||||||||||||
| 129 | 0 | $f->autoflush; | |||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||
| 132 | 0 | $self; | |||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||
| 135 | #-------------------- | ||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||
| 137 | sub log($$$$) | ||||||||||||||||||||||||||||||||||||||
| 138 | 9 | ▶ | { my ($self, $opts, $reason, $msg, $domain) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
log: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 139 | 9 | my $trans = $self->translate($opts, $reason, $msg); | |||||||||||||||||||||||||||||||||||||
| 140 | 9 | my $text = $self->format->($trans, $domain, $msg, %$opts); | |||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||
| 142 | 9 | my $out = $self->output($msg); | |||||||||||||||||||||||||||||||||||||
| 143 | 9 | flock $out, LOCK_EX; | |||||||||||||||||||||||||||||||||||||
| 144 | 9 | $out->print($text); | |||||||||||||||||||||||||||||||||||||
| 145 | 9 | flock $out, LOCK_UN; | |||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||
| 149 | sub close() | ||||||||||||||||||||||||||||||||||||||
| 150 | 4 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
close: called CC=6 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 151 | 4 | ▶ | $self->SUPER::close | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 152 | or return; | ||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||
| 154 | 4 | my $to = $self->{to}; | |||||||||||||||||||||||||||||||||||||
| 155 | my @fh_to_close | ||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | = ref $to eq 'CODE' ? values %{$self->{LRDF_out}} | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 157 | : $self->filename ? $self->{LRDF_output} | ||||||||||||||||||||||||||||||||||||||
| 158 | 4 | ▶ | : (); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||
| 160 | 4 | ▶ | $_ && $_->close for @fh_to_close; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 161 | 4 | $self; | |||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||
| 164 | 4 | 1; | |||||||||||||||||||||||||||||||||||||