home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / Sys / Syslog.pm < prev   
Text File  |  1997-05-19  |  5KB  |  221 lines

  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, $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, $format, @args
  47.  
  48. If I<$priority> permits, 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<larry@wall.org>E<gt>
  89.  
  90. =cut
  91.  
  92. require 'syslog.ph';
  93.  
  94. $maskpri = &LOG_UPTO(&LOG_DEBUG);
  95.  
  96. sub openlog {
  97.     ($ident, $logopt, $facility) = @_;  # package vars
  98.     $lo_pid = $logopt =~ /\bpid\b/;
  99.     $lo_ndelay = $logopt =~ /\bndelay\b/;
  100.     $lo_cons = $logopt =~ /\bcons\b/;
  101.     $lo_nowait = $logopt =~ /\bnowait\b/;
  102.     &connect if $lo_ndelay;
  103.  
  104. sub closelog {
  105.     $facility = $ident = '';
  106.     &disconnect;
  107.  
  108. sub setlogmask {
  109.     local($oldmask) = $maskpri;
  110.     $maskpri = shift;
  111.     $oldmask;
  112. }
  113.  
  114. sub syslog {
  115.     local($priority) = shift;
  116.     local($mask) = shift;
  117.     local($message, $whoami);
  118.     local(@words, $num, $numpri, $numfac, $sum);
  119.     local($facility) = $facility;    # may need to change temporarily.
  120.  
  121.     croak "syslog: expected both priority and mask" unless $mask && $priority;
  122.  
  123.     @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
  124.     undef $numpri;
  125.     undef $numfac;
  126.     foreach (@words) {
  127.     $num = &xlate($_);        # Translate word to number.
  128.     if (/^kern$/ || $num < 0) {
  129.         croak "syslog: invalid level/facility: $_";
  130.     }
  131.     elsif ($num <= &LOG_PRIMASK) {
  132.         croak "syslog: too many levels given: $_" if defined($numpri);
  133.         $numpri = $num;
  134.         return 0 unless &LOG_MASK($numpri) & $maskpri;
  135.     }
  136.     else {
  137.         croak "syslog: too many facilities given: $_" if defined($numfac);
  138.         $facility = $_;
  139.         $numfac = $num;
  140.     }
  141.     }
  142.  
  143.     croak "syslog: level must be given" unless defined($numpri);
  144.  
  145.     if (!defined($numfac)) {    # Facility not specified in this call.
  146.     $facility = 'user' unless $facility;
  147.     $numfac = &xlate($facility);
  148.     }
  149.  
  150.     &connect unless $connected;
  151.  
  152.     $whoami = $ident;
  153.  
  154.     if (!$whoami && $mask =~ /^(\S.*?):\s?(.*)/) {
  155.     $whoami = $1;
  156.     $mask = $2;
  157.     } 
  158.  
  159.     unless ($whoami) {
  160.     ($whoami = getlogin) ||
  161.         ($whoami = getpwuid($<)) ||
  162.         ($whoami = 'syslog');
  163.     }
  164.  
  165.     $whoami .= "[$$]" if $lo_pid;
  166.  
  167.     $mask =~ s/%m/$!/g;
  168.     $mask .= "\n" unless $mask =~ /\n$/;
  169.     $message = sprintf ($mask, @_);
  170.  
  171.     $sum = $numpri + $numfac;
  172.     unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
  173.     if ($lo_cons) {
  174.         if ($pid = fork) {
  175.         unless ($lo_nowait) {
  176.             $died = waitpid($pid, 0);
  177.         }
  178.         }
  179.         else {
  180.         open(CONS,">/dev/console");
  181.         print CONS "<$facility.$priority>$whoami: $message\r";
  182.         exit if defined $pid;        # if fork failed, we're parent
  183.         close CONS;
  184.         }
  185.     }
  186.     }
  187. }
  188.  
  189. sub xlate {
  190.     local($name) = @_;
  191.     $name = uc $name;
  192.     $name = "LOG_$name" unless $name =~ /^LOG_/;
  193.     $name = "Sys::Syslog::$name";
  194.     defined &$name ? &$name : -1;
  195. }
  196.  
  197. sub connect {
  198.     unless ($host) {
  199.     require Sys::Hostname;
  200.     my($host_uniq) = Sys::Hostname::hostname();
  201.     ($host) = $host_uniq =~ /([\w\-]+)/;
  202.     }
  203.     my $udp = getprotobyname('udp');
  204.     my $syslog = getservbyname('syslog','udp');
  205.     my $this = sockaddr_in($syslog, INADDR_ANY);
  206.     my $that = sockaddr_in($syslog, inet_aton($host) || croak "Can't lookup $host");
  207.     socket(SYSLOG,AF_INET,SOCK_DGRAM,$udp)          || croak "socket: $!";
  208.     connect(SYSLOG,$that)                  || croak "connect: $!";
  209.     local($old) = select(SYSLOG); $| = 1; select($old);
  210.     $connected = 1;
  211. }
  212.  
  213. sub disconnect {
  214.     close SYSLOG;
  215.     $connected = 0;
  216. }
  217.  
  218. 1;
  219.