home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl502b.zip / lib / Sys / Syslog.pm < prev   
Text File  |  1995-12-28  |  4KB  |  167 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. use Sys::Hostname;
  11.  
  12. #
  13. # syslog.pl
  14. #
  15. # $Log:    syslog.pl,v $
  16. # tom christiansen <tchrist@convex.com>
  17. # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
  18. # NOTE: openlog now takes three arguments, just like openlog(3)
  19. #
  20. # call syslog() with a string priority and a list of printf() args
  21. # like syslog(3)
  22. #
  23. #  usage: use Syslog;
  24. #
  25. #  then (put these all in a script to test function)
  26. #        
  27. #    openlog($program,'cons,pid','user');
  28. #    syslog('info','this is another test');
  29. #    syslog('mail|warning','this is a better test: %d', time);
  30. #    closelog();
  31. #    
  32. #    syslog('debug','this is the last test');
  33. #    openlog("$program $$",'ndelay','user');
  34. #    syslog('notice','fooprogram: this is really done');
  35. #
  36. #    $! = 55;
  37. #    syslog('info','problem was %m'); # %m == $! in syslog(3)
  38.  
  39. $host = hostname() unless $host;    # set $Syslog::host to change
  40.  
  41. require 'syslog.ph';
  42.  
  43. $maskpri = &LOG_UPTO(&LOG_DEBUG);
  44.  
  45. sub openlog {
  46.     ($ident, $logopt, $facility) = @_;  # package vars
  47.     $lo_pid = $logopt =~ /\bpid\b/;
  48.     $lo_ndelay = $logopt =~ /\bndelay\b/;
  49.     $lo_cons = $logopt =~ /\bcons\b/;
  50.     $lo_nowait = $logopt =~ /\bnowait\b/;
  51.     &connect if $lo_ndelay;
  52.  
  53. sub closelog {
  54.     $facility = $ident = '';
  55.     &disconnect;
  56.  
  57. sub setlogmask {
  58.     local($oldmask) = $maskpri;
  59.     $maskpri = shift;
  60.     $oldmask;
  61. }
  62.  
  63. sub syslog {
  64.     local($priority) = shift;
  65.     local($mask) = shift;
  66.     local($message, $whoami);
  67.     local(@words, $num, $numpri, $numfac, $sum);
  68.     local($facility) = $facility;    # may need to change temporarily.
  69.  
  70.     croak "syslog: expected both priority and mask" unless $mask && $priority;
  71.  
  72.     @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
  73.     undef $numpri;
  74.     undef $numfac;
  75.     foreach (@words) {
  76.     $num = &xlate($_);        # Translate word to number.
  77.     if (/^kern$/ || $num < 0) {
  78.         croak "syslog: invalid level/facility: $_";
  79.     }
  80.     elsif ($num <= &LOG_PRIMASK) {
  81.         croak "syslog: too many levels given: $_" if defined($numpri);
  82.         $numpri = $num;
  83.         return 0 unless &LOG_MASK($numpri) & $maskpri;
  84.     }
  85.     else {
  86.         croak "syslog: too many facilities given: $_" if defined($numfac);
  87.         $facility = $_;
  88.         $numfac = $num;
  89.     }
  90.     }
  91.  
  92.     croak "syslog: level must be given" unless defined($numpri);
  93.  
  94.     if (!defined($numfac)) {    # Facility not specified in this call.
  95.     $facility = 'user' unless $facility;
  96.     $numfac = &xlate($facility);
  97.     }
  98.  
  99.     &connect unless $connected;
  100.  
  101.     $whoami = $ident;
  102.  
  103.     if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
  104.     $whoami = $1;
  105.     $mask = $2;
  106.     } 
  107.  
  108.     unless ($whoami) {
  109.     ($whoami = getlogin) ||
  110.         ($whoami = getpwuid($<)) ||
  111.         ($whoami = 'syslog');
  112.     }
  113.  
  114.     $whoami .= "[$$]" if $lo_pid;
  115.  
  116.     $mask =~ s/%m/$!/g;
  117.     $mask .= "\n" unless $mask =~ /\n$/;
  118.     $message = sprintf ($mask, @_);
  119.  
  120.     $sum = $numpri + $numfac;
  121.     unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
  122.     if ($lo_cons) {
  123.         if ($pid = fork) {
  124.         unless ($lo_nowait) {
  125.             $died = waitpid($pid, 0);
  126.         }
  127.         }
  128.         else {
  129.         open(CONS,">/dev/console");
  130.         print CONS "<$facility.$priority>$whoami: $message\r";
  131.         exit if defined $pid;        # if fork failed, we're parent
  132.         close CONS;
  133.         }
  134.     }
  135.     }
  136. }
  137.  
  138. sub xlate {
  139.     local($name) = @_;
  140.     $name =~ y/a-z/A-Z/;
  141.     $name = "LOG_$name" unless $name =~ /^LOG_/;
  142.     $name = "Sys::Syslog::$name";
  143.     eval(&$name) || -1;
  144. }
  145.  
  146. sub connect {
  147.     my $udp = getprotobyname('udp');
  148.     my $syslog = getservbyname('syslog','udp');
  149.     my $this = sockaddr_in($syslog, INADDR_ANY);
  150.     my $that = sockaddr_in($syslog, inet_aton($host) || croak "Can't lookup $host");
  151.     socket(SYSLOG,AF_INET,SOCK_DGRAM,$udp)          || croak "socket: $!";
  152.     connect(SYSLOG,$that)                  || croak "connect: $!";
  153.     local($old) = select(SYSLOG); $| = 1; select($old);
  154.     $connected = 1;
  155. }
  156.  
  157. sub disconnect {
  158.     close SYSLOG;
  159.     $connected = 0;
  160. }
  161.  
  162. 1;
  163.  
  164.