This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix VMS test to eliminate void context warning
[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;
26 syslog $priority, $mask, $format, @args;
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
46=item syslog $priority, $mask, $format, @args
47
48If I<$priority> and I<$mask> permit, logs I<($format, @args)>
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
37120919 92$host = hostname() unless $host; # set $Syslog::host to change
a0d0e21e
LW
93
94require 'syslog.ph';
95
96$maskpri = &LOG_UPTO(&LOG_DEBUG);
97
98sub openlog {
99 ($ident, $logopt, $facility) = @_; # package vars
100 $lo_pid = $logopt =~ /\bpid\b/;
101 $lo_ndelay = $logopt =~ /\bndelay\b/;
102 $lo_cons = $logopt =~ /\bcons\b/;
103 $lo_nowait = $logopt =~ /\bnowait\b/;
104 &connect if $lo_ndelay;
105}
106
107sub closelog {
108 $facility = $ident = '';
109 &disconnect;
110}
111
112sub setlogmask {
113 local($oldmask) = $maskpri;
114 $maskpri = shift;
115 $oldmask;
116}
117
118sub syslog {
119 local($priority) = shift;
120 local($mask) = shift;
121 local($message, $whoami);
122 local(@words, $num, $numpri, $numfac, $sum);
123 local($facility) = $facility; # may need to change temporarily.
124
125 croak "syslog: expected both priority and mask" unless $mask && $priority;
126
127 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
128 undef $numpri;
129 undef $numfac;
130 foreach (@words) {
131 $num = &xlate($_); # Translate word to number.
132 if (/^kern$/ || $num < 0) {
133 croak "syslog: invalid level/facility: $_";
134 }
135 elsif ($num <= &LOG_PRIMASK) {
136 croak "syslog: too many levels given: $_" if defined($numpri);
137 $numpri = $num;
138 return 0 unless &LOG_MASK($numpri) & $maskpri;
139 }
140 else {
141 croak "syslog: too many facilities given: $_" if defined($numfac);
142 $facility = $_;
143 $numfac = $num;
144 }
145 }
146
147 croak "syslog: level must be given" unless defined($numpri);
148
149 if (!defined($numfac)) { # Facility not specified in this call.
150 $facility = 'user' unless $facility;
151 $numfac = &xlate($facility);
152 }
153
154 &connect unless $connected;
155
156 $whoami = $ident;
157
158 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
159 $whoami = $1;
160 $mask = $2;
161 }
162
163 unless ($whoami) {
164 ($whoami = getlogin) ||
165 ($whoami = getpwuid($<)) ||
166 ($whoami = 'syslog');
167 }
168
169 $whoami .= "[$$]" if $lo_pid;
170
171 $mask =~ s/%m/$!/g;
172 $mask .= "\n" unless $mask =~ /\n$/;
173 $message = sprintf ($mask, @_);
174
175 $sum = $numpri + $numfac;
176 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
177 if ($lo_cons) {
178 if ($pid = fork) {
179 unless ($lo_nowait) {
cb1a09d0 180 $died = waitpid($pid, 0);
a0d0e21e
LW
181 }
182 }
183 else {
184 open(CONS,">/dev/console");
185 print CONS "<$facility.$priority>$whoami: $message\r";
186 exit if defined $pid; # if fork failed, we're parent
187 close CONS;
188 }
189 }
190 }
191}
192
193sub xlate {
194 local($name) = @_;
195 $name =~ y/a-z/A-Z/;
196 $name = "LOG_$name" unless $name =~ /^LOG_/;
748a9306 197 $name = "Sys::Syslog::$name";
a0d0e21e
LW
198 eval(&$name) || -1;
199}
200
201sub connect {
4fc7577b
PP
202 unless ($host) {
203 require Sys::Hostname;
204 $host = Sys::Hostname::hostname();
205 }
cb1a09d0
AD
206 my $udp = getprotobyname('udp');
207 my $syslog = getservbyname('syslog','udp');
208 my $this = sockaddr_in($syslog, INADDR_ANY);
209 my $that = sockaddr_in($syslog, inet_aton($host) || croak "Can't lookup $host");
210 socket(SYSLOG,AF_INET,SOCK_DGRAM,$udp) || croak "socket: $!";
211 connect(SYSLOG,$that) || croak "connect: $!";
a0d0e21e
LW
212 local($old) = select(SYSLOG); $| = 1; select($old);
213 $connected = 1;
214}
215
216sub disconnect {
217 close SYSLOG;
218 $connected = 0;
219}
220
2211;