home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / ntcode / ntperlb / lib / syslog.pl < prev    next >
Encoding:
Perl Script  |  1995-05-19  |  6.2 KB  |  225 lines

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