4 # tom christiansen <tchrist@convex.com>
5 # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
6 # NOTE: openlog now takes three arguments, just like openlog(3)
8 # call syslog() with a string priority and a list of printf() args
11 # usage: do 'syslog.pl' || die "syslog.pl: $@";
13 # then (put these all in a script to test function)
16 # do openlog($program,'cons,pid','user');
17 # do syslog('info','this is another test');
18 # do syslog('warn','this is a better test: %d', time);
21 # do syslog('debug','this is the last test');
22 # do openlog("$program $$",'ndelay','user');
23 # do syslog('notice','fooprogram: this is really done');
26 # do syslog('info','problem was %m'); # %m == $! in syslog(3)
30 $host = 'localhost' unless $host; # set $syslog'host to change
32 do '/usr/local/lib/perl/syslog.h'
33 || die "syslog: Can't do syslog.h: ",($@||$!),"\n";
36 ($ident, $logopt, $facility) = @_; # package vars
37 $lo_pid = $logopt =~ /\bpid\b/;
38 $lo_ndelay = $logopt =~ /\bndelay\b/;
39 $lo_cons = $logopt =~ /\bncons\b/;
40 $lo_nowait = $logopt =~ /\bnowait\b/;
41 &connect if $lo_ndelay;
45 $facility = $ident = '';
50 local($priority) = shift;
52 local($message, $whoami);
54 &connect unless $connected;
58 die "syslog: expected both priority and mask" unless $mask && $priority;
60 $facility = "user" unless $facility;
62 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
66 $whoami .= " [$$]" if $lo_pid;
69 $mask .= "\n" unless $mask =~ /\n$/;
70 $message = sprintf ($mask, @_);
72 $whoami = sprintf ("%s %d",$ENV{'USER'}||$ENV{'LOGNAME'},$$) unless $whoami;
74 $sum = &xlate($priority) + &xlate($facility);
75 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
79 do {$died = wait;} until $died == $pid || $died < 0;
83 open(CONS,">/dev/console");
84 print CONS "$<facility.$priority>$whoami: $message\n";
85 exit if defined $pid; # if fork failed, we're parent
95 $name = "LOG_$name" unless $name =~ /^LOG_/;
96 $name = "syslog'$name";
109 ($name,$aliases,$proto) = getprotobyname('udp');
112 ($name,$aliase,$port,$proto) = getservbyname('syslog','udp');
115 if (chop($myname = `hostname`)) {
116 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
117 die "Can't lookup $myname\n" unless $name;
118 @bytes = unpack("C4",$addrs[0]);
123 $this = pack($pat, $af_inet, 0, @bytes);
125 if ($host =~ /^\d+\./) {
126 @bytes = split(/\./,$host);
129 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
130 die "Can't lookup $host\n" unless $name;
131 @bytes = unpack("C4",$addrs[0]);
133 $that = pack($pat,$af_inet,$syslog,@bytes);
135 socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
136 bind(SYSLOG,$this) || die "bind: $!\n";
137 connect(SYSLOG,$that) || die "connect: $!\n";
139 local($old) = select(SYSLOG); $| = 1; select($old);