5 # Revision 3.0.1.1 90/08/09 03:57:17 lwall
6 # patch19: Initial revision
8 # Revision 1.2 90/06/11 18:45:30 18:45:30 root ()
9 # - Changed 'warn' to 'mail|warning' in test call (to give example of
10 # facility specification, and because 'warn' didn't work on HP-UX).
11 # - Fixed typo in &openlog ("ncons" should be "cons").
12 # - Added (package-global) $maskpri, and &setlogmask.
14 # - put argument test ahead of &connect (why waste cycles?),
15 # - allowed facility to be specified in &syslog's first arg (temporarily
16 # overrides any $facility set in &openlog), just as in syslog(3C),
17 # - do a return 0 when bit for $numpri not set in log mask (see syslog(3C)),
18 # - changed $whoami code to use getlogin, getpwuid($<) and 'syslog'
19 # (in that order) when $ident is null,
20 # - made PID logging consistent with syslog(3C) and subject to $lo_pid only,
21 # - fixed typo in "print CONS" statement ($<facility should be <$facility).
22 # - changed \n to \r in print CONS (\r is useful, $message already has a \n).
23 # - Changed &xlate to return -1 for an unknown name, instead of croaking.
26 # tom christiansen <tchrist@convex.com>
27 # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
28 # NOTE: openlog now takes three arguments, just like openlog(3)
30 # call syslog() with a string priority and a list of printf() args
33 # usage: require 'syslog.pl';
35 # then (put these all in a script to test function)
38 # do openlog($program,'cons,pid','user');
39 # do syslog('info','this is another test');
40 # do syslog('mail|warning','this is a better test: %d', time);
43 # do syslog('debug','this is the last test');
44 # do openlog("$program $$",'ndelay','user');
45 # do syslog('notice','fooprogram: this is really done');
48 # do syslog('info','problem was %m'); # %m == $! in syslog(3)
52 $host = 'localhost' unless $host; # set $syslog'host to change
54 require '/usr/local/lib/perl/syslog.ph';
56 $maskpri = &LOG_UPTO(&LOG_DEBUG);
59 ($ident, $logopt, $facility) = @_; # package vars
60 $lo_pid = $logopt =~ /\bpid\b/;
61 $lo_ndelay = $logopt =~ /\bndelay\b/;
62 $lo_cons = $logopt =~ /\bcons\b/;
63 $lo_nowait = $logopt =~ /\bnowait\b/;
64 &connect if $lo_ndelay;
68 $facility = $ident = '';
73 local($oldmask) = $maskpri;
79 local($priority) = shift;
81 local($message, $whoami);
82 local(@words, $num, $numpri, $numfac, $sum);
83 local($facility) = $facility; # may need to change temporarily.
85 die "syslog: expected both priority and mask" unless $mask && $priority;
87 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
91 $num = &xlate($_); # Translate word to number.
92 if (/^kern$/ || $num < 0) {
93 die "syslog: invalid level/facility: $_\n";
95 elsif ($num <= &LOG_PRIMASK) {
96 die "syslog: too many levels given: $_\n" if defined($numpri);
98 return 0 unless &LOG_MASK($numpri) & $maskpri;
101 die "syslog: too many facilities given: $_\n" if defined($numfac);
107 die "syslog: level must be given\n" unless defined($numpri);
109 if (!defined($numfac)) { # Facility not specified in this call.
110 $facility = 'user' unless $facility;
111 $numfac = &xlate($facility);
114 &connect unless $connected;
118 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
124 ($whoami = getlogin) ||
125 ($whoami = getpwuid($<)) ||
126 ($whoami = 'syslog');
129 $whoami .= "[$$]" if $lo_pid;
132 $mask .= "\n" unless $mask =~ /\n$/;
133 $message = sprintf ($mask, @_);
135 $sum = $numpri + $numfac;
136 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
139 unless ($lo_nowait) {
140 do {$died = wait;} until $died == $pid || $died < 0;
144 open(CONS,">/dev/console");
145 print CONS "<$facility.$priority>$whoami: $message\r";
146 exit if defined $pid; # if fork failed, we're parent
156 $name = "LOG_$name" unless $name =~ /^LOG_/;
157 $name = "syslog'$name";
170 ($name,$aliases,$proto) = getprotobyname('udp');
173 ($name,$aliase,$port,$proto) = getservbyname('syslog','udp');
176 if (chop($myname = `hostname`)) {
177 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
178 die "Can't lookup $myname\n" unless $name;
179 @bytes = unpack("C4",$addrs[0]);
184 $this = pack($pat, $af_inet, 0, @bytes);
186 if ($host =~ /^\d+\./) {
187 @bytes = split(/\./,$host);
190 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
191 die "Can't lookup $host\n" unless $name;
192 @bytes = unpack("C4",$addrs[0]);
194 $that = pack($pat,$af_inet,$syslog,@bytes);
196 socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
197 bind(SYSLOG,$this) || die "bind: $!\n";
198 connect(SYSLOG,$that) || die "connect: $!\n";
200 local($old) = select(SYSLOG); $| = 1; select($old);