File Coverage

blib/lib/Paranoid/Log/Syslog.pm
Criterion Covered Total %
statement 63 72 87.5
branch 11 16 68.7
condition 5 9 55.5
subroutine 14 16 87.5
pod 4 4 100.0
total 97 117 82.9


line stmt bran cond sub pod time code
1             # Paranoid::Log::Syslog -- Log Facility Syslog for paranoid programs
2             #
3             # (c) 2005 - 2015, Arthur Corliss
4             #
5             # $Id: lib/Paranoid/Log/Syslog.pm, 2.00 2016/05/13 19:51:02 acorliss Exp $
6             #
7             # This software is licensed under the same terms as Perl, itself.
8             # Please see http://dev.perl.org/licenses/ for more information.
9             #
10             #####################################################################
11              
12             #####################################################################
13             #
14             # Environment definitions
15             #
16             #####################################################################
17              
18             package Paranoid::Log::Syslog;
19              
20 1     1   86601 use 5.006;
  1         3  
21              
22 1     1   4 use strict;
  1         1  
  1         15  
23 1     1   3 use warnings;
  1         4  
  1         23  
24 1     1   3 use vars qw($VERSION);
  1         1  
  1         33  
25 1     1   3 use Paranoid;
  1         1  
  1         36  
26 1     1   3 use Paranoid::Debug qw(:all);
  1         1  
  1         147  
27 1     1   417 use Unix::Syslog qw(:macros :subs);
  1         625  
  1         251  
28 1     1   5 use Carp;
  1         1  
  1         547  
29              
30             ($VERSION) = ( q$Revision: 2.00 $ =~ /(\d+(?:\.\d+)+)/sm );
31              
32             my @p2lmap = ( LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_WARNING,
33             LOG_ERR, LOG_CRIT, LOG_ALERT, LOG_EMERG,
34             );
35              
36             #####################################################################
37             #
38             # Module code follows
39             #
40             #####################################################################
41              
42             {
43              
44             my %loggers = ();
45              
46             sub _logger {
47 8     8   8 my $name = shift;
48 8         8 my %options = @_;
49              
50 8 100       16 unless ( exists $loggers{$name} ) {
51              
52             # Set a default ident
53 3 100 66     11 unless ( defined $options{ident} and length $options{ident} ) {
54 2         15 ( $options{ident} ) = ( $0 =~ m#([^/]+)$#s );
55             }
56              
57             # Set a default facility
58 3 50       10 $options{facility} = 'user' unless defined $options{facility};
59 3         5 $options{facility} = _transFacility( $options{facility} );
60              
61             # Set a default syslog mode options
62 3         7 $options{sysopt} = LOG_CONS | LOG_PID;
63              
64             # Set PID
65 3         5 $options{pid} = $$;
66              
67             # Save the options
68 3         8 $loggers{$name} = {%options};
69             }
70              
71 8         7 return %{ $loggers{$name} };
  8         21  
72             }
73              
74             sub _delLogger {
75 0     0   0 my $name = shift;
76              
77 0         0 delete $loggers{$name};
78              
79 0         0 return 1;
80             }
81              
82             my $lastLogger;
83              
84             sub _lastLogger : lvalue {
85 14     14   16 $lastLogger;
86             }
87              
88             }
89              
90             sub init {
91              
92             # Purpose: Exists purely for compliance.
93             # Returns: True (1)
94             # Usage: init();
95              
96 1     1 1 191 return 1;
97             }
98              
99             sub _transFacility {
100              
101             # Purpose: Translates the string log facilities into the syslog constants
102             # Returns: Constant scalar value
103             # Usage: $facility = _transFacility($facilityName);
104              
105 3     3   4 my $f = lc shift;
106 3         40 my %trans = (
107             authpriv => LOG_AUTHPRIV,
108             auth => LOG_AUTHPRIV,
109             cron => LOG_CRON,
110             daemon => LOG_DAEMON,
111             ftp => LOG_FTP,
112             kern => LOG_KERN,
113             local0 => LOG_LOCAL0,
114             local1 => LOG_LOCAL1,
115             local2 => LOG_LOCAL2,
116             local3 => LOG_LOCAL3,
117             local4 => LOG_LOCAL4,
118             local5 => LOG_LOCAL5,
119             local6 => LOG_LOCAL6,
120             local7 => LOG_LOCAL7,
121             lpr => LOG_LPR,
122             mail => LOG_MAIL,
123             news => LOG_NEWS,
124             syslog => LOG_SYSLOG,
125             user => LOG_USER,
126             uucp => LOG_UUCP,
127             );
128              
129 3 50       12 return exists $trans{$f} ? $trans{$f} : undef;
130             }
131              
132             sub addLogger {
133              
134             # Purpose: Exists purely for compliance.
135             # Returns: True (1)
136             # Usage: init();
137              
138 3     3 1 1257 my %record = @_;
139              
140 3         3 _logger( $record{name}, %{ $record{options} } );
  3         8  
141              
142 3         7 return 1;
143             }
144              
145             sub delLogger {
146              
147             # Purpose: Exists purely for compliance.
148             # Returns: True (1)
149             # Usage: init();
150              
151 0     0 1 0 my $name = shift;
152              
153 0         0 return _delLogger($name);
154             }
155              
156             sub logMsg {
157              
158             # Purpose: Logs the passed message to the named file
159             # Returns: Return value of print()
160             # Usage: logMsg(%recordd);
161              
162 5     5 1 1000 my %record = @_;
163 5         9 my %options = _logger( $record{name} );
164 5         9 my $llogger = _lastLogger();
165 5         3 my $rv;
166              
167 5         12 pdebug( 'entering w/%s', PDLEVEL1, %record );
168 5         210 pIn();
169              
170 5 50 33     34 if ( defined $record{message} and length $record{message} ) {
171              
172             # Check for children processes
173 5 50       12 if ( $options{pid} != $$ ) {
174 0         0 closelog();
175 0         0 $llogger = _lastLogger() = undef;
176 0         0 _delLogger( $record{name} );
177 0         0 _logger( $record{name}, %options );
178             }
179              
180             # Close the syslog connection and reconfigure if
181             # this is a different logger
182 5 100 66     17 if ( defined $llogger and $llogger ne $record{name} ) {
183 4         7 closelog();
184 4         5 $llogger = _lastLogger() = undef;
185             }
186              
187             # Open a new connection
188 5 50       7 unless ( defined $llogger ) {
189 5         16 openlog( $options{ident}, $options{sysopt}, $options{facility} );
190 5         5 _lastLogger() = $record{name};
191             }
192              
193             # Logg the message
194 5         15 syslog( $p2lmap[ $record{severity} ], '%s', $record{message} );
195 5         269 $rv = 1;
196              
197             }
198              
199 5         12 pOut();
200 5         39 pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
201              
202 5         120 return $rv;
203             }
204              
205             1;
206              
207             __END__