This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix a2p translation of '{print "a" "b" "c"}'
[perl5.git] / lib / Sys / Syslog.pm
CommitLineData
a0d0e21e
LW
1package Sys::Syslog;
2require 5.000;
3require Exporter;
4use Carp;
5
6@ISA = qw(Exporter);
7@EXPORT = qw(openlog closelog setlogmask syslog);
8
37120919 9use Socket;
37120919 10
5be1dfc7 11# adapted from syslog.pl
a0d0e21e 12#
5be1dfc7 13# Tom Christiansen <tchrist@convex.com>
a0d0e21e
LW
14# modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
15# NOTE: openlog now takes three arguments, just like openlog(3)
5be1dfc7
HF
16
17=head1 NAME
18
19Sys::Syslog, openlog, closelog, setlogmask, syslog - Perl interface to the UNIX syslog(3) calls
20
21=head1 SYNOPSIS
22
23 use Sys::Syslog;
24
25 openlog $ident, $logopt, $facility;
2eae817d 26 syslog $priority, $format, @args;
5be1dfc7
HF
27 $oldmask = setlogmask $mask_priority;
28 closelog;
29
30=head1 DESCRIPTION
31
32Sys::Syslog is an interface to the UNIX C<syslog(3)> program.
33Call C<syslog()> with a string priority and a list of C<printf()> args
34just like C<syslog(3)>.
35
36Syslog provides the functions:
37
38=over
39
40=item openlog $ident, $logopt, $facility
41
42I<$ident> is prepended to every message.
43I<$logopt> contains one or more of the words I<pid>, I<ndelay>, I<cons>, I<nowait>.
44I<$facility> specifies the part of the system
45
2eae817d 46=item syslog $priority, $format, @args
5be1dfc7 47
2eae817d 48If I<$priority> permits, logs I<($format, @args)>
5be1dfc7
HF
49printed as by C<printf(3V)>, with the addition that I<%m>
50is replaced with C<"$!"> (the latest error message).
51
52=item setlogmask $mask_priority
53
54Sets log mask I<$mask_priority> and returns the old mask.
55
56=item closelog
57
58Closes the log file.
59
60=back
61
62Note that C<openlog> now takes three arguments, just like C<openlog(3)>.
63
64=head1 EXAMPLES
65
66 openlog($program, 'cons,pid', 'user');
67 syslog('info', 'this is another test');
68 syslog('mail|warning', 'this is a better test: %d', time);
69 closelog();
70
71 syslog('debug', 'this is the last test');
72 openlog("$program $$", 'ndelay', 'user');
73 syslog('notice', 'fooprogram: this is really done');
74
75 $! = 55;
76 syslog('info', 'problem was %m'); # %m == $! in syslog(3)
77
78=head1 DEPENDENCIES
79
80B<Sys::Syslog> needs F<syslog.ph>, which can be created with C<h2ph>.
81
82=head1 SEE ALSO
83
84L<syslog(3)>
85
86=head1 AUTHOR
87
88Tom Christiansen E<lt>F<tchrist@perl.com>E<gt> and Larry Wall E<lt>F<lwall@sems.com>E<gt>
89
90=cut
a0d0e21e 91
a0d0e21e
LW
92require 'syslog.ph';
93
94$maskpri = &LOG_UPTO(&LOG_DEBUG);
95
96sub openlog {
97 ($ident, $logopt, $facility) = @_; # package vars
98 $lo_pid = $logopt =~ /\bpid\b/;
99 $lo_ndelay = $logopt =~ /\bndelay\b/;
100 $lo_cons = $logopt =~ /\bcons\b/;
101 $lo_nowait = $logopt =~ /\bnowait\b/;
102 &connect if $lo_ndelay;
103}
104
105sub closelog {
106 $facility = $ident = '';
107 &disconnect;
108}
109
110sub setlogmask {
111 local($oldmask) = $maskpri;
112 $maskpri = shift;
113 $oldmask;
114}
115
116sub syslog {
117 local($priority) = shift;
118 local($mask) = shift;
119 local($message, $whoami);
120 local(@words, $num, $numpri, $numfac, $sum);
121 local($facility) = $facility; # may need to change temporarily.
122
123 croak "syslog: expected both priority and mask" unless $mask && $priority;
124
125 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
126 undef $numpri;
127 undef $numfac;
128 foreach (@words) {
129 $num = &xlate($_); # Translate word to number.
130 if (/^kern$/ || $num < 0) {
131 croak "syslog: invalid level/facility: $_";
132 }
133 elsif ($num <= &LOG_PRIMASK) {
134 croak "syslog: too many levels given: $_" if defined($numpri);
135 $numpri = $num;
136 return 0 unless &LOG_MASK($numpri) & $maskpri;
137 }
138 else {
139 croak "syslog: too many facilities given: $_" if defined($numfac);
140 $facility = $_;
141 $numfac = $num;
142 }
143 }
144
145 croak "syslog: level must be given" unless defined($numpri);
146
147 if (!defined($numfac)) { # Facility not specified in this call.
148 $facility = 'user' unless $facility;
149 $numfac = &xlate($facility);
150 }
151
152 &connect unless $connected;
153
154 $whoami = $ident;
155
156 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
157 $whoami = $1;
158 $mask = $2;
159 }
160
161 unless ($whoami) {
162 ($whoami = getlogin) ||
163 ($whoami = getpwuid($<)) ||
164 ($whoami = 'syslog');
165 }
166
167 $whoami .= "[$$]" if $lo_pid;
168
169 $mask =~ s/%m/$!/g;
170 $mask .= "\n" unless $mask =~ /\n$/;
171 $message = sprintf ($mask, @_);
172
173 $sum = $numpri + $numfac;
174 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
175 if ($lo_cons) {
176 if ($pid = fork) {
177 unless ($lo_nowait) {
cb1a09d0 178 $died = waitpid($pid, 0);
a0d0e21e
LW
179 }
180 }
181 else {
182 open(CONS,">/dev/console");
183 print CONS "<$facility.$priority>$whoami: $message\r";
184 exit if defined $pid; # if fork failed, we're parent
185 close CONS;
186 }
187 }
188 }
189}
190
191sub xlate {
192 local($name) = @_;
193 $name =~ y/a-z/A-Z/;
194 $name = "LOG_$name" unless $name =~ /^LOG_/;
748a9306 195 $name = "Sys::Syslog::$name";
a0d0e21e
LW
196 eval(&$name) || -1;
197}
198
199sub connect {
4fc7577b
PP
200 unless ($host) {
201 require Sys::Hostname;
2eae817d
PP
202 my($host_uniq) = Sys::Hostname::hostname();
203 ($host) = $host_uniq =~ /(\w+)/;
4fc7577b 204 }
cb1a09d0
AD
205 my $udp = getprotobyname('udp');
206 my $syslog = getservbyname('syslog','udp');
207 my $this = sockaddr_in($syslog, INADDR_ANY);
208 my $that = sockaddr_in($syslog, inet_aton($host) || croak "Can't lookup $host");
209 socket(SYSLOG,AF_INET,SOCK_DGRAM,$udp) || croak "socket: $!";
210 connect(SYSLOG,$that) || croak "connect: $!";
a0d0e21e
LW
211 local($old) = select(SYSLOG); $| = 1; select($old);
212 $connected = 1;
213}
214
215sub disconnect {
216 close SYSLOG;
217 $connected = 0;
218}
219
2201;