perl syslog compatibility package

Tom Christiansen tchrist at convex.com
Sat Feb 10 13:05:24 AEST 1990


The following package, syslog.pl, uses /usr/ucb/logger to simulate
calls to BSD4.3's syslog(3).  I find it useful in sysadmin scripts.
It's a package because I needed file-local variables for storing the
'ident' and 'facility' fields initialized by openlog().  Entry points
are openlog(), syslog(), and closelog(); the last is basically a no-op
here for compatibility.


#
# syslog.pl
#
# tom christiansen <tchrist at convex.com>
#
# call syslog() with a string priority and a list of printf() args
# like syslog(3) but using logger(8)
#
#  usage: do 'syslog.pl' || die "syslog.pl: $@";
#
#  then (put these all in a script to test function)
#		
#
#	do openlog($program,'user');
#	do syslog('info','this is another test');
#	do syslog('warn','this is a better test: %d', time);
#	do closelog();
#	
#	do syslog('debug','this is the last test');
#	do syslog('notice','fooprogram: this is really done');
#
#	do openlog("$program $$",'user');
#
#	$! = 55;
#	do syslog('info','problem was %m'); # %m == $! in syslog(3)

package syslog;

$logpath = '/usr/ucb/logger';

die "syslog: can't call $logpath: $!" unless -x $logpath;

sub main'openlog {
    ($ident, $facility) = @_;  # package vars
} 

sub main'closelog {
    $facility = $ident = '';
} 
 
sub main'syslog {
    local($priority) = shift;
    local($mask) = shift;
    local($message, $whoami);

    $whoami = $ident;

    die "syslog: expected both priority and mask" unless $mask && $priority;

    $facility = "user" unless $facility;

    if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
	$whoami = $1;
	$mask = $2;
    } 

    $mask =~ s/%m/$!/g;

    $message = sprintf ($mask, @_);

    $whoami = sprintf ("%s %d", $ENV{'USER'}, $$) unless $whoami;

    warn "logger returned $?" 
	if system $logpath, '-t', $whoami, 
			    '-p', "$facility.$priority", 
			    $message;
}

1;
--

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist at convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"



More information about the Alt.sources mailing list