home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl560.zip / lib / syslog.pl < prev    next >
Perl Script  |  2000-03-12  |  5KB  |  200 lines

  1. #
  2. # syslog.pl
  3. #
  4. # $Log:    syslog.pl,v $
  5. # tom christiansen <tchrist@convex.com>
  6. # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
  7. # NOTE: openlog now takes three arguments, just like openlog(3)
  8. #
  9. # call syslog() with a string priority and a list of printf() args
  10. # like syslog(3)
  11. #
  12. #  usage: require 'syslog.pl';
  13. #
  14. #  then (put these all in a script to test function)
  15. #        
  16. #
  17. #    do openlog($program,'cons,pid','user');
  18. #    do syslog('info','this is another test');
  19. #    do syslog('mail|warning','this is a better test: %d', time);
  20. #    do closelog();
  21. #    
  22. #    do syslog('debug','this is the last test');
  23. #    do openlog("$program $$",'ndelay','user');
  24. #    do syslog('notice','fooprogram: this is really done');
  25. #
  26. #    $! = 55;
  27. #    do syslog('info','problem was %m'); # %m == $! in syslog(3)
  28.  
  29. package syslog;
  30.  
  31. use warnings::register;
  32.  
  33. $host = 'localhost' unless $host;    # set $syslog'host to change
  34.  
  35. if ($] >= 5 && warnings::enabled()) {
  36.     warnings::warn "You should 'use Sys::Syslog' instead; continuing";
  37.  
  38. require 'syslog.ph';
  39.  
  40.  eval 'use Socket; 1'             ||
  41.      eval { require "socket.ph" }     ||
  42.      require "sys/socket.ph";
  43.  
  44. $maskpri = &LOG_UPTO(&LOG_DEBUG);
  45.  
  46. sub main'openlog {
  47.     ($ident, $logopt, $facility) = @_;  # package vars
  48.     $lo_pid = $logopt =~ /\bpid\b/;
  49.     $lo_ndelay = $logopt =~ /\bndelay\b/;
  50.     $lo_cons = $logopt =~ /\bcons\b/;
  51.     $lo_nowait = $logopt =~ /\bnowait\b/;
  52.     &connect if $lo_ndelay;
  53.  
  54. sub main'closelog {
  55.     $facility = $ident = '';
  56.     &disconnect;
  57.  
  58. sub main'setlogmask {
  59.     local($oldmask) = $maskpri;
  60.     $maskpri = shift;
  61.     $oldmask;
  62. }
  63.  
  64. sub main'syslog {
  65.     local($priority) = shift;
  66.     local($mask) = shift;
  67.     local($message, $whoami);
  68.     local(@words, $num, $numpri, $numfac, $sum);
  69.     local($facility) = $facility;    # may need to change temporarily.
  70.  
  71.     die "syslog: expected both priority and mask" unless $mask && $priority;
  72.  
  73.     @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
  74.     undef $numpri;
  75.     undef $numfac;
  76.     foreach (@words) {
  77.     $num = &xlate($_);        # Translate word to number.
  78.     if (/^kern$/ || $num < 0) {
  79.         die "syslog: invalid level/facility: $_\n";
  80.     }
  81.     elsif ($num <= &LOG_PRIMASK) {
  82.         die "syslog: too many levels given: $_\n" if defined($numpri);
  83.         $numpri = $num;
  84.         return 0 unless &LOG_MASK($numpri) & $maskpri;
  85.     }
  86.     else {
  87.         die "syslog: too many facilities given: $_\n" if defined($numfac);
  88.         $facility = $_;
  89.         $numfac = $num;
  90.     }
  91.     }
  92.  
  93.     die "syslog: level must be given\n" unless defined($numpri);
  94.  
  95.     if (!defined($numfac)) {    # Facility not specified in this call.
  96.     $facility = 'user' unless $facility;
  97.     $numfac = &xlate($facility);
  98.     }
  99.  
  100.     &connect unless $connected;
  101.  
  102.     $whoami = $ident;
  103.  
  104.     if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
  105.     $whoami = $1;
  106.     $mask = $2;
  107.     } 
  108.  
  109.     unless ($whoami) {
  110.     ($whoami = getlogin) ||
  111.         ($whoami = getpwuid($<)) ||
  112.         ($whoami = 'syslog');
  113.     }
  114.  
  115.     $whoami .= "[$$]" if $lo_pid;
  116.  
  117.     $mask =~ s/%m/$!/g;
  118.     $mask .= "\n" unless $mask =~ /\n$/;
  119.     $message = sprintf ($mask, @_);
  120.  
  121.     $sum = $numpri + $numfac;
  122.     unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
  123.     if ($lo_cons) {
  124.         if ($pid = fork) {
  125.         unless ($lo_nowait) {
  126.             do {$died = wait;} until $died == $pid || $died < 0;
  127.         }
  128.         }
  129.         else {
  130.         open(CONS,">/dev/console");
  131.         print CONS "<$facility.$priority>$whoami: $message\r";
  132.         exit if defined $pid;        # if fork failed, we're parent
  133.         close CONS;
  134.         }
  135.     }
  136.     }
  137. }
  138.  
  139. sub xlate {
  140.     local($name) = @_;
  141.     $name = uc $name;
  142.     $name = "LOG_$name" unless $name =~ /^LOG_/;
  143.     $name = "syslog'$name";
  144.     defined &$name ? &$name : -1;
  145. }
  146.  
  147. sub connect {
  148.     $pat = 'S n C4 x8';
  149.  
  150.     $af_unix = &AF_UNIX;
  151.     $af_inet = &AF_INET;
  152.  
  153.     $stream = &SOCK_STREAM;
  154.     $datagram = &SOCK_DGRAM;
  155.  
  156.     ($name,$aliases,$proto) = getprotobyname('udp');
  157.     $udp = $proto;
  158.  
  159.     ($name,$aliases,$port,$proto) = getservbyname('syslog','udp');
  160.     $syslog = $port;
  161.  
  162.     if (chop($myname = `hostname`)) {
  163.     ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
  164.     die "Can't lookup $myname\n" unless $name;
  165.     @bytes = unpack("C4",$addrs[0]);
  166.     }
  167.     else {
  168.     @bytes = (0,0,0,0);
  169.     }
  170.     $this = pack($pat, $af_inet, 0, @bytes);
  171.  
  172.     if ($host =~ /^\d+\./) {
  173.     @bytes = split(/\./,$host);
  174.     }
  175.     else {
  176.     ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
  177.     die "Can't lookup $host\n" unless $name;
  178.     @bytes = unpack("C4",$addrs[0]);
  179.     }
  180.     $that = pack($pat,$af_inet,$syslog,@bytes);
  181.  
  182.     socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
  183.     bind(SYSLOG,$this) || die "bind: $!\n";
  184.     connect(SYSLOG,$that) || die "connect: $!\n";
  185.  
  186.     local($old) = select(SYSLOG); $| = 1; select($old);
  187.     $connected = 1;
  188. }
  189.  
  190. sub disconnect {
  191.     close SYSLOG;
  192.     $connected = 0;
  193. }
  194.  
  195. 1;
  196.