home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PERL4036.ZIP / lib / syslog.pl < prev    next >
Perl Script  |  1993-02-08  |  6KB  |  222 lines

  1. #
  2. # syslog.pl
  3. #
  4. # $Log:    syslog.pl,v $
  5. # Revision 4.0.1.1  92/06/08  13:48:05  lwall
  6. # patch20: new warning for ambiguous use of unary operators
  7. # Revision 4.0  91/03/20  01:26:24  lwall
  8. # 4.0 baseline.
  9. # Revision 3.0.1.4  90/11/10  01:41:11  lwall
  10. # patch38: syslog.pl was referencing an absolute path
  11. # Revision 3.0.1.3  90/10/15  17:42:18  lwall
  12. # patch29: various portability fixes
  13. # Revision 3.0.1.1  90/08/09  03:57:17  lwall
  14. # patch19: Initial revision
  15. # Revision 1.2  90/06/11  18:45:30  18:45:30  root ()
  16. # - Changed 'warn' to 'mail|warning' in test call (to give example of
  17. #   facility specification, and because 'warn' didn't work on HP-UX).
  18. # - Fixed typo in &openlog ("ncons" should be "cons").
  19. # - Added (package-global) $maskpri, and &setlogmask.
  20. # - In &syslog:
  21. #   - put argument test ahead of &connect (why waste cycles?),
  22. #   - allowed facility to be specified in &syslog's first arg (temporarily
  23. #     overrides any $facility set in &openlog), just as in syslog(3C),
  24. #   - do a return 0 when bit for $numpri not set in log mask (see syslog(3C)),
  25. #   - changed $whoami code to use getlogin, getpwuid($<) and 'syslog'
  26. #     (in that order) when $ident is null,
  27. #   - made PID logging consistent with syslog(3C) and subject to $lo_pid only,
  28. #   - fixed typo in "print CONS" statement ($<facility should be <$facility).
  29. #   - changed \n to \r in print CONS (\r is useful, $message already has a \n).
  30. # - Changed &xlate to return -1 for an unknown name, instead of croaking.
  31. #
  32. # tom christiansen <tchrist@convex.com>
  33. # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
  34. # NOTE: openlog now takes three arguments, just like openlog(3)
  35. #
  36. # call syslog() with a string priority and a list of printf() args
  37. # like syslog(3)
  38. #
  39. #  usage: require 'syslog.pl';
  40. #
  41. #  then (put these all in a script to test function)
  42. #        
  43. #
  44. #    do openlog($program,'cons,pid','user');
  45. #    do syslog('info','this is another test');
  46. #    do syslog('mail|warning','this is a better test: %d', time);
  47. #    do closelog();
  48. #    
  49. #    do syslog('debug','this is the last test');
  50. #    do openlog("$program $$",'ndelay','user');
  51. #    do syslog('notice','fooprogram: this is really done');
  52. #
  53. #    $! = 55;
  54. #    do syslog('info','problem was %m'); # %m == $! in syslog(3)
  55.  
  56. package syslog;
  57.  
  58. $host = 'localhost' unless $host;    # set $syslog'host to change
  59.  
  60. require 'syslog.ph';
  61.  
  62. $maskpri = &LOG_UPTO(&LOG_DEBUG);
  63.  
  64. sub main'openlog {
  65.     ($ident, $logopt, $facility) = @_;  # package vars
  66.     $lo_pid = $logopt =~ /\bpid\b/;
  67.     $lo_ndelay = $logopt =~ /\bndelay\b/;
  68.     $lo_cons = $logopt =~ /\bcons\b/;
  69.     $lo_nowait = $logopt =~ /\bnowait\b/;
  70.     &connect if $lo_ndelay;
  71.  
  72. sub main'closelog {
  73.     $facility = $ident = '';
  74.     &disconnect;
  75.  
  76. sub main'setlogmask {
  77.     local($oldmask) = $maskpri;
  78.     $maskpri = shift;
  79.     $oldmask;
  80. }
  81.  
  82. sub main'syslog {
  83.     local($priority) = shift;
  84.     local($mask) = shift;
  85.     local($message, $whoami);
  86.     local(@words, $num, $numpri, $numfac, $sum);
  87.     local($facility) = $facility;    # may need to change temporarily.
  88.  
  89.     die "syslog: expected both priority and mask" unless $mask && $priority;
  90.  
  91.     @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
  92.     undef $numpri;
  93.     undef $numfac;
  94.     foreach (@words) {
  95.     $num = &xlate($_);        # Translate word to number.
  96.     if (/^kern$/ || $num < 0) {
  97.         die "syslog: invalid level/facility: $_\n";
  98.     }
  99.     elsif ($num <= &LOG_PRIMASK) {
  100.         die "syslog: too many levels given: $_\n" if defined($numpri);
  101.         $numpri = $num;
  102.         return 0 unless &LOG_MASK($numpri) & $maskpri;
  103.     }
  104.     else {
  105.         die "syslog: too many facilities given: $_\n" if defined($numfac);
  106.         $facility = $_;
  107.         $numfac = $num;
  108.     }
  109.     }
  110.  
  111.     die "syslog: level must be given\n" unless defined($numpri);
  112.  
  113.     if (!defined($numfac)) {    # Facility not specified in this call.
  114.     $facility = 'user' unless $facility;
  115.     $numfac = &xlate($facility);
  116.     }
  117.  
  118.     &connect unless $connected;
  119.  
  120.     $whoami = $ident;
  121.  
  122.     if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
  123.     $whoami = $1;
  124.     $mask = $2;
  125.     } 
  126.  
  127.     unless ($whoami) {
  128.     ($whoami = getlogin) ||
  129.         ($whoami = getpwuid($<)) ||
  130.         ($whoami = 'syslog');
  131.     }
  132.  
  133.     $whoami .= "[$$]" if $lo_pid;
  134.  
  135.     $mask =~ s/%m/$!/g;
  136.     $mask .= "\n" unless $mask =~ /\n$/;
  137.     $message = sprintf ($mask, @_);
  138.  
  139.     $sum = $numpri + $numfac;
  140.     unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
  141.     if ($lo_cons) {
  142.         if ($pid = fork) {
  143.         unless ($lo_nowait) {
  144.             do {$died = wait;} until $died == $pid || $died < 0;
  145.         }
  146.         }
  147.         else {
  148.         open(CONS,">/dev/console");
  149.         print CONS "<$facility.$priority>$whoami: $message\r";
  150.         exit if defined $pid;        # if fork failed, we're parent
  151.         close CONS;
  152.         }
  153.     }
  154.     }
  155. }
  156.  
  157. sub xlate {
  158.     local($name) = @_;
  159.     $name =~ y/a-z/A-Z/;
  160.     $name = "LOG_$name" unless $name =~ /^LOG_/;
  161.     $name = "syslog'$name";
  162.     eval(&$name) || -1;
  163. }
  164.  
  165. sub connect {
  166.     $pat = 'S n C4 x8';
  167.  
  168.     $af_unix = 1;
  169.     $af_inet = 2;
  170.  
  171.     $stream = 1;
  172.     $datagram = 2;
  173.  
  174.     ($name,$aliases,$proto) = getprotobyname('udp');
  175.     $udp = $proto;
  176.  
  177.     ($name,$aliase,$port,$proto) = getservbyname('syslog','udp');
  178.     $syslog = $port;
  179.  
  180.     if (chop($myname = `hostname`)) {
  181.     ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
  182.     die "Can't lookup $myname\n" unless $name;
  183.     @bytes = unpack("C4",$addrs[0]);
  184.     }
  185.     else {
  186.     @bytes = (0,0,0,0);
  187.     }
  188.     $this = pack($pat, $af_inet, 0, @bytes);
  189.  
  190.     if ($host =~ /^\d+\./) {
  191.     @bytes = split(/\./,$host);
  192.     }
  193.     else {
  194.     ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
  195.     die "Can't lookup $host\n" unless $name;
  196.     @bytes = unpack("C4",$addrs[0]);
  197.     }
  198.     $that = pack($pat,$af_inet,$syslog,@bytes);
  199.  
  200.     socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
  201.     bind(SYSLOG,$this) || die "bind: $!\n";
  202.     connect(SYSLOG,$that) || die "connect: $!\n";
  203.  
  204.     local($old) = select(SYSLOG); $| = 1; select($old);
  205.     $connected = 1;
  206. }
  207.  
  208. sub disconnect {
  209.     close SYSLOG;
  210.     $connected = 0;
  211. }
  212.  
  213. 1;
  214.