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, $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, $format, @args
49 If I<$priority> permits, 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<larry@wall.org>E<gt>
95 $maskpri = &LOG_UPTO(&LOG_DEBUG);
98 ($ident, $logopt, $facility) = @_; # package vars
99 $lo_pid = $logopt =~ /\bpid\b/;
100 $lo_ndelay = $logopt =~ /\bndelay\b/;
101 $lo_cons = $logopt =~ /\bcons\b/;
102 $lo_nowait = $logopt =~ /\bnowait\b/;
103 &connect if $lo_ndelay;
107 $facility = $ident = '';
112 local($oldmask) = $maskpri;
118 local($priority) = shift;
119 local($mask) = shift;
120 local($message, $whoami);
121 local(@words, $num, $numpri, $numfac, $sum);
122 local($facility) = $facility; # may need to change temporarily.
124 croak "syslog: expected both priority and mask" unless $mask && $priority;
126 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
130 $num = &xlate($_); # Translate word to number.
131 if (/^kern$/ || $num < 0) {
132 croak "syslog: invalid level/facility: $_";
134 elsif ($num <= &LOG_PRIMASK) {
135 croak "syslog: too many levels given: $_" if defined($numpri);
137 return 0 unless &LOG_MASK($numpri) & $maskpri;
140 croak "syslog: too many facilities given: $_" if defined($numfac);
146 croak "syslog: level must be given" unless defined($numpri);
148 if (!defined($numfac)) { # Facility not specified in this call.
149 $facility = 'user' unless $facility;
150 $numfac = &xlate($facility);
153 &connect unless $connected;
157 if (!$whoami && $mask =~ /^(\S.*?):\s?(.*)/) {
163 ($whoami = getlogin) ||
164 ($whoami = getpwuid($<)) ||
165 ($whoami = 'syslog');
168 $whoami .= "[$$]" if $lo_pid;
171 $mask .= "\n" unless $mask =~ /\n$/;
172 $message = sprintf ($mask, @_);
174 $sum = $numpri + $numfac;
175 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
178 unless ($lo_nowait) {
179 $died = waitpid($pid, 0);
183 open(CONS,">/dev/console");
184 print CONS "<$facility.$priority>$whoami: $message\r";
185 exit if defined $pid; # if fork failed, we're parent
195 $name = "LOG_$name" unless $name =~ /^LOG_/;
196 $name = "Sys::Syslog::$name";
197 defined &$name ? &$name : -1;
202 require Sys::Hostname;
203 my($host_uniq) = Sys::Hostname::hostname();
204 ($host) = $host_uniq =~ /([\w\-]+)/;
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);