Fix VMS test to eliminate void context warning
[p5sagit/p5-mst-13.2.git] / lib / Sys / Syslog.pm
1 package Sys::Syslog;
2 require 5.000;
3 require Exporter;
4 use Carp;
5
6 @ISA = qw(Exporter);
7 @EXPORT = qw(openlog closelog setlogmask syslog);
8
9 use Socket;
10
11 # adapted from syslog.pl
12 #
13 # Tom Christiansen <tchrist@convex.com>
14 # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
15 # NOTE: openlog now takes three arguments, just like openlog(3)
16
17 =head1 NAME
18
19 Sys::Syslog, openlog, closelog, setlogmask, syslog - Perl interface to the UNIX syslog(3) calls
20
21 =head1 SYNOPSIS
22
23     use Sys::Syslog;
24
25     openlog $ident, $logopt, $facility;
26     syslog $priority, $mask, $format, @args;
27     $oldmask = setlogmask $mask_priority;
28     closelog;
29
30 =head1 DESCRIPTION
31
32 Sys::Syslog is an interface to the UNIX C<syslog(3)> program.
33 Call C<syslog()> with a string priority and a list of C<printf()> args
34 just like C<syslog(3)>.
35
36 Syslog provides the functions:
37
38 =over
39
40 =item openlog $ident, $logopt, $facility
41
42 I<$ident> is prepended to every message.
43 I<$logopt> contains one or more of the words I<pid>, I<ndelay>, I<cons>, I<nowait>.
44 I<$facility> specifies the part of the system
45
46 =item syslog $priority, $mask, $format, @args
47
48 If I<$priority> and I<$mask> permit, logs I<($format, @args)>
49 printed as by C<printf(3V)>, with the addition that I<%m>
50 is replaced with C<"$!"> (the latest error message).
51
52 =item setlogmask $mask_priority
53
54 Sets log mask I<$mask_priority> and returns the old mask.
55
56 =item closelog
57
58 Closes the log file.
59
60 =back
61
62 Note that C<openlog> now takes three arguments, just like C<openlog(3)>.
63
64 =head1 EXAMPLES
65
66     openlog($program, 'cons,pid', 'user');
67     syslog('info', 'this is another test');
68     syslog('mail|warning', 'this is a better test: %d', time);
69     closelog();
70
71     syslog('debug', 'this is the last test');
72     openlog("$program $$", 'ndelay', 'user');
73     syslog('notice', 'fooprogram: this is really done');
74
75     $! = 55;
76     syslog('info', 'problem was %m'); # %m == $! in syslog(3)
77
78 =head1 DEPENDENCIES
79
80 B<Sys::Syslog> needs F<syslog.ph>, which can be created with C<h2ph>.
81
82 =head1 SEE ALSO
83
84 L<syslog(3)>
85
86 =head1 AUTHOR
87
88 Tom Christiansen E<lt>F<tchrist@perl.com>E<gt> and Larry Wall E<lt>F<lwall@sems.com>E<gt>
89
90 =cut
91
92 $host = hostname() unless $host;        # set $Syslog::host to change
93
94 require 'syslog.ph';
95
96 $maskpri = &LOG_UPTO(&LOG_DEBUG);
97
98 sub openlog {
99     ($ident, $logopt, $facility) = @_;  # package vars
100     $lo_pid = $logopt =~ /\bpid\b/;
101     $lo_ndelay = $logopt =~ /\bndelay\b/;
102     $lo_cons = $logopt =~ /\bcons\b/;
103     $lo_nowait = $logopt =~ /\bnowait\b/;
104     &connect if $lo_ndelay;
105
106
107 sub closelog {
108     $facility = $ident = '';
109     &disconnect;
110
111
112 sub setlogmask {
113     local($oldmask) = $maskpri;
114     $maskpri = shift;
115     $oldmask;
116 }
117  
118 sub syslog {
119     local($priority) = shift;
120     local($mask) = shift;
121     local($message, $whoami);
122     local(@words, $num, $numpri, $numfac, $sum);
123     local($facility) = $facility;       # may need to change temporarily.
124
125     croak "syslog: expected both priority and mask" unless $mask && $priority;
126
127     @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
128     undef $numpri;
129     undef $numfac;
130     foreach (@words) {
131         $num = &xlate($_);              # Translate word to number.
132         if (/^kern$/ || $num < 0) {
133             croak "syslog: invalid level/facility: $_";
134         }
135         elsif ($num <= &LOG_PRIMASK) {
136             croak "syslog: too many levels given: $_" if defined($numpri);
137             $numpri = $num;
138             return 0 unless &LOG_MASK($numpri) & $maskpri;
139         }
140         else {
141             croak "syslog: too many facilities given: $_" if defined($numfac);
142             $facility = $_;
143             $numfac = $num;
144         }
145     }
146
147     croak "syslog: level must be given" unless defined($numpri);
148
149     if (!defined($numfac)) {    # Facility not specified in this call.
150         $facility = 'user' unless $facility;
151         $numfac = &xlate($facility);
152     }
153
154     &connect unless $connected;
155
156     $whoami = $ident;
157
158     if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
159         $whoami = $1;
160         $mask = $2;
161     } 
162
163     unless ($whoami) {
164         ($whoami = getlogin) ||
165             ($whoami = getpwuid($<)) ||
166                 ($whoami = 'syslog');
167     }
168
169     $whoami .= "[$$]" if $lo_pid;
170
171     $mask =~ s/%m/$!/g;
172     $mask .= "\n" unless $mask =~ /\n$/;
173     $message = sprintf ($mask, @_);
174
175     $sum = $numpri + $numfac;
176     unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
177         if ($lo_cons) {
178             if ($pid = fork) {
179                 unless ($lo_nowait) {
180                     $died = waitpid($pid, 0);
181                 }
182             }
183             else {
184                 open(CONS,">/dev/console");
185                 print CONS "<$facility.$priority>$whoami: $message\r";
186                 exit if defined $pid;           # if fork failed, we're parent
187                 close CONS;
188             }
189         }
190     }
191 }
192
193 sub xlate {
194     local($name) = @_;
195     $name =~ y/a-z/A-Z/;
196     $name = "LOG_$name" unless $name =~ /^LOG_/;
197     $name = "Sys::Syslog::$name";
198     eval(&$name) || -1;
199 }
200
201 sub connect {
202     unless ($host) {
203         require Sys::Hostname;
204         $host = Sys::Hostname::hostname();
205     }
206     my $udp = getprotobyname('udp');
207     my $syslog = getservbyname('syslog','udp');
208     my $this = sockaddr_in($syslog, INADDR_ANY);
209     my $that = sockaddr_in($syslog, inet_aton($host) || croak "Can't lookup $host");
210     socket(SYSLOG,AF_INET,SOCK_DGRAM,$udp)           || croak "socket: $!";
211     connect(SYSLOG,$that)                            || croak "connect: $!";
212     local($old) = select(SYSLOG); $| = 1; select($old);
213     $connected = 1;
214 }
215
216 sub disconnect {
217     close SYSLOG;
218     $connected = 0;
219 }
220
221 1;