Xref: utzoo alt.sources:1488 comp.lang.perl:338 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!cs.utexas.edu!sun-barr!newstop!texsun!convex!tchrist From: tchrist@convex.com (Tom Christiansen) Newsgroups: alt.sources,comp.lang.perl Subject: perl syslog compatibility package Message-ID: <5318@convex.convex.com> Date: 10 Feb 90 02:05:24 GMT Sender: news@convex.com Reply-To: tchrist@convex.Com (Tom Christiansen) Followup-To: alt.sources Organization: Convex Computer Corp, Richardson, TX Lines: 82 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 # # 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@convex.COM "EMACS belongs in : Editor too big!"