7 @EXPORT = qw(openlog closelog setlogmask syslog);
12 # adapted from syslog.pl
14 # Tom Christiansen <tchrist@convex.com>
15 # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
16 # NOTE: openlog now takes three arguments, just like openlog(3)
20 Sys::Syslog, openlog, closelog, setlogmask, syslog - Perl interface to the UNIX syslog(3) calls
26 openlog $ident, $logopt, $facility;
27 syslog $priority, $mask, $format, @args;
28 $oldmask = setlogmask $mask_priority;
33 Sys::Syslog is an interface to the UNIX C<syslog(3)> program.
34 Call C<syslog()> with a string priority and a list of C<printf()> args
35 just like C<syslog(3)>.
37 Syslog provides the functions:
41 =item openlog $ident, $logopt, $facility
43 I<$ident> is prepended to every message.
44 I<$logopt> contains one or more of the words I<pid>, I<ndelay>, I<cons>, I<nowait>.
45 I<$facility> specifies the part of the system
47 =item syslog $priority, $mask, $format, @args
49 If I<$priority> and I<$mask> permit, logs I<($format, @args)>
50 printed as by C<printf(3V)>, with the addition that I<%m>
51 is replaced with C<"$!"> (the latest error message).
53 =item setlogmask $mask_priority
55 Sets log mask I<$mask_priority> and returns the old mask.
63 Note that C<openlog> now takes three arguments, just like C<openlog(3)>.
67 openlog($program, 'cons,pid', 'user');
68 syslog('info', 'this is another test');
69 syslog('mail|warning', 'this is a better test: %d', time);
72 syslog('debug', 'this is the last test');
73 openlog("$program $$", 'ndelay', 'user');
74 syslog('notice', 'fooprogram: this is really done');
77 syslog('info', 'problem was %m'); # %m == $! in syslog(3)
81 B<Sys::Syslog> needs F<syslog.ph>, which can be created with C<h2ph>.
89 Tom Christiansen E<lt>F<tchrist@perl.com>E<gt> and Larry Wall E<lt>F<lwall@sems.com>E<gt>
93 $host = hostname() unless $host; # set $Syslog::host to change
97 $maskpri = &LOG_UPTO(&LOG_DEBUG);
100 ($ident, $logopt, $facility) = @_; # package vars
101 $lo_pid = $logopt =~ /\bpid\b/;
102 $lo_ndelay = $logopt =~ /\bndelay\b/;
103 $lo_cons = $logopt =~ /\bcons\b/;
104 $lo_nowait = $logopt =~ /\bnowait\b/;
105 &connect if $lo_ndelay;
109 $facility = $ident = '';
114 local($oldmask) = $maskpri;
120 local($priority) = shift;
121 local($mask) = shift;
122 local($message, $whoami);
123 local(@words, $num, $numpri, $numfac, $sum);
124 local($facility) = $facility; # may need to change temporarily.
126 croak "syslog: expected both priority and mask" unless $mask && $priority;
128 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
132 $num = &xlate($_); # Translate word to number.
133 if (/^kern$/ || $num < 0) {
134 croak "syslog: invalid level/facility: $_";
136 elsif ($num <= &LOG_PRIMASK) {
137 croak "syslog: too many levels given: $_" if defined($numpri);
139 return 0 unless &LOG_MASK($numpri) & $maskpri;
142 croak "syslog: too many facilities given: $_" if defined($numfac);
148 croak "syslog: level must be given" unless defined($numpri);
150 if (!defined($numfac)) { # Facility not specified in this call.
151 $facility = 'user' unless $facility;
152 $numfac = &xlate($facility);
155 &connect unless $connected;
159 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
165 ($whoami = getlogin) ||
166 ($whoami = getpwuid($<)) ||
167 ($whoami = 'syslog');
170 $whoami .= "[$$]" if $lo_pid;
173 $mask .= "\n" unless $mask =~ /\n$/;
174 $message = sprintf ($mask, @_);
176 $sum = $numpri + $numfac;
177 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
180 unless ($lo_nowait) {
181 $died = waitpid($pid, 0);
185 open(CONS,">/dev/console");
186 print CONS "<$facility.$priority>$whoami: $message\r";
187 exit if defined $pid; # if fork failed, we're parent
197 $name = "LOG_$name" unless $name =~ /^LOG_/;
198 $name = "Sys::Syslog::$name";
203 my $udp = getprotobyname('udp');
204 my $syslog = getservbyname('syslog','udp');
205 my $this = sockaddr_in($syslog, INADDR_ANY);
206 my $that = sockaddr_in($syslog, inet_aton($host) || croak "Can't lookup $host");
207 socket(SYSLOG,AF_INET,SOCK_DGRAM,$udp) || croak "socket: $!";
208 connect(SYSLOG,$that) || croak "connect: $!";
209 local($old) = select(SYSLOG); $| = 1; select($old);