7 @EXPORT = qw(openlog closelog setlogmask syslog);
17 # tom christiansen <tchrist@convex.com>
18 # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
19 # NOTE: openlog now takes three arguments, just like openlog(3)
21 # call syslog() with a string priority and a list of printf() args
26 # then (put these all in a script to test function)
28 # openlog($program,'cons,pid','user');
29 # syslog('info','this is another test');
30 # syslog('mail|warning','this is a better test: %d', time);
33 # syslog('debug','this is the last test');
34 # openlog("$program $$",'ndelay','user');
35 # syslog('notice','fooprogram: this is really done');
38 # syslog('info','problem was %m'); # %m == $! in syslog(3)
40 $host = hostname() unless $host; # set $Syslog::host to change
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 croak "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 croak "syslog: invalid level/facility: $_";
83 elsif ($num <= &LOG_PRIMASK) {
84 croak "syslog: too many levels given: $_" if defined($numpri);
86 return 0 unless &LOG_MASK($numpri) & $maskpri;
89 croak "syslog: too many facilities given: $_" if defined($numfac);
95 croak "syslog: level must be given" 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 $died = waitpid($pid, 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 = "Sys::Syslog::$name";
150 my $udp = getprotobyname('udp');
151 my $syslog = getservbyname('syslog','udp');
152 my $this = sockaddr_in($syslog, INADDR_ANY);
153 my $that = sockaddr_in($syslog, inet_aton($host) || croak "Can't lookup $host");
154 socket(SYSLOG,AF_INET,SOCK_DGRAM,$udp) || croak "socket: $!";
155 connect(SYSLOG,$that) || croak "connect: $!";
156 local($old) = select(SYSLOG); $| = 1; select($old);