7 @EXPORT = qw(openlog closelog setlogmask setlogsock syslog);
12 # adapted from syslog.pl
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)
17 # Modified to add UNIX domain sockets by Sean Robinson <robinson_s@sc.maricopa.edu>
18 # with support from Tim Bunce <Tim.Bunce@ig.co.uk> and the perl5-porters mailing list
22 Sys::Syslog, openlog, closelog, setlogmask, setlogsock, syslog - Perl interface to the UNIX syslog(3) calls
28 setlogsock $sock_unix;
29 openlog $ident, $logopt, $facility;
30 syslog $priority, $format, @args;
31 $oldmask = setlogmask $mask_priority;
36 Sys::Syslog is an interface to the UNIX C<syslog(3)> program.
37 Call C<syslog()> with a string priority and a list of C<printf()> args
38 just like C<syslog(3)>.
40 Syslog provides the functions:
44 =item openlog $ident, $logopt, $facility
46 I<$ident> is prepended to every message.
47 I<$logopt> contains one or more of the words I<pid>, I<ndelay>, I<cons>, I<nowait>.
48 I<$facility> specifies the part of the system
50 =item syslog $priority, $format, @args
52 If I<$priority> permits, logs I<($format, @args)>
53 printed as by C<printf(3V)>, with the addition that I<%m>
54 is replaced with C<"$!"> (the latest error message).
56 =item setlogmask $mask_priority
58 Sets log mask I<$mask_priority> and returns the old mask.
60 =item setlogsock $sock_unix
62 Defines or undefines the boolean I<$sock_unix>;
63 a value equalling C<unix> defines I<$sock_unix>, a value
64 equalling C<inet >undefines I<$sock_unix>, and any other
66 The I<$sock_unix> boolean determines whether the next
67 call to C<openlog()> or C<syslog()> will connect to the
68 UNIX domain socket returned by C<_PATH_LOG> in
69 I<syslog.ph> or to the INET socket returned by
72 The default is for the INET socket to be used.
80 Note that C<openlog> now takes three arguments, just like C<openlog(3)>.
84 openlog($program, 'cons,pid', 'user');
85 syslog('info', 'this is another test');
86 syslog('mail|warning', 'this is a better test: %d', time);
89 syslog('debug', 'this is the last test');
92 openlog("$program $$", 'ndelay', 'user');
93 syslog('notice', 'fooprogram: this is really done');
97 syslog('info', 'problem was %m'); # %m == $! in syslog(3)
101 B<Sys::Syslog> needs F<syslog.ph>, which can be created with C<h2ph>.
109 Tom Christiansen E<lt>F<tchrist@perl.com>E<gt> and Larry Wall E<lt>F<larry@wall.org>E<gt>.
110 UNIX domain sockets added by Sean Robinson E<lt>F<robinson_s@sc.maricopa.edu>E<gt>
111 with support from Tim Bunce <Tim.Bunce@ig.co.uk> and the perl5-porters mailing list.
117 $maskpri = &LOG_UPTO(&LOG_DEBUG);
120 ($ident, $logopt, $facility) = @_; # package vars
121 $lo_pid = $logopt =~ /\bpid\b/;
122 $lo_ndelay = $logopt =~ /\bndelay\b/;
123 $lo_cons = $logopt =~ /\bcons\b/;
124 $lo_nowait = $logopt =~ /\bnowait\b/;
125 &connect if $lo_ndelay;
129 $facility = $ident = '';
134 local($oldmask) = $maskpri;
140 local($setsock) = shift;
141 if (lc($setsock) eq 'unix') {
143 } elsif (lc($setsock) eq 'inet') {
146 croak "Invalid argument passed to setlogsock; must be 'unix' or 'inet'";
151 local($priority) = shift;
152 local($mask) = shift;
153 local($message, $whoami);
154 local(@words, $num, $numpri, $numfac, $sum);
155 local($facility) = $facility; # may need to change temporarily.
157 croak "syslog: expected both priority and mask" unless $mask && $priority;
159 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
163 $num = &xlate($_); # Translate word to number.
164 if (/^kern$/ || $num < 0) {
165 croak "syslog: invalid level/facility: $_";
167 elsif ($num <= &LOG_PRIMASK) {
168 croak "syslog: too many levels given: $_" if defined($numpri);
170 return 0 unless &LOG_MASK($numpri) & $maskpri;
173 croak "syslog: too many facilities given: $_" if defined($numfac);
179 croak "syslog: level must be given" unless defined($numpri);
181 if (!defined($numfac)) { # Facility not specified in this call.
182 $facility = 'user' unless $facility;
183 $numfac = &xlate($facility);
186 &connect unless $connected;
190 if (!$whoami && $mask =~ /^(\S.*?):\s?(.*)/) {
196 ($whoami = getlogin) ||
197 ($whoami = getpwuid($<)) ||
198 ($whoami = 'syslog');
201 $whoami .= "[$$]" if $lo_pid;
204 $mask .= "\n" unless $mask =~ /\n$/;
205 $message = sprintf ($mask, @_);
207 $sum = $numpri + $numfac;
208 unless (send(SYSLOG,"<$sum>$whoami: $message\0",0)) {
211 unless ($lo_nowait) {
212 $died = waitpid($pid, 0);
216 open(CONS,">/dev/console");
217 print CONS "<$facility.$priority>$whoami: $message\r";
218 exit if defined $pid; # if fork failed, we're parent
228 $name = "LOG_$name" unless $name =~ /^LOG_/;
229 $name = "Sys::Syslog::$name";
230 defined &$name ? &$name : -1;
235 require Sys::Hostname;
236 my($host_uniq) = Sys::Hostname::hostname();
237 ($host) = $host_uniq =~ /([A-Za-z0-9_.-]+)/; # allow FQDN (inc _)
239 unless ( $sock_unix ) {
240 my $udp = getprotobyname('udp');
241 my $syslog = getservbyname('syslog','udp');
242 my $this = sockaddr_in($syslog, INADDR_ANY);
243 my $that = sockaddr_in($syslog, inet_aton($host) || croak "Can't lookup $host");
244 socket(SYSLOG,AF_INET,SOCK_DGRAM,$udp) || croak "socket: $!";
245 connect(SYSLOG,$that) || croak "connect: $!";
247 my $syslog = &_PATH_LOG || croak "_PATH_LOG not found in syslog.ph";
248 my $that = sockaddr_un($syslog) || croak "Can't locate $syslog";
249 socket(SYSLOG,AF_UNIX,SOCK_STREAM,0) || croak "open: $!";
250 connect(SYSLOG,$that) || croak "connect: $!";
252 local($old) = select(SYSLOG); $| = 1; select($old);