7 @EXPORT = qw(openlog closelog setlogmask syslog);
14 # tom christiansen <tchrist@convex.com>
15 # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
16 # NOTE: openlog now takes three arguments, just like openlog(3)
18 # call syslog() with a string priority and a list of printf() args
23 # then (put these all in a script to test function)
25 # openlog($program,'cons,pid','user');
26 # syslog('info','this is another test');
27 # syslog('mail|warning','this is a better test: %d', time);
30 # syslog('debug','this is the last test');
31 # openlog("$program $$",'ndelay','user');
32 # syslog('notice','fooprogram: this is really done');
35 # syslog('info','problem was %m'); # %m == $! in syslog(3)
37 $host = 'localhost' unless $host; # set $Syslog::host to change
41 $maskpri = &LOG_UPTO(&LOG_DEBUG);
44 ($ident, $logopt, $facility) = @_; # package vars
45 $lo_pid = $logopt =~ /\bpid\b/;
46 $lo_ndelay = $logopt =~ /\bndelay\b/;
47 $lo_cons = $logopt =~ /\bcons\b/;
48 $lo_nowait = $logopt =~ /\bnowait\b/;
49 &connect if $lo_ndelay;
53 $facility = $ident = '';
58 local($oldmask) = $maskpri;
64 local($priority) = shift;
66 local($message, $whoami);
67 local(@words, $num, $numpri, $numfac, $sum);
68 local($facility) = $facility; # may need to change temporarily.
70 croak "syslog: expected both priority and mask" unless $mask && $priority;
72 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
76 $num = &xlate($_); # Translate word to number.
77 if (/^kern$/ || $num < 0) {
78 croak "syslog: invalid level/facility: $_";
80 elsif ($num <= &LOG_PRIMASK) {
81 croak "syslog: too many levels given: $_" if defined($numpri);
83 return 0 unless &LOG_MASK($numpri) & $maskpri;
86 croak "syslog: too many facilities given: $_" if defined($numfac);
92 croak "syslog: level must be given" unless defined($numpri);
94 if (!defined($numfac)) { # Facility not specified in this call.
95 $facility = 'user' unless $facility;
96 $numfac = &xlate($facility);
99 &connect unless $connected;
103 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
109 ($whoami = getlogin) ||
110 ($whoami = getpwuid($<)) ||
111 ($whoami = 'syslog');
114 $whoami .= "[$$]" if $lo_pid;
117 $mask .= "\n" unless $mask =~ /\n$/;
118 $message = sprintf ($mask, @_);
120 $sum = $numpri + $numfac;
121 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
124 unless ($lo_nowait) {
125 do {$died = wait;} until $died == $pid || $died < 0;
129 open(CONS,">/dev/console");
130 print CONS "<$facility.$priority>$whoami: $message\r";
131 exit if defined $pid; # if fork failed, we're parent
141 $name = "LOG_$name" unless $name =~ /^LOG_/;
142 $name = "Sys::Syslog::$name";
155 ($name,$aliases,$proto) = getprotobyname('udp');
158 ($name,$aliase,$port,$proto) = getservbyname('syslog','udp');
161 if (chop($myname = `hostname`)) {
162 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
163 croak "Can't lookup $myname" unless $name;
164 @bytes = unpack("C4",$addrs[0]);
169 $this = pack($pat, $af_inet, 0, @bytes);
171 if ($host =~ /^\d+\./) {
172 @bytes = split(/\./,$host);
175 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
176 croak "Can't lookup $host" unless $name;
177 @bytes = unpack("C4",$addrs[0]);
179 $that = pack($pat,$af_inet,$syslog,@bytes);
181 socket(SYSLOG,$af_inet,$datagram,$udp) || croak "socket: $!";
182 bind(SYSLOG,$this) || croak "bind: $!";
183 connect(SYSLOG,$that) || croak "connect: $!";
185 local($old) = select(SYSLOG); $| = 1; select($old);