1 warn "Legacy library @{[(caller(0))[6]]} will be removed from the Perl core distribution in the next major release. Please install it from the CPAN distribution Perl4::CoreLibs. It is being used at @{[(caller)[1]]}, line @{[(caller)[2]]}.\n";
8 # tom christiansen <tchrist@convex.com>
9 # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
10 # NOTE: openlog now takes three arguments, just like openlog(3)
12 # call syslog() with a string priority and a list of printf() args
15 # usage: require 'syslog.pl';
17 # then (put these all in a script to test function)
20 # do openlog($program,'cons,pid','user');
21 # do syslog('info','this is another test');
22 # do syslog('mail|warning','this is a better test: %d', time);
25 # do syslog('debug','this is the last test');
26 # do openlog("$program $$",'ndelay','user');
27 # do syslog('notice','fooprogram: this is really done');
30 # do syslog('info','problem was %m'); # %m == $! in syslog(3)
34 use warnings::register;
36 $host = 'localhost' unless $host; # set $syslog'host to change
38 if ($] >= 5 && warnings::enabled()) {
39 warnings::warn("You should 'use Sys::Syslog' instead; continuing");
44 eval 'use Socket; 1' ||
45 eval { require "socket.ph" } ||
46 require "sys/socket.ph";
48 $maskpri = &LOG_UPTO(&LOG_DEBUG);
51 ($ident, $logopt, $facility) = @_; # package vars
52 $lo_pid = $logopt =~ /\bpid\b/;
53 $lo_ndelay = $logopt =~ /\bndelay\b/;
54 $lo_cons = $logopt =~ /\bcons\b/;
55 $lo_nowait = $logopt =~ /\bnowait\b/;
56 &connect if $lo_ndelay;
60 $facility = $ident = '';
65 local($oldmask) = $maskpri;
71 local($priority) = shift;
73 local($message, $whoami);
74 local(@words, $num, $numpri, $numfac, $sum);
75 local($facility) = $facility; # may need to change temporarily.
77 die "syslog: expected both priority and mask" unless $mask && $priority;
79 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
83 $num = &xlate($_); # Translate word to number.
84 if (/^kern$/ || $num < 0) {
85 die "syslog: invalid level/facility: $_\n";
87 elsif ($num <= &LOG_PRIMASK) {
88 die "syslog: too many levels given: $_\n" if defined($numpri);
90 return 0 unless &LOG_MASK($numpri) & $maskpri;
93 die "syslog: too many facilities given: $_\n" if defined($numfac);
99 die "syslog: level must be given\n" unless defined($numpri);
101 if (!defined($numfac)) { # Facility not specified in this call.
102 $facility = 'user' unless $facility;
103 $numfac = &xlate($facility);
106 &connect unless $connected;
110 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
116 ($whoami = getlogin) ||
117 ($whoami = getpwuid($<)) ||
118 ($whoami = 'syslog');
121 $whoami .= "[$$]" if $lo_pid;
124 $mask .= "\n" unless $mask =~ /\n$/;
125 $message = sprintf ($mask, @_);
127 $sum = $numpri + $numfac;
128 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
131 unless ($lo_nowait) {
132 do {$died = wait;} until $died == $pid || $died < 0;
136 open(CONS,">/dev/console");
137 print CONS "<$facility.$priority>$whoami: $message\r";
138 exit if defined $pid; # if fork failed, we're parent
148 $name = "LOG_$name" unless $name =~ /^LOG_/;
149 $name = "syslog'$name";
150 defined &$name ? &$name : -1;
159 $stream = &SOCK_STREAM;
160 $datagram = &SOCK_DGRAM;
162 ($name,$aliases,$proto) = getprotobyname('udp');
165 ($name,$aliases,$port,$proto) = getservbyname('syslog','udp');
168 if (chop($myname = `hostname`)) {
169 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
170 die "Can't lookup $myname\n" unless $name;
171 @bytes = unpack("C4",$addrs[0]);
176 $this = pack($pat, $af_inet, 0, @bytes);
178 if ($host =~ /^\d+\./) {
179 @bytes = split(/\./,$host);
182 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
183 die "Can't lookup $host\n" unless $name;
184 @bytes = unpack("C4",$addrs[0]);
186 $that = pack($pat,$af_inet,$syslog,@bytes);
188 socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
189 bind(SYSLOG,$this) || die "bind: $!\n";
190 connect(SYSLOG,$that) || die "connect: $!\n";
192 local($old) = select(SYSLOG); $| = 1; select($old);