| 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::Syslog;{ | ||||||||||||||||||||||||||||
| 13 | 1 | our $VERSION = '1.46'; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use base 'Log::Report::Dispatcher'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use Log::Report 'log-report', import => [ qw/__ __x error panic/ ]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Sys::Syslog qw/:standard :extended :macros/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use Log::Report::Util qw/@reasons expand_reasons/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use Encode qw/encode/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use File::Basename qw/basename/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | my %default_reasonToPrio = ( | |||||||||||||||||||||||||||
| 30 | TRACE => LOG_DEBUG, | ||||||||||||||||||||||||||||
| 31 | ASSERT => LOG_DEBUG, | ||||||||||||||||||||||||||||
| 32 | INFO => LOG_INFO, | ||||||||||||||||||||||||||||
| 33 | NOTICE => LOG_NOTICE, | ||||||||||||||||||||||||||||
| 34 | WARNING => LOG_WARNING, | ||||||||||||||||||||||||||||
| 35 | MISTAKE => LOG_WARNING, | ||||||||||||||||||||||||||||
| 36 | ERROR => LOG_ERR, | ||||||||||||||||||||||||||||
| 37 | FAULT => LOG_ERR, | ||||||||||||||||||||||||||||
| 38 | ALERT => LOG_ALERT, | ||||||||||||||||||||||||||||
| 39 | FAILURE => LOG_EMERG, | ||||||||||||||||||||||||||||
| 40 | PANIC => LOG_CRIT, | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | @reasons==keys %default_reasonToPrio | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | or panic __"not all reasons have a default translation"; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | #-------------------- | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | my $active; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub init($) | ||||||||||||||||||||||||||||
| 51 | 1 | ▶ | { my ($self, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=13 SCAR=27.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | $args->{format_reason} ||= 'IGNORE'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"format_reason"} ||= "IGNORE"
MC/DC0%0/1
$args->{"format_reason"} ||= "IGNORE"
$args->{"format_reason"}
Truth table50%1/2
A
A= $args->{"format_reason"}
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | $self->SUPER::init($args); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | ▶ | ! $active | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | or error __x"max one active syslog dispatcher, attempt for {new} have {old}", new => $self->name, old => $active; | ||||||||||||||||||||||||||||
| 58 | 1 | $active = $self->name; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | setlogsock(delete $args->{logsocket}) | ||||||||||||||||||||||||||||
| 61 | 1 | ▶ | if $args->{logsocket}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | my $ident = delete $args->{identity} || basename $0; | ||||||||||||||||||||||||||
|
Condition33%1/3
delete $args->{'identity'} || basename($0)
MC/DC0%0/2
delete $args->{'identity'} || basename($0)
delete $args->{'identity'}
basename($0)
Truth table33%1/3
A || B
A= delete $args->{'identity'}B= basename($0)
| |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | my $flags = delete $args->{flags} || 'pid,nowait'; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $args->{'flags'} || 'pid,nowait'
MC/DC0%0/1
delete $args->{'flags'} || 'pid,nowait'
delete $args->{'flags'}
Truth table50%1/2
A
A= delete $args->{'flags'}
| |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | my $fac = delete $args->{facility} || 'user'; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $args->{'facility'} || 'user'
MC/DC0%0/1
delete $args->{'facility'} || 'user'
delete $args->{'facility'}
Truth table50%1/2
A
A= delete $args->{'facility'}
| |||||||||||||||||||||||||||||
| 66 | 1 | openlog $ident, $flags, $fac; # doesn't produce error. | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1 | $self->{LRDS_incl_dom} = delete $args->{include_domain}; | |||||||||||||||||||||||||||
| 69 | 1 | ▶ | $self->{LRDS_charset} = delete $args->{charset} || "utf-8"; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $args->{'charset'} || 'utf-8'
MC/DC0%0/1
delete $args->{'charset'} || 'utf-8'
delete $args->{'charset'}
Truth table50%1/2
A
A= delete $args->{'charset'}
| |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | $self->{LRDS_format} = $args->{format} || sub {$_[0]}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$args->{'format'} || sub {
$_[0];
}
MC/DC0%0/2
$args->{'format'} || sub {
$_[0];
}
$args->{'format'}
sub {
$_[0];
}
Truth table33%1/3
A || B
A= $args->{'format'}B= sub {
$_[0];
}
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | $self->{prio} = +{ %default_reasonToPrio }; | |||||||||||||||||||||||||||
| 73 | 1 | ▶ | if(my $to_prio = delete $args->{to_prio}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | { my @to = @$to_prio; | |||||||||||||||||||||||||||
| 75 | 1 | ▶ | while(@to) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | { my ($reasons, $level) = splice @to, 0, 2; | |||||||||||||||||||||||||||
| 77 | 1 | my @reasons = expand_reasons $reasons; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | my $prio = Sys::Syslog::xlate($level); | |||||||||||||||||||||||||||
| 80 | 1 | ▶ | $prio != -1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | or error __x"syslog level '{level}' not understood", level => $level; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | $self->{prio}{$_} = $prio for @reasons; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | $self; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub close() | ||||||||||||||||||||||||||||
| 91 | 1 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 1 | undef $active; | |||||||||||||||||||||||||||
| 93 | 1 | closelog; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | $self->SUPER::close; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | #-------------------- | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub format(;$) | ||||||||||||||||||||||||||||
| 101 | 1 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
format: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 1 | ▶ | @_ ? $self->{LRDS_format} = shift : $self->{LRDS_format}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | #-------------------- | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub log($$$$$) | ||||||||||||||||||||||||||||
| 108 | 1 | ▶ | { my ($self, $opts, $reason, $msg, $domain) = @_; | ||||||||||||||||||||||||||
|
Subroutine
log: called CC=5 SCAR=17.8
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 1 | ▶ | my $text = $self->translate($opts, $reason, $msg) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 1 | my $format = $self->format; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # handle each line in message separately | ||||||||||||||||||||||||||||
| 113 | 1 | $text =~ s/\s+$//s; | |||||||||||||||||||||||||||
| 114 | 1 | my @text = split /\n/, $format->($text, $domain, $msg, %$opts); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 1 | my $prio = $self->reasonToPrio($reason); | |||||||||||||||||||||||||||
| 117 | 1 | my $charset = $self->{LRDS_charset}; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | ▶ | if($self->{LRDS_incl_dom} && $domain) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"LRDS_incl_dom"} and $domain
MC/DC0%0/2
$self->{"LRDS_incl_dom"} and $domain
$self->{"LRDS_incl_dom"}
$domain
Truth table33%1/3
A and B
A= $self->{"LRDS_incl_dom"}B= $domain
| |||||||||||||||||||||||||||||
| 120 | 0 | { $domain =~ s/\%//g; # security | |||||||||||||||||||||||||||
| 121 | 0 | syslog $prio, "$domain %s", encode($charset, shift @text); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | syslog $prio, "%s", encode($charset, $_) | ||||||||||||||||||||||||||||
| 125 | 1 | for @text; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | ▶ | sub reasonToPrio($) { $_[0]->{prio}{$_[1]} } | ||||||||||||||||||||||||||
|
Subroutine
reasonToPrio: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | 1; | |||||||||||||||||||||||||||