4 use warnings::register;
7 use Fcntl qw(O_WRONLY);
9 use POSIX qw(strftime setlocale LC_TIME);
18 standard => [qw(openlog syslog closelog setlogmask)],
19 extended => [qw(setlogsock)],
23 LOG_ALERT LOG_CRIT LOG_DEBUG LOG_EMERG LOG_ERR
24 LOG_INFO LOG_NOTICE LOG_WARNING
29 LOG_AUTH LOG_AUTHPRIV LOG_CRON LOG_DAEMON LOG_FTP LOG_KERN
30 LOG_LOCAL0 LOG_LOCAL1 LOG_LOCAL2 LOG_LOCAL3 LOG_LOCAL4
31 LOG_LOCAL5 LOG_LOCAL6 LOG_LOCAL7 LOG_LPR LOG_MAIL LOG_NEWS
32 LOG_SYSLOG LOG_USER LOG_UUCP
34 # Mac OS X specific facilities
35 qw( LOG_INSTALL LOG_LAUNCHD LOG_NETINFO LOG_RAS LOG_REMOTEAUTH ),
36 # modern BSD specific facilities
37 qw( LOG_CONSOLE LOG_NTP LOG_SECURITY ),
38 # IRIX specific facilities
39 qw( LOG_AUDIT LOG_LFMT ),
43 LOG_CONS LOG_PID LOG_NDELAY LOG_NOWAIT LOG_ODELAY LOG_PERROR
48 LOG_FACMASK LOG_NFACILITIES LOG_PRIMASK
55 @{$EXPORT_TAGS{standard}},
59 @{$EXPORT_TAGS{extended}},
60 @{$EXPORT_TAGS{macros}},
65 XSLoader::load('Sys::Syslog', $VERSION);
69 push @ISA, 'DynaLoader';
70 bootstrap Sys::Syslog $VERSION;
78 use vars qw($host); # host to send syslog messages to (see notes at end)
88 use vars qw($facility);
89 my $connected = 0; # flag to indicate if we're connected or not
90 my $syslog_send; # coderef of the function used to send messages
91 my $syslog_path = undef; # syslog path for "stream" and "unix" mechanisms
92 my $syslog_xobj = undef; # if defined, holds the external object used to send messages
93 my $transmit_ok = 0; # flag to indicate if the last message was transmited
94 my $sock_timeout = 0; # socket timeout, see below
95 my $current_proto = undef; # current mechanism used to transmit messages
96 my $ident = ''; # identifiant prepended to each message
97 $facility = ''; # current facility
98 my $maskpri = LOG_UPTO(&LOG_DEBUG); # current log mask
108 # Default is now to first use the native mechanism, so Perl programs
109 # behave like other normal Unix programs, then try other mechanisms.
110 my @connectMethods = qw(native tcp udp unix pipe stream console);
111 if ($^O =~ /^(freebsd|linux)$/) {
112 @connectMethods = grep { $_ ne 'udp' } @connectMethods;
115 # And on Win32 systems, we try to use the native mechanism for this
116 # platform, the events logger, available through Win32::EventLog.
118 my $is_Win32 = $^O =~ /Win32/i;
120 if (can_load("Sys::Syslog::Win32")) {
121 unshift @connectMethods, 'eventlog';
128 my @defaultMethods = @connectMethods;
129 my @fallbackMethods = ();
131 # The timeout in connection_ok() was pushed up to 0.25 sec in
132 # Sys::Syslog v0.19 in order to address a heisenbug on MacOSX:
133 # http://london.pm.org/pipermail/london.pm/Week-of-Mon-20061211/005961.html
135 # However, this also had the effect of slowing this test for
136 # all other operating systems, which apparently impacted some
137 # users (cf. CPAN-RT #34753). So, in order to make everybody
138 # happy, the timeout is now zero by default on all systems
139 # except on OSX where it is set to 250 msec, and can be set
140 # with the infamous setlogsock() function.
141 $sock_timeout = 0.25 if $^O =~ /darwin/;
143 # coderef for a nicer handling of errors
144 my $err_sub = $options{nofatal} ? \&warnings::warnif : \&croak;
148 # This AUTOLOAD is used to 'autoload' constants from the constant()
152 ($constname = $AUTOLOAD) =~ s/.*:://;
153 croak "Sys::Syslog::constant() not defined" if $constname eq 'constant';
154 my ($error, $val) = constant($constname);
155 croak $error if $error;
157 *$AUTOLOAD = sub { $val };
163 ($ident, my $logopt, $facility) = @_;
166 $ident ||= basename($0) || getlogin() || getpwuid($<) || 'syslog';
168 $facility ||= LOG_USER();
170 for my $opt (split /\b/, $logopt) {
171 $options{$opt} = 1 if exists $options{$opt}
174 $err_sub = delete $options{nofatal} ? \&warnings::warnif : \&croak;
175 return 1 unless $options{ndelay};
180 $facility = $ident = '';
185 my $oldmask = $maskpri;
186 $maskpri = shift unless $_[0] == 0;
191 my ($setsock, $setpath, $settime) = @_;
195 = "Invalid argument passed to setlogsock; must be 'stream', 'pipe', "
196 . "'unix', 'native', 'eventlog', 'tcp', 'udp' or 'inet'";
197 croak $diag_invalid_arg unless defined $setsock;
198 croak "Invalid number of arguments" unless @_ >= 1 and @_ <= 3;
200 $syslog_path = $setpath if defined $setpath;
201 $sock_timeout = $settime if defined $settime;
203 disconnect_log() if $connected;
205 @fallbackMethods = ();
206 @connectMethods = @defaultMethods;
208 if (ref $setsock eq 'ARRAY') {
209 @connectMethods = @$setsock;
211 } elsif (lc $setsock eq 'stream') {
212 if (not defined $syslog_path) {
213 my @try = qw(/dev/log /dev/conslog);
215 if (length &_PATH_LOG) { # Undefined _PATH_LOG is "".
216 unshift @try, &_PATH_LOG;
226 if (not defined $syslog_path) {
227 warnings::warnif "stream passed to setlogsock, but could not find any device";
232 if (not -w $syslog_path) {
233 warnings::warnif "stream passed to setlogsock, but $syslog_path is not writable";
236 @connectMethods = qw(stream);
239 } elsif (lc $setsock eq 'unix') {
240 if (length _PATH_LOG() || (defined $syslog_path && -w $syslog_path)) {
241 $syslog_path = _PATH_LOG() unless defined $syslog_path;
242 @connectMethods = qw(unix);
244 warnings::warnif 'unix passed to setlogsock, but path not available';
248 } elsif (lc $setsock eq 'pipe') {
249 for my $path ($syslog_path, &_PATH_LOG, "/dev/log") {
250 next unless defined $path and length $path and -p $path and -w _;
251 $syslog_path = $path;
255 if (not $syslog_path) {
256 warnings::warnif "pipe passed to setlogsock, but path not available";
260 @connectMethods = qw(pipe);
262 } elsif (lc $setsock eq 'native') {
263 @connectMethods = qw(native);
265 } elsif (lc $setsock eq 'eventlog') {
266 if (can_load("Win32::EventLog")) {
267 @connectMethods = qw(eventlog);
269 warnings::warnif "eventlog passed to setlogsock, but no Win32 API available";
274 } elsif (lc $setsock eq 'tcp') {
275 if (getservbyname('syslog', 'tcp') || getservbyname('syslogng', 'tcp')) {
276 @connectMethods = qw(tcp);
277 $host = $syslog_path;
279 warnings::warnif "tcp passed to setlogsock, but tcp service unavailable";
283 } elsif (lc $setsock eq 'udp') {
284 if (getservbyname('syslog', 'udp')) {
285 @connectMethods = qw(udp);
286 $host = $syslog_path;
288 warnings::warnif "udp passed to setlogsock, but udp service unavailable";
292 } elsif (lc $setsock eq 'inet') {
293 @connectMethods = ( 'tcp', 'udp' );
295 } elsif (lc $setsock eq 'console') {
296 @connectMethods = qw(console);
299 croak $diag_invalid_arg
306 my $priority = shift;
309 my (@words, $num, $numpri, $numfac, $sum);
311 my $fail_time = undef;
314 # if $ident is undefined, it means openlog() wasn't previously called
315 # so do it now in order to have sensible defaults
316 openlog() unless $ident;
318 local $facility = $facility; # may need to change temporarily.
320 croak "syslog: expecting argument \$priority" unless defined $priority;
321 croak "syslog: expecting argument \$format" unless defined $mask;
323 croak "syslog: invalid level/facility: $priority" if $priority =~ /^-\d+$/;
324 @words = split(/\W+/, $priority, 2); # Allow "level" or "level|facility".
328 for my $word (@words) {
329 next if length $word == 0;
331 $num = xlate($word); # Translate word to number.
334 croak "syslog: invalid level/facility: $word"
336 elsif ($num <= &LOG_PRIMASK) {
337 croak "syslog: too many levels given: $word" if defined $numpri;
339 return 0 unless LOG_MASK($numpri) & $maskpri;
342 croak "syslog: too many facilities given: $word" if defined $numfac;
348 croak "syslog: level must be given" unless defined $numpri;
350 if (not defined $numfac) { # Facility not specified in this call.
351 $facility = 'user' unless $facility;
352 $numfac = xlate($facility);
355 connect_log() unless $connected;
358 # escape percent signs for sprintf()
359 $error =~ s/%/%%/g if @_;
360 # replace %m with $error, if preceded by an even number of percent signs
361 $mask =~ s/(?<!%)((?:%%)*)%m/$1$error/g;
364 $mask .= "\n" unless $mask =~ /\n$/;
365 $message = @_ ? sprintf($mask, @_) : $mask;
367 # See CPAN-RT#24431. Opened on Apple Radar as bug #4944407 on 2007.01.21
368 # Supposedly resolved on Leopard.
369 chomp $message if $^O =~ /darwin/;
371 if ($current_proto eq 'native') {
374 elsif ($current_proto eq 'eventlog') {
379 $whoami .= "[$$]" if $options{pid};
381 $sum = $numpri + $numfac;
382 my $oldlocale = setlocale(LC_TIME);
383 setlocale(LC_TIME, 'C');
384 my $timestamp = strftime "%b %e %T", localtime;
385 setlocale(LC_TIME, $oldlocale);
386 $buf = "<$sum>$timestamp $whoami: $message\0";
389 # handle PERROR option
390 # "native" mechanism already handles it by itself
391 if ($options{perror} and $current_proto ne 'native') {
394 $whoami .= "[$$]" if $options{pid};
395 print STDERR "$whoami: $message\n";
398 # it's possible that we'll get an error from sending
399 # (e.g. if method is UDP and there is no UDP listener,
400 # then we'll get ECONNREFUSED on the send). So what we
401 # want to do at this point is to fallback onto a different
403 while (scalar @fallbackMethods || $syslog_send) {
404 if ($failed && (time - $fail_time) > 60) {
405 # it's been a while... maybe things have been fixed
406 @fallbackMethods = ();
408 $transmit_ok = 0; # make it look like a fresh attempt
412 if ($connected && !connection_ok()) {
413 # Something was OK, but has now broken. Remember coz we'll
414 # want to go back to what used to be OK.
415 $failed = $current_proto unless $failed;
420 connect_log() unless $connected;
421 $failed = undef if ($current_proto && $failed && $current_proto eq $failed);
424 if ($syslog_send->($buf, $numpri, $numfac)) {
428 # typically doesn't happen, since errors are rare from write().
432 # could not send, could not fallback onto a working
433 # connection method. Lose.
437 sub _syslog_send_console {
439 chop($buf); # delete the NUL from the end
440 # The console print is a method which could block
441 # so we do it in a child process and always return success
443 if (my $pid = fork) {
445 if ($options{nowait}) {
448 if (waitpid($pid, 0) >= 0) {
451 # it's possible that the caller has other
452 # plans for SIGCHLD, so let's not interfere
457 if (open(CONS, ">/dev/console")) {
458 my $ret = print CONS $buf . "\r"; # XXX: should this be \x0A ?
459 exit $ret if defined $pid;
462 exit if defined $pid;
466 sub _syslog_send_stream {
468 # XXX: this only works if the OS stream implementation makes a write
469 # look like a putmsg() with simple header. For instance it works on
470 # Solaris 8 but not Solaris 7.
471 # To be correct, it should use a STREAMS API, but perl doesn't have one.
472 return syswrite(SYSLOG, $buf, length($buf));
475 sub _syslog_send_pipe {
477 return print SYSLOG $buf;
480 sub _syslog_send_socket {
482 return syswrite(SYSLOG, $buf, length($buf));
483 #return send(SYSLOG, $buf, 0);
486 sub _syslog_send_native {
487 my ($buf, $numpri) = @_;
488 syslog_xs($numpri, $buf);
495 # private function to translate names to numeric values
500 return $name+0 if $name =~ /^\s*\d+\s*$/;
502 $name = "LOG_$name" unless $name =~ /^LOG_/;
504 # ExtUtils::Constant 0.20 introduced a new way to implement
505 # constants, called ProxySubs. When it was used to generate
506 # the C code, the constant() function no longer returns the
507 # correct value. Therefore, we first try a direct call to
508 # constant(), and if the value is an error we try to call the
509 # constant by its full name.
510 my $value = constant($name);
512 if (index($value, "not a valid") >= 0) {
513 $name = "Sys::Syslog::$name";
514 $value = eval { no strict "refs"; &$name };
515 $value = $@ unless defined $value;
518 $value = -1 if index($value, "not a valid") >= 0;
520 return defined $value ? $value : -1;
526 # This function acts as a kind of front-end: it tries to connect to
527 # a syslog service using the selected methods, trying each one in the
531 @fallbackMethods = @connectMethods unless scalar @fallbackMethods;
533 if ($transmit_ok && $current_proto) {
534 # Retry what we were on, because it has worked in the past.
535 unshift(@fallbackMethods, $current_proto);
542 while ($proto = shift @fallbackMethods) {
544 my $fn = "connect_$proto";
545 $connected = &$fn(\@errs) if defined &$fn;
551 $current_proto = $proto;
552 my ($old) = select(SYSLOG); $| = 1; select($old);
554 @fallbackMethods = ();
555 $err_sub->(join "\n\t- ", "no connection to syslog available", @errs);
563 my $tcp = getprotobyname('tcp');
565 push @$errs, "getprotobyname failed for tcp";
569 my $syslog = getservbyname('syslog', 'tcp');
570 $syslog = getservbyname('syslogng', 'tcp') unless defined $syslog;
571 if (!defined $syslog) {
572 push @$errs, "getservbyname failed for syslog/tcp and syslogng/tcp";
578 $addr = inet_aton($host);
580 push @$errs, "can't lookup $host";
584 $addr = INADDR_LOOPBACK;
586 $addr = sockaddr_in($syslog, $addr);
588 if (!socket(SYSLOG, AF_INET, SOCK_STREAM, $tcp)) {
589 push @$errs, "tcp socket: $!";
593 setsockopt(SYSLOG, SOL_SOCKET, SO_KEEPALIVE, 1);
594 if (silent_eval { IPPROTO_TCP() }) {
595 # These constants don't exist in 5.005. They were added in 1999
596 setsockopt(SYSLOG, IPPROTO_TCP(), TCP_NODELAY(), 1);
598 if (!connect(SYSLOG, $addr)) {
599 push @$errs, "tcp connect: $!";
603 $syslog_send = \&_syslog_send_socket;
611 my $udp = getprotobyname('udp');
613 push @$errs, "getprotobyname failed for udp";
617 my $syslog = getservbyname('syslog', 'udp');
618 if (!defined $syslog) {
619 push @$errs, "getservbyname failed for syslog/udp";
625 $addr = inet_aton($host);
627 push @$errs, "can't lookup $host";
631 $addr = INADDR_LOOPBACK;
633 $addr = sockaddr_in($syslog, $addr);
635 if (!socket(SYSLOG, AF_INET, SOCK_DGRAM, $udp)) {
636 push @$errs, "udp socket: $!";
639 if (!connect(SYSLOG, $addr)) {
640 push @$errs, "udp connect: $!";
644 # We want to check that the UDP connect worked. However the only
645 # way to do that is to send a message and see if an ICMP is returned
646 _syslog_send_socket("");
647 if (!connection_ok()) {
648 push @$errs, "udp connect: nobody listening";
652 $syslog_send = \&_syslog_send_socket;
659 # might want syslog_path to be variable based on syslog.h (if only
661 $syslog_path = '/dev/conslog' unless defined $syslog_path;
662 if (!-w $syslog_path) {
663 push @$errs, "stream $syslog_path is not writable";
666 if (!sysopen(SYSLOG, $syslog_path, O_WRONLY, 0400)) {
667 push @$errs, "stream can't open $syslog_path: $!";
670 $syslog_send = \&_syslog_send_stream;
677 $syslog_path ||= &_PATH_LOG || "/dev/log";
679 if (not -w $syslog_path) {
680 push @$errs, "$syslog_path is not writable";
684 if (not open(SYSLOG, ">$syslog_path")) {
685 push @$errs, "can't write to $syslog_path: $!";
689 $syslog_send = \&_syslog_send_pipe;
697 $syslog_path ||= _PATH_LOG() if length _PATH_LOG();
699 if (not defined $syslog_path) {
700 push @$errs, "_PATH_LOG not available in syslog.h and no user-supplied socket path";
704 if (not (-S $syslog_path or -c _)) {
705 push @$errs, "$syslog_path is not a socket";
709 my $addr = sockaddr_un($syslog_path);
711 push @$errs, "can't locate $syslog_path";
714 if (!socket(SYSLOG, AF_UNIX, SOCK_STREAM, 0)) {
715 push @$errs, "unix stream socket: $!";
719 if (!connect(SYSLOG, $addr)) {
720 if (!socket(SYSLOG, AF_UNIX, SOCK_DGRAM, 0)) {
721 push @$errs, "unix dgram socket: $!";
724 if (!connect(SYSLOG, $addr)) {
725 push @$errs, "unix dgram connect: $!";
730 $syslog_send = \&_syslog_send_socket;
739 # reconstruct the numeric equivalent of the options
740 for my $opt (keys %options) {
741 $logopt += xlate($opt) if $options{$opt}
744 openlog_xs($ident, $logopt, xlate($facility));
745 $syslog_send = \&_syslog_send_native;
750 sub connect_eventlog {
753 $syslog_xobj = Sys::Syslog::Win32::_install();
754 $syslog_send = \&Sys::Syslog::Win32::_syslog_send;
759 sub connect_console {
761 if (!-w '/dev/console') {
762 push @$errs, "console is not writable";
765 $syslog_send = \&_syslog_send_console;
769 # To test if the connection is still good, we need to check if any
770 # errors are present on the connection. The errors will not be raised
771 # by a write. Instead, sockets are made readable and the next read
772 # would cause the error to be returned. Unfortunately the syslog
773 # 'protocol' never provides anything for us to read. But with
774 # judicious use of select(), we can see if it would be readable...
776 return 1 if defined $current_proto and (
777 $current_proto eq 'native' or $current_proto eq 'console'
778 or $current_proto eq 'eventlog'
782 vec($rin, fileno(SYSLOG), 1) = 1;
783 my $ret = select $rin, undef, $rin, $sock_timeout;
784 return ($ret ? 0 : 1);
789 $syslog_send = undef;
791 if (defined $current_proto and $current_proto eq 'native') {
795 elsif (defined $current_proto and $current_proto eq 'eventlog') {
796 $syslog_xobj->Close();
805 # Wrappers around eval() that makes sure that nobody, and I say NOBODY,
806 # ever knows that I wanted to test if something was here or not.
807 # It is needed because some applications are trying to be too smart,
808 # do it wrong, and it ends up in EPIC FAIL.
809 # Yes I'm speaking of YOU, SpamAssassin.
811 sub silent_eval (&) {
812 local($SIG{__DIE__}, $SIG{__WARN__}, $@);
813 return eval { $_[0]->() }
817 local($SIG{__DIE__}, $SIG{__WARN__}, $@);
818 return eval "use $_[0]; 1"
822 "Eighth Rule: read the documentation."
828 Sys::Syslog - Perl interface to the UNIX syslog(3) calls
836 use Sys::Syslog; # all except setlogsock(), or:
837 use Sys::Syslog qw(:DEFAULT setlogsock); # default set, plus setlogsock()
838 use Sys::Syslog qw(:standard :macros); # standard functions, plus macros
840 openlog $ident, $logopt, $facility; # don't forget this
841 syslog $priority, $format, @args;
842 $oldmask = setlogmask $mask_priority;
848 C<Sys::Syslog> is an interface to the UNIX C<syslog(3)> program.
849 Call C<syslog()> with a string priority and a list of C<printf()> args
850 just like C<syslog(3)>.
852 You can find a kind of FAQ in L<"THE RULES OF SYS::SYSLOG">. Please read
853 it before coding, and again before asking questions.
858 C<Sys::Syslog> exports the following C<Exporter> tags:
864 C<:standard> exports the standard C<syslog(3)> functions:
866 openlog closelog setlogmask syslog
870 C<:extended> exports the Perl specific functions for C<syslog(3)>:
876 C<:macros> exports the symbols corresponding to most of your C<syslog(3)>
877 macros and the C<LOG_UPTO()> and C<LOG_MASK()> functions.
878 See L<"CONSTANTS"> for the supported constants and their meaning.
882 By default, C<Sys::Syslog> exports the symbols from the C<:standard> tag.
889 =item B<openlog($ident, $logopt, $facility)>
892 C<$ident> is prepended to every message. C<$logopt> contains zero or
893 more of the options detailed below. C<$facility> specifies the part
894 of the system to report about, for example C<LOG_USER> or C<LOG_LOCAL0>:
895 see L<"Facilities"> for a list of well-known facilities, and your
896 C<syslog(3)> documentation for the facilities available in your system.
897 Check L<"SEE ALSO"> for useful links. Facility can be given as a string
900 This function will croak if it can't connect to the syslog daemon.
902 Note that C<openlog()> now takes three arguments, just like C<openlog(3)>.
904 B<You should use C<openlog()> before calling C<syslog()>.>
912 C<cons> - This option is ignored, since the failover mechanism will drop
913 down to the console automatically if all other media fail.
917 C<ndelay> - Open the connection immediately (normally, the connection is
918 opened when the first message is logged).
922 C<nofatal> - When set to true, C<openlog()> and C<syslog()> will only
923 emit warnings instead of dying if the connection to the syslog can't
928 C<nowait> - Don't wait for child processes that may have been created
929 while logging the message. (The GNU C library does not create a child
930 process, so this option has no effect on Linux.)
934 C<perror> - Write the message to standard error output as well to the
939 C<pid> - Include PID with each message.
945 Open the syslog with options C<ndelay> and C<pid>, and with facility C<LOCAL0>:
947 openlog($name, "ndelay,pid", "local0");
949 Same thing, but this time using the macro corresponding to C<LOCAL0>:
951 openlog($name, "ndelay,pid", LOG_LOCAL0);
954 =item B<syslog($priority, $message)>
956 =item B<syslog($priority, $format, @args)>
958 If C<$priority> permits, logs C<$message> or C<sprintf($format, @args)>
959 with the addition that C<%m> in $message or C<$format> is replaced with
960 C<"$!"> (the latest error message).
962 C<$priority> can specify a level, or a level and a facility. Levels and
963 facilities can be given as strings or as macros. When using the C<eventlog>
964 mechanism, priorities C<DEBUG> and C<INFO> are mapped to event type
965 C<informational>, C<NOTICE> and C<WARNIN> to C<warning> and C<ERR> to
966 C<EMERG> to C<error>.
968 If you didn't use C<openlog()> before using C<syslog()>, C<syslog()> will
969 try to guess the C<$ident> by extracting the shortest prefix of
970 C<$format> that ends in a C<":">.
974 syslog("info", $message); # informational level
975 syslog(LOG_INFO, $message); # informational level
977 syslog("info|local0", $message); # information level, Local0 facility
978 syslog(LOG_INFO|LOG_LOCAL0, $message); # information level, Local0 facility
984 C<Sys::Syslog> version v0.07 and older passed the C<$message> as the
985 formatting string to C<sprintf()> even when no formatting arguments
986 were provided. If the code calling C<syslog()> might execute with
987 older versions of this module, make sure to call the function as
988 C<syslog($priority, "%s", $message)> instead of C<syslog($priority,
989 $message)>. This protects against hostile formatting sequences that
990 might show up if $message contains tainted data.
995 =item B<setlogmask($mask_priority)>
997 Sets the log mask for the current process to C<$mask_priority> and
998 returns the old mask. If the mask argument is 0, the current log mask
999 is not modified. See L<"Levels"> for the list of available levels.
1000 You can use the C<LOG_UPTO()> function to allow all levels up to a
1001 given priority (but it only accept the numeric macros as arguments).
1007 setlogmask( LOG_MASK(LOG_ERR) );
1009 Log everything except informational messages:
1011 setlogmask( ~(LOG_MASK(LOG_INFO)) );
1013 Log critical messages, errors and warnings:
1015 setlogmask( LOG_MASK(LOG_CRIT) | LOG_MASK(LOG_ERR) | LOG_MASK(LOG_WARNING) );
1017 Log all messages up to debug:
1019 setlogmask( LOG_UPTO(LOG_DEBUG) );
1022 =item B<setlogsock($sock_type)>
1024 =item B<setlogsock($sock_type, $stream_location)> (added in Perl 5.004_02)
1026 =item B<setlogsock($sock_type, $stream_location, $sock_timeout)> (added in 0.25)
1028 Sets the socket type to be used for the next call to
1029 C<openlog()> or C<syslog()> and returns true on success,
1030 C<undef> on failure. The available mechanisms are:
1036 C<"native"> - use the native C functions from your C<syslog(3)> library
1037 (added in C<Sys::Syslog> 0.15).
1041 C<"eventlog"> - send messages to the Win32 events logger (Win32 only;
1042 added in C<Sys::Syslog> 0.19).
1046 C<"tcp"> - connect to a TCP socket, on the C<syslog/tcp> or C<syslogng/tcp>
1047 service. If defined, the second parameter is used as a hostname to connect to.
1051 C<"udp"> - connect to a UDP socket, on the C<syslog/udp> service.
1052 If defined, the second parameter is used as a hostname to connect to,
1053 and the third parameter as the timeout used to check for UDP response.
1057 C<"inet"> - connect to an INET socket, either TCP or UDP, tried in that
1058 order. If defined, the second parameter is used as a hostname to connect to.
1062 C<"unix"> - connect to a UNIX domain socket (in some systems a character
1063 special device). The name of that socket is the second parameter or, if
1064 you omit the second parameter, the value returned by the C<_PATH_LOG> macro
1065 (if your system defines it), or F</dev/log> or F</dev/conslog>, whatever is
1070 C<"stream"> - connect to the stream indicated by the pathname provided as
1071 the optional second parameter, or, if omitted, to F</dev/conslog>.
1072 For example Solaris and IRIX system may prefer C<"stream"> instead of C<"unix">.
1076 C<"pipe"> - connect to the named pipe indicated by the pathname provided as
1077 the optional second parameter, or, if omitted, to the value returned by
1078 the C<_PATH_LOG> macro (if your system defines it), or F</dev/log>
1079 (added in C<Sys::Syslog> 0.21).
1083 C<"console"> - send messages directly to the console, as for the C<"cons">
1084 option of C<openlog()>.
1088 A reference to an array can also be passed as the first parameter.
1089 When this calling method is used, the array should contain a list of
1090 mechanisms which are attempted in order.
1092 The default is to try C<native>, C<tcp>, C<udp>, C<unix>, C<pipe>, C<stream>,
1094 Under systems with the Win32 API, C<eventlog> will be added as the first
1095 mechanism to try if C<Win32::EventLog> is available.
1097 Giving an invalid value for C<$sock_type> will C<croak>.
1101 Select the UDP socket mechanism:
1105 Select the native, UDP socket then UNIX domain socket mechanisms:
1107 setlogsock(["native", "udp", "unix"]);
1113 Now that the "native" mechanism is supported by C<Sys::Syslog> and selected
1114 by default, the use of the C<setlogsock()> function is discouraged because
1115 other mechanisms are less portable across operating systems. Authors of
1116 modules and programs that use this function, especially its cargo-cult form
1117 C<setlogsock("unix")>, are advised to remove any occurence of it unless they
1118 specifically want to use a given mechanism (like TCP or UDP to connect to
1125 Closes the log file and returns true on success.
1130 =head1 THE RULES OF SYS::SYSLOG
1132 I<The First Rule of Sys::Syslog is:>
1133 You do not call C<setlogsock>.
1135 I<The Second Rule of Sys::Syslog is:>
1136 You B<do not> call C<setlogsock>.
1138 I<The Third Rule of Sys::Syslog is:>
1139 The program crashes, C<die>s, calls C<closelog>, the log is over.
1141 I<The Fourth Rule of Sys::Syslog is:>
1142 One facility, one priority.
1144 I<The Fifth Rule of Sys::Syslog is:>
1147 I<The Sixth Rule of Sys::Syslog is:>
1148 No C<syslog> before C<openlog>.
1150 I<The Seventh Rule of Sys::Syslog is:>
1151 Logs will go on as long as they have to.
1153 I<The Eighth, and Final Rule of Sys::Syslog is:>
1154 If this is your first use of Sys::Syslog, you must read the doc.
1161 openlog($program, 'cons,pid', 'user');
1162 syslog('info', '%s', 'this is another test');
1163 syslog('mail|warning', 'this is a better test: %d', time);
1166 syslog('debug', 'this is the last test');
1170 openlog("$program $$", 'ndelay', 'user');
1171 syslog('notice', 'fooprogram: this is really done');
1173 Example of use of C<%m>:
1176 syslog('info', 'problem was %m'); # %m == $! in syslog(3)
1178 Log to UDP port on C<$remotehost> instead of logging locally:
1180 setlogsock("udp", $remotehost);
1181 openlog($program, 'ndelay', 'user');
1182 syslog('info', 'something happened over here');
1193 C<LOG_AUDIT> - audit daemon (IRIX); falls back to C<LOG_AUTH>
1197 C<LOG_AUTH> - security/authorization messages
1201 C<LOG_AUTHPRIV> - security/authorization messages (private)
1205 C<LOG_CONSOLE> - C</dev/console> output (FreeBSD); falls back to C<LOG_USER>
1209 C<LOG_CRON> - clock daemons (B<cron> and B<at>)
1213 C<LOG_DAEMON> - system daemons without separate facility value
1217 C<LOG_FTP> - FTP daemon
1221 C<LOG_KERN> - kernel messages
1225 C<LOG_INSTALL> - installer subsystem (Mac OS X); falls back to C<LOG_USER>
1229 C<LOG_LAUNCHD> - launchd - general bootstrap daemon (Mac OS X);
1230 falls back to C<LOG_DAEMON>
1234 C<LOG_LFMT> - logalert facility; falls back to C<LOG_USER>
1238 C<LOG_LOCAL0> through C<LOG_LOCAL7> - reserved for local use
1242 C<LOG_LPR> - line printer subsystem
1246 C<LOG_MAIL> - mail subsystem
1250 C<LOG_NETINFO> - NetInfo subsystem (Mac OS X); falls back to C<LOG_DAEMON>
1254 C<LOG_NEWS> - USENET news subsystem
1258 C<LOG_NTP> - NTP subsystem (FreeBSD, NetBSD); falls back to C<LOG_DAEMON>
1262 C<LOG_RAS> - Remote Access Service (VPN / PPP) (Mac OS X);
1263 falls back to C<LOG_AUTH>
1267 C<LOG_REMOTEAUTH> - remote authentication/authorization (Mac OS X);
1268 falls back to C<LOG_AUTH>
1272 C<LOG_SECURITY> - security subsystems (firewalling, etc.) (FreeBSD);
1273 falls back to C<LOG_AUTH>
1277 C<LOG_SYSLOG> - messages generated internally by B<syslogd>
1281 C<LOG_USER> (default) - generic user-level messages
1285 C<LOG_UUCP> - UUCP subsystem
1296 C<LOG_EMERG> - system is unusable
1300 C<LOG_ALERT> - action must be taken immediately
1304 C<LOG_CRIT> - critical conditions
1308 C<LOG_ERR> - error conditions
1312 C<LOG_WARNING> - warning conditions
1316 C<LOG_NOTICE> - normal, but significant, condition
1320 C<LOG_INFO> - informational message
1324 C<LOG_DEBUG> - debug-level message
1333 =item C<Invalid argument passed to setlogsock>
1335 B<(F)> You gave C<setlogsock()> an invalid value for C<$sock_type>.
1337 =item C<eventlog passed to setlogsock, but no Win32 API available>
1339 B<(W)> You asked C<setlogsock()> to use the Win32 event logger but the
1340 operating system running the program isn't Win32 or does not provides Win32
1341 compatible facilities.
1343 =item C<no connection to syslog available>
1345 B<(F)> C<syslog()> failed to connect to the specified socket.
1347 =item C<stream passed to setlogsock, but %s is not writable>
1349 B<(W)> You asked C<setlogsock()> to use a stream socket, but the given
1350 path is not writable.
1352 =item C<stream passed to setlogsock, but could not find any device>
1354 B<(W)> You asked C<setlogsock()> to use a stream socket, but didn't
1355 provide a path, and C<Sys::Syslog> was unable to find an appropriate one.
1357 =item C<tcp passed to setlogsock, but tcp service unavailable>
1359 B<(W)> You asked C<setlogsock()> to use a TCP socket, but the service
1360 is not available on the system.
1362 =item C<syslog: expecting argument %s>
1364 B<(F)> You forgot to give C<syslog()> the indicated argument.
1366 =item C<syslog: invalid level/facility: %s>
1368 B<(F)> You specified an invalid level or facility.
1370 =item C<syslog: too many levels given: %s>
1372 B<(F)> You specified too many levels.
1374 =item C<syslog: too many facilities given: %s>
1376 B<(F)> You specified too many facilities.
1378 =item C<syslog: level must be given>
1380 B<(F)> You forgot to specify a level.
1382 =item C<udp passed to setlogsock, but udp service unavailable>
1384 B<(W)> You asked C<setlogsock()> to use a UDP socket, but the service
1385 is not available on the system.
1387 =item C<unix passed to setlogsock, but path not available>
1389 B<(W)> You asked C<setlogsock()> to use a UNIX socket, but C<Sys::Syslog>
1390 was unable to find an appropriate an appropriate device.
1401 SUSv3 issue 6, IEEE Std 1003.1, 2004 edition,
1402 L<http://www.opengroup.org/onlinepubs/000095399/basedefs/syslog.h.html>
1404 GNU C Library documentation on syslog,
1405 L<http://www.gnu.org/software/libc/manual/html_node/Syslog.html>
1407 Solaris 10 documentation on syslog,
1408 L<http://docs.sun.com/app/docs/doc/816-5168/syslog-3c?a=view>
1410 Mac OS X documentation on syslog,
1411 L<http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/syslog.3.html>
1413 IRIX 6.5 documentation on syslog,
1414 L<http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=0650&db=man&fname=3c+syslog>
1416 AIX 5L 5.3 documentation on syslog,
1417 L<http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf2/syslog.htm>
1419 HP-UX 11i documentation on syslog,
1420 L<http://docs.hp.com/en/B2355-60130/syslog.3C.html>
1422 Tru64 5.1 documentation on syslog,
1423 L<http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V51_HTML/MAN/MAN3/0193____.HTM>
1426 L<http://stratadoc.stratus.com/vos/15.1.1/r502-01/wwhelp/wwhimpl/js/html/wwhelp.htm?context=r502-01&file=ch5r502-01bi.html>
1430 I<RFC 3164 - The BSD syslog Protocol>, L<http://www.faqs.org/rfcs/rfc3164.html>
1431 -- Please note that this is an informational RFC, and therefore does not
1432 specify a standard of any kind.
1434 I<RFC 3195 - Reliable Delivery for syslog>, L<http://www.faqs.org/rfcs/rfc3195.html>
1438 I<Syslogging with Perl>, L<http://lexington.pm.org/meetings/022001.html>
1443 L<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wes/wes/windows_event_log.asp>
1446 =head1 AUTHORS & ACKNOWLEDGEMENTS
1448 Tom Christiansen E<lt>F<tchrist (at) perl.com>E<gt> and Larry Wall
1449 E<lt>F<larry (at) wall.org>E<gt>.
1451 UNIX domain sockets added by Sean Robinson
1452 E<lt>F<robinson_s (at) sc.maricopa.edu>E<gt> with support from Tim Bunce
1453 E<lt>F<Tim.Bunce (at) ig.co.uk>E<gt> and the C<perl5-porters> mailing list.
1455 Dependency on F<syslog.ph> replaced with XS code by Tom Hughes
1456 E<lt>F<tom (at) compton.nu>E<gt>.
1458 Code for C<constant()>s regenerated by Nicholas Clark E<lt>F<nick (at) ccl4.org>E<gt>.
1460 Failover to different communication modes by Nick Williams
1461 E<lt>F<Nick.Williams (at) morganstanley.com>E<gt>.
1463 Extracted from core distribution for publishing on the CPAN by
1464 SE<eacute>bastien Aperghis-Tramoni E<lt>sebastien (at) aperghis.netE<gt>.
1466 XS code for using native C functions borrowed from C<L<Unix::Syslog>>,
1467 written by Marcus Harnisch E<lt>F<marcus.harnisch (at) gmx.net>E<gt>.
1469 Yves Orton suggested and helped for making C<Sys::Syslog> use the native
1470 event logger under Win32 systems.
1472 Jerry D. Hedden and Reini Urban provided greatly appreciated help to
1473 debug and polish C<Sys::Syslog> under Cygwin.
1478 Please report any bugs or feature requests to
1479 C<bug-sys-syslog (at) rt.cpan.org>, or through the web interface at
1480 L<http://rt.cpan.org/Public/Dist/Display.html?Name=Sys-Syslog>.
1481 I will be notified, and then you'll automatically be notified of progress on
1482 your bug as I make changes.
1487 You can find documentation for this module with the perldoc command.
1491 You can also look for information at:
1495 =item * AnnoCPAN: Annotated CPAN documentation
1497 L<http://annocpan.org/dist/Sys-Syslog>
1499 =item * CPAN Ratings
1501 L<http://cpanratings.perl.org/d/Sys-Syslog>
1503 =item * RT: CPAN's request tracker
1505 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Sys-Syslog>
1509 L<http://search.cpan.org/dist/Sys-Syslog/>
1511 =item * Kobes' CPAN Search
1513 L<http://cpan.uwinnipeg.ca/dist/Sys-Syslog>
1515 =item * Perl Documentation
1517 L<http://perldoc.perl.org/Sys/Syslog.html>
1524 Copyright (C) 1990-2008 by Larry Wall and others.
1529 This program is free software; you can redistribute it and/or modify it
1530 under the same terms as Perl itself.
1536 Notes for the future maintainer (even if it's still me..)
1537 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1539 Using Google Code Search, I search who on Earth was relying on $host being
1540 public. It found 5 hits:
1542 * First was inside Indigo Star Perl2exe documentation. Just an old version
1546 * One real hit was inside DalWeathDB, a weather related program. It simply
1549 $Sys::Syslog::host = '127.0.0.1';
1551 - L<http://www.gallistel.net/nparker/weather/code/>
1554 * Two hits were in TPC, a fax server thingy. It does a
1556 $Sys::Syslog::host = $TPC::LOGHOST;
1558 but also has this strange piece of code:
1560 # work around perl5.003 bug
1561 sub Sys::Syslog::hostname {}
1563 I don't know what bug the author referred to.
1565 - L<http://www.tpc.int/>
1566 - L<ftp://ftp.tpc.int/tpc/server/UNIX/>
1567 - L<ftp://ftp-usa.tpc.int/pub/tpc/server/UNIX/>
1570 * Last hit was in Filefix, which seems to be a FIDOnet mail program (!).
1571 This one does not use $host, but has the following piece of code:
1573 sub Sys::Syslog::hostname
1579 I guess this was a more elaborate form of the previous bit, maybe because
1580 of a bug in Sys::Syslog back then?
1582 - L<ftp://ftp.kiae.su/pub/unix/fido/>
1588 - L<http://www.openss7.org/streams.html>
1590 II12021: SYSLOGD HOWTO TCPIPINFO (z/OS, OS/390, MVS)
1591 - L<http://www-1.ibm.com/support/docview.wss?uid=isg1II12021>
1593 Getting the most out of the Event Viewer
1594 - L<http://www.codeproject.com/dotnet/evtvwr.asp?print=true>
1596 Log events to the Windows NT Event Log with JNI
1597 - L<http://www.javaworld.com/javaworld/jw-09-2001/jw-0928-ntmessages.html>