7 @EXPORT = qw(openlog closelog setlogmask syslog);
11 # adapted from syslog.pl
13 # Tom Christiansen <tchrist@convex.com>
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)
19 Sys::Syslog, openlog, closelog, setlogmask, syslog - Perl interface to the UNIX syslog(3) calls
25 openlog $ident, $logopt, $facility;
26 syslog $priority, $mask, $format, @args;
27 $oldmask = setlogmask $mask_priority;
32 Sys::Syslog is an interface to the UNIX C<syslog(3)> program.
33 Call C<syslog()> with a string priority and a list of C<printf()> args
34 just like C<syslog(3)>.
36 Syslog provides the functions:
40 =item openlog $ident, $logopt, $facility
42 I<$ident> is prepended to every message.
43 I<$logopt> contains one or more of the words I<pid>, I<ndelay>, I<cons>, I<nowait>.
44 I<$facility> specifies the part of the system
46 =item syslog $priority, $mask, $format, @args
48 If I<$priority> and I<$mask> permit, logs I<($format, @args)>
49 printed as by C<printf(3V)>, with the addition that I<%m>
50 is replaced with C<"$!"> (the latest error message).
52 =item setlogmask $mask_priority
54 Sets log mask I<$mask_priority> and returns the old mask.
62 Note that C<openlog> now takes three arguments, just like C<openlog(3)>.
66 openlog($program, 'cons,pid', 'user');
67 syslog('info', 'this is another test');
68 syslog('mail|warning', 'this is a better test: %d', time);
71 syslog('debug', 'this is the last test');
72 openlog("$program $$", 'ndelay', 'user');
73 syslog('notice', 'fooprogram: this is really done');
76 syslog('info', 'problem was %m'); # %m == $! in syslog(3)
80 B<Sys::Syslog> needs F<syslog.ph>, which can be created with C<h2ph>.
88 Tom Christiansen E<lt>F<tchrist@perl.com>E<gt> and Larry Wall E<lt>F<lwall@sems.com>E<gt>
92 $host = hostname() unless $host; # set $Syslog::host to change
96 $maskpri = &LOG_UPTO(&LOG_DEBUG);
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;
108 $facility = $ident = '';
113 local($oldmask) = $maskpri;
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.
125 croak "syslog: expected both priority and mask" unless $mask && $priority;
127 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
131 $num = &xlate($_); # Translate word to number.
132 if (/^kern$/ || $num < 0) {
133 croak "syslog: invalid level/facility: $_";
135 elsif ($num <= &LOG_PRIMASK) {
136 croak "syslog: too many levels given: $_" if defined($numpri);
138 return 0 unless &LOG_MASK($numpri) & $maskpri;
141 croak "syslog: too many facilities given: $_" if defined($numfac);
147 croak "syslog: level must be given" unless defined($numpri);
149 if (!defined($numfac)) { # Facility not specified in this call.
150 $facility = 'user' unless $facility;
151 $numfac = &xlate($facility);
154 &connect unless $connected;
158 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
164 ($whoami = getlogin) ||
165 ($whoami = getpwuid($<)) ||
166 ($whoami = 'syslog');
169 $whoami .= "[$$]" if $lo_pid;
172 $mask .= "\n" unless $mask =~ /\n$/;
173 $message = sprintf ($mask, @_);
175 $sum = $numpri + $numfac;
176 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
179 unless ($lo_nowait) {
180 $died = waitpid($pid, 0);
184 open(CONS,">/dev/console");
185 print CONS "<$facility.$priority>$whoami: $message\r";
186 exit if defined $pid; # if fork failed, we're parent
196 $name = "LOG_$name" unless $name =~ /^LOG_/;
197 $name = "Sys::Syslog::$name";
203 require Sys::Hostname;
204 $host = Sys::Hostname::hostname();
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: $!";
212 local($old) = select(SYSLOG); $| = 1; select($old);