diff options
author | Larry Wall <lwall@jpl-devvax.jpl.nasa.gov> | 1990-10-15 23:05:15 +0000 |
---|---|---|
committer | Larry Wall <lwall@jpl-devvax.jpl.nasa.gov> | 1990-10-15 23:05:15 +0000 |
commit | 395c379347344a50494d2458b3a5e38ebdeac851 (patch) | |
tree | d7bf6f167eb9cbbbd8b751896913c4a5bede4458 /lib/syslog.pl | |
parent | 0a12ae7dee71b6eb0609c35185096ab75c95b2da (diff) | |
download | perl-395c379347344a50494d2458b3a5e38ebdeac851.tar.gz |
perl 3.0 patch #35 patch #29, continued
See patch #29.
Diffstat (limited to 'lib/syslog.pl')
-rw-r--r-- | lib/syslog.pl | 91 |
1 files changed, 78 insertions, 13 deletions
diff --git a/lib/syslog.pl b/lib/syslog.pl index c98baf32e3..1d7becf66a 100644 --- a/lib/syslog.pl +++ b/lib/syslog.pl @@ -1,6 +1,31 @@ # # syslog.pl # +# $Log: syslog.pl,v $ +Revision 3.0.1.3 90/10/15 17:42:18 lwall +patch29: various portability fixes + +# Revision 3.0.1.1 90/08/09 03:57:17 lwall +# patch19: Initial revision +# +# Revision 1.2 90/06/11 18:45:30 18:45:30 root () +# - Changed 'warn' to 'mail|warning' in test call (to give example of +# facility specification, and because 'warn' didn't work on HP-UX). +# - Fixed typo in &openlog ("ncons" should be "cons"). +# - Added (package-global) $maskpri, and &setlogmask. +# - In &syslog: +# - put argument test ahead of &connect (why waste cycles?), +# - allowed facility to be specified in &syslog's first arg (temporarily +# overrides any $facility set in &openlog), just as in syslog(3C), +# - do a return 0 when bit for $numpri not set in log mask (see syslog(3C)), +# - changed $whoami code to use getlogin, getpwuid($<) and 'syslog' +# (in that order) when $ident is null, +# - made PID logging consistent with syslog(3C) and subject to $lo_pid only, +# - fixed typo in "print CONS" statement ($<facility should be <$facility). +# - changed \n to \r in print CONS (\r is useful, $message already has a \n). +# - Changed &xlate to return -1 for an unknown name, instead of croaking. +# +# # tom christiansen <tchrist@convex.com> # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov> # NOTE: openlog now takes three arguments, just like openlog(3) @@ -15,7 +40,7 @@ # # do openlog($program,'cons,pid','user'); # do syslog('info','this is another test'); -# do syslog('warn','this is a better test: %d', time); +# do syslog('mail|warning','this is a better test: %d', time); # do closelog(); # # do syslog('debug','this is the last test'); @@ -29,13 +54,15 @@ package syslog; $host = 'localhost' unless $host; # set $syslog'host to change -require 'syslog.ph'; +require '/usr/local/lib/perl/syslog.ph'; + +$maskpri = &LOG_UPTO(&LOG_DEBUG); sub main'openlog { ($ident, $logopt, $facility) = @_; # package vars $lo_pid = $logopt =~ /\bpid\b/; $lo_ndelay = $logopt =~ /\bndelay\b/; - $lo_cons = $logopt =~ /\bncons\b/; + $lo_cons = $logopt =~ /\bcons\b/; $lo_nowait = $logopt =~ /\bnowait\b/; &connect if $lo_ndelay; } @@ -44,33 +71,71 @@ sub main'closelog { $facility = $ident = ''; &disconnect; } + +sub main'setlogmask { + local($oldmask) = $maskpri; + $maskpri = shift; + $oldmask; +} sub main'syslog { local($priority) = shift; local($mask) = shift; local($message, $whoami); + local(@words, $num, $numpri, $numfac, $sum); + local($facility) = $facility; # may need to change temporarily. - &connect unless $connected; + die "syslog: expected both priority and mask" unless $mask && $priority; - $whoami = $ident; + @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility". + undef $numpri; + undef $numfac; + foreach (@words) { + $num = &xlate($_); # Translate word to number. + if (/^kern$/ || $num < 0) { + die "syslog: invalid level/facility: $_\n"; + } + elsif ($num <= &LOG_PRIMASK) { + die "syslog: too many levels given: $_\n" if defined($numpri); + $numpri = $num; + return 0 unless &LOG_MASK($numpri) & $maskpri; + } + else { + die "syslog: too many facilities given: $_\n" if defined($numfac); + $facility = $_; + $numfac = $num; + } + } - die "syslog: expected both priority and mask" unless $mask && $priority; + die "syslog: level must be given\n" unless defined($numpri); + + if (!defined($numfac)) { # Facility not specified in this call. + $facility = 'user' unless $facility; + $numfac = &xlate($facility); + } + + &connect unless $connected; - $facility = "user" unless $facility; + $whoami = $ident; if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) { $whoami = $1; $mask = $2; } - $whoami .= " [$$]" if $lo_pid; + + unless ($whoami) { + ($whoami = getlogin) || + ($whoami = getpwuid($<)) || + ($whoami = 'syslog'); + } + + $whoami .= "[$$]" if $lo_pid; $mask =~ s/%m/$!/g; $mask .= "\n" unless $mask =~ /\n$/; $message = sprintf ($mask, @_); - $whoami = sprintf ("%s %d",$ENV{'USER'}||$ENV{'LOGNAME'},$$) unless $whoami; - - $sum = &xlate($priority) + &xlate($facility); + $sum = $numpri + $numfac; unless (send(SYSLOG,"<$sum>$whoami: $message",0)) { if ($lo_cons) { if ($pid = fork) { @@ -80,7 +145,7 @@ sub main'syslog { } else { open(CONS,">/dev/console"); - print CONS "$<facility.$priority>$whoami: $message\n"; + print CONS "<$facility.$priority>$whoami: $message\r"; exit if defined $pid; # if fork failed, we're parent close CONS; } @@ -93,7 +158,7 @@ sub xlate { $name =~ y/a-z/A-Z/; $name = "LOG_$name" unless $name =~ /^LOG_/; $name = "syslog'$name"; - &$name; + eval &$name || -1; } sub connect { |