File Coverage

blib/lib/Log/Report/Dispatcher/Syslog.pm
Criterion Covered Total %
statement 67 69 97.1
branch 7 14 50.0
condition 7 16 43.7
subroutine 15 15 100.0
pod 4 5 80.0
total 100 119 84.0


line stmt bran cond sub pod time code
1             # Copyrights 2007-2017 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5 1     1   10415 use warnings;
  1         2  
  1         30  
6 1     1   4 use strict;
  1         2  
  1         26  
7              
8             package Log::Report::Dispatcher::Syslog;
9 1     1   4 use vars '$VERSION';
  1         1  
  1         42  
10             $VERSION = '1.22';
11              
12 1     1   5 use base 'Log::Report::Dispatcher';
  1         1  
  1         96  
13              
14 1     1   5 use Log::Report 'log-report';
  1         2  
  1         5  
15              
16 1     1   6 use Sys::Syslog qw/:standard :extended :macros/;
  1         1  
  1         223  
17 1     1   7 use Log::Report::Util qw/@reasons expand_reasons/;
  1         1  
  1         71  
18 1     1   6 use Encode qw/encode/;
  1         2  
  1         35  
19              
20 1     1   5 use File::Basename qw/basename/;
  1         2  
  1         594  
21              
22             my %default_reasonToPrio =
23             ( TRACE => LOG_DEBUG
24             , ASSERT => LOG_DEBUG
25             , INFO => LOG_INFO
26             , NOTICE => LOG_NOTICE
27             , WARNING => LOG_WARNING
28             , MISTAKE => LOG_WARNING
29             , ERROR => LOG_ERR
30             , FAULT => LOG_ERR
31             , ALERT => LOG_ALERT
32             , FAILURE => LOG_EMERG
33             , PANIC => LOG_CRIT
34             );
35              
36             @reasons==keys %default_reasonToPrio
37             or panic __"not all reasons have a default translation";
38              
39              
40             my $active;
41              
42             sub init($)
43 1     1 0 3 { my ($self, $args) = @_;
44 1   50     8 $args->{format_reason} ||= 'IGNORE';
45              
46 1         7 $self->SUPER::init($args);
47              
48 1 50       3 error __x"max one active syslog dispatcher, attempt for {new} have {old}"
49             , new => $self->name, old => $active
50             if $active;
51 1         5 $active = $self->name;
52              
53             setlogsock(delete $args->{logsocket})
54 1 50       4 if $args->{logsocket};
55              
56 1   33     47 my $ident = delete $args->{identity} || basename $0;
57 1   50     6 my $flags = delete $args->{flags} || 'pid,nowait';
58 1   50     6 my $fac = delete $args->{facility} || 'user';
59 1         13 openlog $ident, $flags, $fac; # doesn't produce error.
60              
61 1         25 $self->{LRDS_incl_dom} = delete $args->{include_domain};
62 1   50     5 $self->{LRDS_charset} = delete $args->{charset} || "utf-8";
63 1   50 1   5 $self->{LRDS_format} = $args->{format} || sub {$_[0]};
  1         3  
64              
65 1         8 $self->{prio} = +{ %default_reasonToPrio };
66 1 50       4 if(my $to_prio = delete $args->{to_prio})
67 1         2 { my @to = @$to_prio;
68 1         3 while(@to)
69 1         3 { my ($reasons, $level) = splice @to, 0, 2;
70 1         3 my @reasons = expand_reasons $reasons;
71              
72 1         32 my $prio = Sys::Syslog::xlate($level);
73 1 50       37 error __x"syslog level '{level}' not understood", level => $level
74             if $prio eq -1;
75              
76 1         6 $self->{prio}{$_} = $prio for @reasons;
77             }
78             }
79              
80 1         4 $self;
81             }
82              
83             sub close()
84 1     1 1 2 { my $self = shift;
85 1         2 undef $active;
86 1         3 closelog;
87              
88 1         27 $self->SUPER::close;
89             }
90              
91             #--------------
92              
93             sub format(;$)
94 1     1 1 2 { my $self = shift;
95 1 50       3 @_ ? $self->{LRDS_format} = shift : $self->{LRDS_format};
96             }
97              
98             #--------------
99              
100             sub log($$$$$)
101 1     1 1 3 { my ($self, $opts, $reason, $msg, $domain) = @_;
102 1 50       6 my $text = $self->translate($opts, $reason, $msg) or return;
103 1         3 my $format = $self->format;
104              
105             # handle each line in message separately
106 1         6 $text =~ s/\s+$//s;
107 1         4 my @text = split /\n/, $format->($text, $domain, $msg, %$opts);
108              
109 1         6 my $prio = $self->reasonToPrio($reason);
110 1         2 my $charset = $self->{LRDS_charset};
111              
112 1 50 33     4 if($self->{LRDS_incl_dom} && $domain)
113 0         0 { $domain =~ s/\%//g; # security
114 0         0 syslog $prio, "$domain %s", encode($charset, shift @text);
115             }
116              
117             syslog $prio, "%s", encode($charset, $_)
118 1         6 for @text;
119             }
120              
121              
122 1     1 1 3 sub reasonToPrio($) { $_[0]->{prio}{$_[1]} }
123              
124             1;