This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.002gamma: lib/Pod/Functions.pm
[perl5.git] / lib / Sys / Syslog.pm
CommitLineData
a0d0e21e
LW
1package Sys::Syslog;
2require 5.000;
3require Exporter;
4use Carp;
5
6@ISA = qw(Exporter);
7@EXPORT = qw(openlog closelog setlogmask syslog);
8
37120919
AD
9use Socket;
10use Sys::Hostname;
11
a0d0e21e
LW
12#
13# syslog.pl
14#
15# $Log: syslog.pl,v $
16#
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)
20#
21# call syslog() with a string priority and a list of printf() args
22# like syslog(3)
23#
24# usage: use Syslog;
25#
26# then (put these all in a script to test function)
27#
28# openlog($program,'cons,pid','user');
29# syslog('info','this is another test');
30# syslog('mail|warning','this is a better test: %d', time);
31# closelog();
32#
33# syslog('debug','this is the last test');
34# openlog("$program $$",'ndelay','user');
35# syslog('notice','fooprogram: this is really done');
36#
37# $! = 55;
38# syslog('info','problem was %m'); # %m == $! in syslog(3)
39
37120919 40$host = hostname() unless $host; # set $Syslog::host to change
a0d0e21e
LW
41
42require 'syslog.ph';
43
44$maskpri = &LOG_UPTO(&LOG_DEBUG);
45
46sub openlog {
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;
53}
54
55sub closelog {
56 $facility = $ident = '';
57 &disconnect;
58}
59
60sub setlogmask {
61 local($oldmask) = $maskpri;
62 $maskpri = shift;
63 $oldmask;
64}
65
66sub syslog {
67 local($priority) = shift;
68 local($mask) = shift;
69 local($message, $whoami);
70 local(@words, $num, $numpri, $numfac, $sum);
71 local($facility) = $facility; # may need to change temporarily.
72
73 croak "syslog: expected both priority and mask" unless $mask && $priority;
74
75 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
76 undef $numpri;
77 undef $numfac;
78 foreach (@words) {
79 $num = &xlate($_); # Translate word to number.
80 if (/^kern$/ || $num < 0) {
81 croak "syslog: invalid level/facility: $_";
82 }
83 elsif ($num <= &LOG_PRIMASK) {
84 croak "syslog: too many levels given: $_" if defined($numpri);
85 $numpri = $num;
86 return 0 unless &LOG_MASK($numpri) & $maskpri;
87 }
88 else {
89 croak "syslog: too many facilities given: $_" if defined($numfac);
90 $facility = $_;
91 $numfac = $num;
92 }
93 }
94
95 croak "syslog: level must be given" unless defined($numpri);
96
97 if (!defined($numfac)) { # Facility not specified in this call.
98 $facility = 'user' unless $facility;
99 $numfac = &xlate($facility);
100 }
101
102 &connect unless $connected;
103
104 $whoami = $ident;
105
106 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
107 $whoami = $1;
108 $mask = $2;
109 }
110
111 unless ($whoami) {
112 ($whoami = getlogin) ||
113 ($whoami = getpwuid($<)) ||
114 ($whoami = 'syslog');
115 }
116
117 $whoami .= "[$$]" if $lo_pid;
118
119 $mask =~ s/%m/$!/g;
120 $mask .= "\n" unless $mask =~ /\n$/;
121 $message = sprintf ($mask, @_);
122
123 $sum = $numpri + $numfac;
124 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
125 if ($lo_cons) {
126 if ($pid = fork) {
127 unless ($lo_nowait) {
cb1a09d0 128 $died = waitpid($pid, 0);
a0d0e21e
LW
129 }
130 }
131 else {
132 open(CONS,">/dev/console");
133 print CONS "<$facility.$priority>$whoami: $message\r";
134 exit if defined $pid; # if fork failed, we're parent
135 close CONS;
136 }
137 }
138 }
139}
140
141sub xlate {
142 local($name) = @_;
143 $name =~ y/a-z/A-Z/;
144 $name = "LOG_$name" unless $name =~ /^LOG_/;
748a9306 145 $name = "Sys::Syslog::$name";
a0d0e21e
LW
146 eval(&$name) || -1;
147}
148
149sub connect {
cb1a09d0
AD
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: $!";
a0d0e21e
LW
156 local($old) = select(SYSLOG); $| = 1; select($old);
157 $connected = 1;
158}
159
160sub disconnect {
161 close SYSLOG;
162 $connected = 0;
163}
164
1651;
166