6 # tom christiansen <tchrist@convex.com>
7 # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
8 # NOTE: openlog now takes three arguments, just like openlog(3)
10 # call syslog() with a string priority and a list of printf() args
13 # usage: require 'syslog.pl';
15 # then (put these all in a script to test function)
18 # do openlog($program,'cons,pid','user');
19 # do syslog('info','this is another test');
20 # do syslog('mail|warning','this is a better test: %d', time);
23 # do syslog('debug','this is the last test');
24 # do openlog("$program $$",'ndelay','user');
25 # do syslog('notice','fooprogram: this is really done');
28 # do syslog('info','problem was %m'); # %m == $! in syslog(3)
32 $host = 'localhost' unless $host; # set $syslog'host to change
35 warn "You should 'use Sys::Syslog' instead; continuing" # if $^W
40 eval 'use Socket; 1' ||
41 eval { require "socket.ph" } ||
42 require "sys/socket.ph";
44 $maskpri = &LOG_UPTO(&LOG_DEBUG);
47 ($ident, $logopt, $facility) = @_; # package vars
48 $lo_pid = $logopt =~ /\bpid\b/;
49 $lo_ndelay = $logopt =~ /\bndelay\b/;
50 $lo_cons = $logopt =~ /\bcons\b/;
51 $lo_nowait = $logopt =~ /\bnowait\b/;
52 &connect if $lo_ndelay;
56 $facility = $ident = '';
61 local($oldmask) = $maskpri;
67 local($priority) = shift;
69 local($message, $whoami);
70 local(@words, $num, $numpri, $numfac, $sum);
71 local($facility) = $facility; # may need to change temporarily.
73 die "syslog: expected both priority and mask" unless $mask && $priority;
75 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
79 $num = &xlate($_); # Translate word to number.
80 if (/^kern$/ || $num < 0) {
81 die "syslog: invalid level/facility: $_\n";
83 elsif ($num <= &LOG_PRIMASK) {
84 die "syslog: too many levels given: $_\n" if defined($numpri);
86 return 0 unless &LOG_MASK($numpri) & $maskpri;
89 die "syslog: too many facilities given: $_\n" if defined($numfac);
95 die "syslog: level must be given\n" unless defined($numpri);
97 if (!defined($numfac)) { # Facility not specified in this call.
98 $facility = 'user' unless $facility;
99 $numfac = &xlate($facility);
102 &connect unless $connected;
106 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
112 ($whoami = getlogin) ||
113 ($whoami = getpwuid($<)) ||
114 ($whoami = 'syslog');
117 $whoami .= "[$$]" if $lo_pid;
120 $mask .= "\n" unless $mask =~ /\n$/;
121 $message = sprintf ($mask, @_);
123 $sum = $numpri + $numfac;
124 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
127 unless ($lo_nowait) {
128 do {$died = wait;} until $died == $pid || $died < 0;
132 open(CONS,">/dev/console");
133 print CONS "<$facility.$priority>$whoami: $message\r";
134 exit if defined $pid; # if fork failed, we're parent
144 $name = "LOG_$name" unless $name =~ /^LOG_/;
145 $name = "syslog'$name";
146 defined &$name ? &$name : -1;
155 $stream = &SOCK_STREAM;
156 $datagram = &SOCK_DGRAM;
158 ($name,$aliases,$proto) = getprotobyname('udp');
161 ($name,$aliases,$port,$proto) = getservbyname('syslog','udp');
164 if (chop($myname = `hostname`)) {
165 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
166 die "Can't lookup $myname\n" unless $name;
167 @bytes = unpack("C4",$addrs[0]);
172 $this = pack($pat, $af_inet, 0, @bytes);
174 if ($host =~ /^\d+\./) {
175 @bytes = split(/\./,$host);
178 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
179 die "Can't lookup $host\n" unless $name;
180 @bytes = unpack("C4",$addrs[0]);
182 $that = pack($pat,$af_inet,$syslog,@bytes);
184 socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
185 bind(SYSLOG,$this) || die "bind: $!\n";
186 connect(SYSLOG,$that) || die "connect: $!\n";
188 local($old) = select(SYSLOG); $| = 1; select($old);