blib/lib/Tie/Syslog.pm

filter: Statementstmt 85.7% 102 / 119 Branchbran 51.9% 27 / 52 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.8% 18 / 22 Podpod 100% 1 / 1 totaltotal 74.8% 149 / 199 CC 30 SCAR 16.0
CC
30
Cov
75%
AUTOLOAD
13.6
FILENO
12.0
_parse_config
8.0
CRAP 5.0
↑ Summary
1 package Tie::Syslog;
2  
3 3 $Tie::Syslog::VERSION = '2.04.03';
4  
5 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp qw/carp croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Sys::Syslog qw/:standard :macros/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ------------------------------------------------------------------------------
12 # Define all default handle-tying subs, so that they can be autoloaded if
13 # necessary.
14 3 ▶use subs qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 TIEHANDLE
16 WRITE
17 PRINT
18 PRINTF
19 READ
20 READLINE
21 GETC
22 CLOSE
23 OPEN
24 BINMODE
25 EOF
26 TELL
27 SEEK
28 UNTIE
29 FILENO
30 3 );
31  
32 # --- 'Public' Globals - DEFAULTS ----------------------------------------------
33 3 $Tie::Syslog::ident = (split '/', $0)[-1];
34 3 $Tie::Syslog::logopt = 'pid,ndelay';
35  
36 # --- 'Private' Globals --------------------------------------------------------
37 3 my @mandatory_opts = ('facility', 'priority');
38 # Since calling openlog() twice for the same facility makes it croak, we will
39 # keep a list of already-open-facility-connections. This will also be useful to
40 # know if we must call closelog().
41 3 my %open_connections;
42  
43  
44 # ------------------------------------------------------------------------------
45 # 'Private' functions
46 # ------------------------------------------------------------------------------
47  
48 ########################
49 # sub _parse_config(@) #
50 ########################
51 # This sub is responsible of setting up the configuration for the subsequent
52 # openlog() and syslog() calls. It returns a reference to a hash that contains
53 # configuration parameters for our Tie::Syslog object.
54 4 ▶sub _parse_config(@) {
Subroutine _parse_config: called CC=8 SCAR=20.8
55  
56 4 my $params;
57  
58 4 ▶ if (ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 2 2 4
59 # New-style configuration
60 # Copy values so we don't risk changing an existing reference.
61 # NOTE: this configuration has no defaults defined. 'priority' and
62 # 'facility' must be explicitly set.
63 $params = {
64 2 %{ shift() },
65 };
66 } else {
67 # Old-style configuration, parameters are:
68 # 'facility.loglevel', 'identity', 'logopt', 'setlogsock options'
69 # Old-style config provided local0.error as default, in case nothing
70 # else was specified. We keep this defaults only for old-style config.
71 2 ▶ my ($facility, $priority) =
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 1 2
72 @_ ? split '\.', shift : ('LOG_LOCAL0', 'LOG_ERR');
73 2 ▶ $Tie::Syslog::ident = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
74 2 ▶ $Tie::Syslog::logopt = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
75 # There can still be one option: socket type for setlogsock. Since we
76 # do not call setlogsock (according to Sys::Syslog rules), we
77 # may (safely?) ignore this option.
78 2 $params = {
79 facility => $facility,
80 priority => $priority,
81 };
82 }
83  
84 # Normalize names
85 4 for ('facility', 'priority') {
86 8 ▶ next unless $params->{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{$_} 2 6 8
87 6 $params->{ $_ } = uc( $params->{ $_ } );
88 6 $params->{ $_ } =~ s/EMERGENCY/EMERG/;
89 6 $params->{ $_ } =~ s/ERROR/ERR/;
90 6 $params->{ $_ } =~ s/CRITICAL/CRIT/;
91 $params->{ $_ } = 'LOG_' . $params->{ $_ }
92 6 ▶ unless $params->{ $_ } =~ /^LOG_/;
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{$_} =~ /^LOG_/ 2 4 6
93 }
94  
95 4 return $params;
96 }
97  
98 # ------------------------------------------------------------------------------
99 # Accessors/mutators
100 # ------------------------------------------------------------------------------
101  
102 # Because writing $self->facility() is better than writing $self->{'facility'}
103 3 for my $opt (@mandatory_opts) {
104 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
105 27 ▶ *$opt = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
106 27 my $self = shift;
107 27 ▶ $self->{$opt} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 27 27
108 27 return $self->{$opt};
109 6 };
110 }
111  
112 # ------------------------------------------------------------------------------
113 # Handle tying methods - see 'perldoc perltie' and 'perldoc Tie::Handle'
114 # ------------------------------------------------------------------------------
115  
116 # This is the method called by the 'tie' function.
117 # It returns a hash reference blessed to Tie::Syslog package.
118 4 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: called CC=5 SCAR=16.7
119 4 my $self;
120  
121 # Set log mask as permissive as possible - masking will be done at syslog
122 # level
123 4 eval {
124 4 setlogmask(
125 LOG_MASK(LOG_EMERG)|
126 LOG_MASK(LOG_ALERT)|
127 LOG_MASK(LOG_CRIT)|
128 LOG_MASK(LOG_ERR)|
129 LOG_MASK(LOG_WARNING)|
130 LOG_MASK(LOG_NOTICE)|
131 LOG_MASK(LOG_INFO)|
132 LOG_MASK(LOG_DEBUG)
133 );
134 };
135 4 ▶ carp "Call to setlogmask() failed: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
136 if $@;
137  
138 # See if we were called as an instance method, or as a class method.
139 # In the first case, we provide a copy-constructor that takes the invocant
140 # as a prototype and uses the same configuration.
141 4 ▶ if (my $pkg = ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pkg = ref $_[0]) { } 0 4 4
142 # Use a copy-constructor, providing support for
143 # single-parameter-override via the @_
144 0 my $prototype = shift;
145 0 my $other_parameters = _parse_config @_;
146 0 $self = bless {
147 %$prototype,
148 %$other_parameters,
149 }, $pkg;
150 } else {
151 # Called as a class method
152 4 $pkg = shift;
153
154 4 my $parameters = _parse_config @_;
155  
156 4 $self = bless $parameters, $pkg;
157 }
158  
159 # Check for all mandatory values
160 4 for (@mandatory_opts) {
161 croak "You must provide value for '$_' option"
162 7 ▶ unless $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$_} 1 6 7
163 }
164  
165 # Now openlog() if needed, by calling our own open()
166 3 $self->OPEN();
167  
168 # Finally return
169 3 return $self;
170 }
171  
172 5 ▶sub OPEN {
Subroutine OPEN: called CC=3 SCAR=11.2
173 # Ignore any parameter passed, since we just call openlog() with parameters
174 # got from initialization
175 5 my $self = shift;
176 5 my $f = $self->facility;
177 5 eval {
178 openlog($Tie::Syslog::ident, $Tie::Syslog::logopt, $self->facility)
179 5 ▶ unless $open_connections{ $f };
Branch: 1 branch
BranchTrueFalseTotal
unless $open_connections{$f} 5 0 5
180 };
181 5 ▶ croak "openlog() failed with errors: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
182 if $@;
183 5 $open_connections{ $f } = 1;
184 5 return $self->{'is_open'} = 1;
185 }
186  
187 # Usually, we should have just one connection to syslog. It may happen, though,
188 # that multiple connections have been established, if multiple facilities have
189 # been used (but please NOTE that this is AGAINST Sys::Syslog rules).
190 # In the latter case, closelog() will just close the last connection, which may
191 # be completely unrelated to the handle we're closing here. In case of multiple
192 # connections, just skip closelog().
193 9 ▶sub CLOSE {
Subroutine CLOSE: called CC=4 SCAR=14.0
194 9 my $self = shift;
195 9 ▶ return 1 unless $self->{'is_open'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"is_open"} 4 5 9
196 5 $self->{'is_open'} = 0;
197 5 ▶ unless (scalar(keys(%open_connections)) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %open_connections > 1) 5 0 5
198 5 eval {
199 5 closelog();
200 };
201 5 ▶ croak "Call to closelog() failed with errors: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
202 if $@;
203 5 delete $open_connections{ $self->facility };
204 }
205 5 return 1;
206 }
207  
208 5 ▶sub PRINT {
Subroutine PRINT: called CC=3 SCAR=11.3
209 5 my $self = shift;
210 5 ▶ carp "Cannot PRINT to a closed filehandle!" unless $self->{'is_open'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"is_open"} 0 5 5
211 5 eval { syslog $self->facility."|".$self->priority, "@_" };
212 5 ▶ croak "PRINT failed with errors: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
213 if $@;
214 }
215  
216 1 ▶sub PRINTF {
Subroutine PRINTF: called CC=3 SCAR=11.2
217 1 my $self = shift;
218 1 ▶ carp "Cannot PRINTF to a closed filehandle!" unless $self->{'is_open'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"is_open"} 0 1 1
219 1 my $format = shift;
220 1 eval { syslog $self->facility."|".$self->priority, $format, @_ };
221 1 ▶ croak "PRINTF failed with errors: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
222 if $@;
223 }
224  
225 # Provide a fallback method for write
226 1 ▶sub WRITE {
Subroutine WRITE: called CC=2 SCAR=7.1
227 1 my $self = shift;
228 1 my $string = shift;
229 1 ▶ my $length = shift || length $string;
Condition33%1/3
shift() || length $string
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || length $string
shift() length $string
Truth table33%1/3
A || B
A= shift()B= length $string
ABresult
1X1
011
000
230 1 my $offset = shift; # Ignored
231  
232 1 $self->PRINT(substr($string, 0, $length));
233  
234 1 return $length;
235 }
236  
237 # This peeks a little into Sys:Syslog internals, so it might break sooner or
238 # later. Expect this to happen.
239 # fileno() of socket if available
240 # -1 if we have an open handle
241 # undef if we're not connected
242 # When Sys::Syslog uses 'native' connection, *Sys::Syslog::SYSLOG is not
243 # defined, and $Sys::Syslog::connected is a lexical, so it's not accessible
244 # by us. In other words, we have to try and guess.
245 0 ▶sub FILENO {
Subroutine FILENO: not called CC=3 SCAR=24.8
246 0 my $self = shift;
247 0 my $fd = fileno(*Sys::Syslog::SYSLOG);
248 return defined($fd) ? $fd
249 0 ▶ : $self->{'is_open'} ? -1 : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $fd ? : 0 0 0
$self->{'is_open'} ? : 0 0 0
250 }
251  
252 7 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
253 7 my $self = shift;
254 7 ▶ return 1 unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 7 7
255 7 $self->CLOSE();
256 7 undef $self;
257 }
258  
259 3 ▶sub UNTIE {
Subroutine UNTIE: called CC=2 SCAR=7.1
260 3 my $self = shift;
261 3 ▶ return 1 unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 3 3
262 3 $self->DESTROY;
263 }
264  
265  
266 # ------------------------------------------------------------------------------
267 # Provide a graceful fallback for not(-yet?)-implemented methods
268 # ------------------------------------------------------------------------------
269 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=4 SCAR=26.1
270 0 my $self = shift;
271 0 my $name = (split '::', our $AUTOLOAD)[-1];
272 0 ▶ return if $name eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "DESTROY" 0 0 0
273  
274 0 my $err = "$name operation not (yet?) supported";
275  
276 # See if errors are fatals
277 0 ▶ my $errors_are_fatal = ref($self) ? $self->{'errors_are_fatal'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
278 0 ▶ confess $err if $errors_are_fatal;
Branch: 1 branch
BranchTrueFalseTotal
if $errors_are_fatal 0 0 0
279  
280 # Install a handler for this operation if errors are nonfatal
281 {
282 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
283 0 ▶ *$name = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
284 0 print "$name operation not (yet?) supported";
285 }
286 0 }
287  
288 0 $self->$name;
289 }
290  
291  
292 # ------------------------------------------------------------------------------
293 # Compatibility with Tie::Syslog v1.x
294 # ------------------------------------------------------------------------------
295 # die() and warn() print to STDERR
296 0 ▶sub ExtendedSTDERR {
Subroutine ExtendedSTDERR: not called CC=1 SCAR=6.9
Pod documented
297 0 return 1;
298 }
299  
300 3 'End of Tie::Syslog'
301 __END__
↑ Summary