home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_mlb.zip / syslog.pl < prev    next >
Perl Script  |  1997-11-25  |  5KB  |  198 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. $host = 'localhost' unless $host;    # set $syslog'host to change
  32.  
  33. if ($] >= 5) {
  34.     warn "You should 'use Sys::Syslog' instead; continuing" # if $^W
  35.  
  36. require 'syslog.ph';
  37.  
  38.  eval 'use Socket; 1'             ||
  39.      eval { require "socket.ph" }     ||
  40.      require "sys/socket.ph";
  41.  
  42. $maskpri = &LOG_UPTO(&LOG_DEBUG);
  43.  
  44. sub main'openlog {
  45.     ($ident, $logopt, $facility) = @_;  # package vars
  46.     $lo_pid = $logopt =~ /\bpid\b/;
  47.     $lo_ndelay = $logopt =~ /\bndelay\b/;
  48.     $lo_cons = $logopt =~ /\bcons\b/;
  49.     $lo_nowait = $logopt =~ /\bnowait\b/;
  50.     &connect if $lo_ndelay;
  51.  
  52. sub main'closelog {
  53.     $facility = $ident = '';
  54.     &disconnect;
  55.  
  56. sub main'setlogmask {
  57.     local($oldmask) = $maskpri;
  58.     $maskpri = shift;
  59.     $oldmask;
  60. }
  61.  
  62. sub main'syslog {
  63.     local($priority) = shift;
  64.     local($mask) = shift;
  65.     local($message, $whoami);
  66.     local(@words, $num, $numpri, $numfac, $sum);
  67.     local($facility) = $facility;    # may need to change temporarily.
  68.  
  69.     die "syslog: expected both priority and mask" unless $mask && $priority;
  70.  
  71.     @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
  72.     undef $numpri;
  73.     undef $numfac;
  74.     foreach (@words) {
  75.     $num = &xlate($_);        # Translate word to number.
  76.     if (/^kern$/ || $num < 0) {
  77.         die "syslog: invalid level/facility: $_\n";
  78.     }
  79.     elsif ($num <= &LOG_PRIMASK) {
  80.         die "syslog: too many levels given: $_\n" if defined($numpri);
  81.         $numpri = $num;
  82.         return 0 unless &LOG_MASK($numpri) & $maskpri;
  83.     }
  84.     else {
  85.         die "syslog: too many facilities given: $_\n" if defined($numfac);
  86.         $facility = $_;
  87.         $numfac = $num;
  88.     }
  89.     }
  90.  
  91.     die "syslog: level must be given\n" unless defined($numpri);
  92.  
  93.     if (!defined($numfac)) {    # Facility not specified in this call.
  94.     $facility = 'user' unless $facility;
  95.     $numfac = &xlate($facility);
  96.     }
  97.  
  98.     &connect unless $connected;
  99.  
  100.     $whoami = $ident;
  101.  
  102.     if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
  103.     $whoami = $1;
  104.     $mask = $2;
  105.     } 
  106.  
  107.     unless ($whoami) {
  108.     ($whoami = getlogin) ||
  109.         ($whoami = getpwuid($<)) ||
  110.         ($whoami = 'syslog');
  111.     }
  112.  
  113.     $whoami .= "[$$]" if $lo_pid;
  114.  
  115.     $mask =~ s/%m/$!/g;
  116.     $mask .= "\n" unless $mask =~ /\n$/;
  117.     $message = sprintf ($mask, @_);
  118.  
  119.     $sum = $numpri + $numfac;
  120.     unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
  121.     if ($lo_cons) {
  122.         if ($pid = fork) {
  123.         unless ($lo_nowait) {
  124.             do {$died = wait;} until $died == $pid || $died < 0;
  125.         }
  126.         }
  127.         else {
  128.         open(CONS,">/dev/console");
  129.         print CONS "<$facility.$priority>$whoami: $message\r";
  130.         exit if defined $pid;        # if fork failed, we're parent
  131.         close CONS;
  132.         }
  133.     }
  134.     }
  135. }
  136.  
  137. sub xlate {
  138.     local($name) = @_;
  139.     $name = uc $name;
  140.     $name = "LOG_$name" unless $name =~ /^LOG_/;
  141.     $name = "syslog'$name";
  142.     defined &$name ? &$name : -1;
  143. }
  144.  
  145. sub connect {
  146.     $pat = 'S n C4 x8';
  147.  
  148.     $af_unix = &AF_UNIX;
  149.     $af_inet = &AF_INET;
  150.  
  151.     $stream = &SOCK_STREAM;
  152.     $datagram = &SOCK_DGRAM;
  153.  
  154.     ($name,$aliases,$proto) = getprotobyname('udp');
  155.     $udp = $proto;
  156.  
  157.     ($name,$aliases,$port,$proto) = getservbyname('syslog','udp');
  158.     $syslog = $port;
  159.  
  160.     if (chop($myname = `hostname`)) {
  161.     ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
  162.     die "Can't lookup $myname\n" unless $name;
  163.     @bytes = unpack("C4",$addrs[0]);
  164.     }
  165.     else {
  166.     @bytes = (0,0,0,0);
  167.     }
  168.     $this = pack($pat, $af_inet, 0, @bytes);
  169.  
  170.     if ($host =~ /^\d+\./) {
  171.     @bytes = split(/\./,$host);
  172.     }
  173.     else {
  174.     ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
  175.     die "Can't lookup $host\n" unless $name;
  176.     @bytes = unpack("C4",$addrs[0]);
  177.     }
  178.     $that = pack($pat,$af_inet,$syslog,@bytes);
  179.  
  180.     socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
  181.     bind(SYSLOG,$this) || die "bind: $!\n";
  182.     connect(SYSLOG,$that) || die "connect: $!\n";
  183.  
  184.     local($old) = select(SYSLOG); $| = 1; select($old);
  185.     $connected = 1;
  186. }
  187.  
  188. sub disconnect {
  189.     close SYSLOG;
  190.     $connected = 0;
  191. }
  192.  
  193. 1;
  194.