home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-osu / syslog.shar < prev    next >
Encoding:
Internet Message Format  |  1990-03-13  |  5.1 KB

  1. Path: tut.cis.ohio-state.edu!ucbvax!decwrl!elroy.jpl.nasa.gov!jpl-devvax!lwall
  2. From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: openlog(3), syslog(3) in perl
  5. Message-ID: <7423@jpl-devvax.JPL.NASA.GOV>
  6. Date: 15 Mar 90 02:03:51 GMT
  7. References: <39461@apple.Apple.COM>
  8. Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall)
  9. Organization: Jet Propulsion Laboratory, Pasadena, CA
  10. Lines: 179
  11.  
  12. In article <39461@apple.Apple.COM> fair@apple.com (Erik E. Fair) writes:
  13. : Has anybody duplicated these routines for perl 3.0 yet, in such a
  14. : manner that you don't have to spawn a process for every line you log
  15. : (i.e. that hack I saw already isn't acceptable)?
  16. :     trolling for wheels that are already 'round,
  17. :     Erik E. Fair    apple!fair    fair@apple.com
  18. : P.S.    I'm not a perl hacker (yet).
  19.  
  20. Little does he know the insidious nature of the malady...  :-)
  21.  
  22. Here's a warmed-over version of Tom's syslog.pl.  It talks to the inet
  23. socket of syslogd.  (There is, in fact, a hidden option to log to a different
  24. machine.)  It also purports to support the logopt flags of openlog, though
  25. some of them haven't been tested.  I won't claim the wheel's round yet,
  26. but it's getting closer.
  27.  
  28. Larry
  29.  
  30. #!/bin/sh
  31. : make a subdirectory, cd to it, and run this through sh.
  32. echo 'If this kit is complete, "End of kit" will echo at the end'
  33. echo Extracting syslog.pl
  34. sed >syslog.pl <<'!STUFFY!FUNK!' -e 's/X//'
  35. X#
  36. X# syslog.pl
  37. X#
  38. X# tom christiansen <tchrist@convex.com>
  39. X# modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
  40. X# NOTE: openlog now takes three arguments, just like openlog(3)
  41. X#
  42. X# call syslog() with a string priority and a list of printf() args
  43. X# like syslog(3)
  44. X#
  45. X#  usage: do 'syslog.pl' || die "syslog.pl: $@";
  46. X#
  47. X#  then (put these all in a script to test function)
  48. X#        
  49. X#
  50. X#    do openlog($program,'cons,pid','user');
  51. X#    do syslog('info','this is another test');
  52. X#    do syslog('warn','this is a better test: %d', time);
  53. X#    do closelog();
  54. X#    
  55. X#    do syslog('debug','this is the last test');
  56. X#    do openlog("$program $$",'ndelay','user');
  57. X#    do syslog('notice','fooprogram: this is really done');
  58. X#
  59. X#    $! = 55;
  60. X#    do syslog('info','problem was %m'); # %m == $! in syslog(3)
  61. X
  62. Xpackage syslog;
  63. X
  64. X$host = 'localhost' unless $host;    # set $syslog'host to change
  65. X
  66. Xdo '/usr/local/lib/perl/syslog.h'
  67. X    || die "syslog: Can't do syslog.h: ",($@||$!),"\n";
  68. X
  69. Xsub main'openlog {
  70. X    ($ident, $logopt, $facility) = @_;  # package vars
  71. X    $lo_pid = $logopt =~ /\bpid\b/;
  72. X    $lo_ndelay = $logopt =~ /\bndelay\b/;
  73. X    $lo_cons = $logopt =~ /\bncons\b/;
  74. X    $lo_nowait = $logopt =~ /\bnowait\b/;
  75. X    &connect if $lo_ndelay;
  76. X} 
  77. X
  78. Xsub main'closelog {
  79. X    $facility = $ident = '';
  80. X    &disconnect;
  81. X} 
  82. Xsub main'syslog {
  83. X    local($priority) = shift;
  84. X    local($mask) = shift;
  85. X    local($message, $whoami);
  86. X
  87. X    &connect unless $connected;
  88. X
  89. X    $whoami = $ident;
  90. X
  91. X    die "syslog: expected both priority and mask" unless $mask && $priority;
  92. X
  93. X    $facility = "user" unless $facility;
  94. X
  95. X    if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
  96. X    $whoami = $1;
  97. X    $mask = $2;
  98. X    } 
  99. X    $whoami .= " [$$]" if $lo_pid;
  100. X
  101. X    $mask =~ s/%m/$!/g;
  102. X    $mask .= "\n" unless $mask =~ /\n$/;
  103. X    $message = sprintf ($mask, @_);
  104. X
  105. X    $whoami = sprintf ("%s %d",$ENV{'USER'}||$ENV{'LOGNAME'},$$) unless $whoami;
  106. X
  107. X    $sum = &xlate($priority) + &xlate($facility);
  108. X    unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
  109. X    if ($lo_cons) {
  110. X        if ($pid = fork) {
  111. X        unless ($lo_nowait) {
  112. X            do {$died = wait;} until $died == $pid || $died < 0;
  113. X        }
  114. X        }
  115. X        else {
  116. X        open(CONS,">/dev/console");
  117. X        print CONS "$<facility.$priority>$whoami: $message\n";
  118. X        exit if defined $pid;        # if fork failed, we're parent
  119. X        close CONS;
  120. X        }
  121. X    }
  122. X    }
  123. X}
  124. X
  125. Xsub xlate {
  126. X    local($name) = @_;
  127. X    $name =~ y/a-z/A-Z/;
  128. X    $name = "LOG_$name" unless $name =~ /^LOG_/;
  129. X    $name = "syslog'$name";
  130. X    &$name;
  131. X}
  132. X
  133. Xsub connect {
  134. X    $pat = 'S n C4 x8';
  135. X
  136. X    $af_unix = 1;
  137. X    $af_inet = 2;
  138. X
  139. X    $stream = 1;
  140. X    $datagram = 2;
  141. X
  142. X    ($name,$aliases,$proto) = getprotobyname('udp');
  143. X    $udp = $proto;
  144. X
  145. X    ($name,$aliase,$port,$proto) = getservbyname('syslog','udp');
  146. X    $syslog = $port;
  147. X
  148. X    if (chop($myname = `hostname`)) {
  149. X    ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
  150. X    die "Can't lookup $myname\n" unless $name;
  151. X    @bytes = unpack("C4",$addrs[0]);
  152. X    }
  153. X    else {
  154. X    @bytes = (0,0,0,0);
  155. X    }
  156. X    $this = pack($pat, $af_inet, 0, @bytes);
  157. X
  158. X    if ($host =~ /^\d+\./) {
  159. X    @bytes = split(/\./,$host);
  160. X    }
  161. X    else {
  162. X    ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
  163. X    die "Can't lookup $host\n" unless $name;
  164. X    @bytes = unpack("C4",$addrs[0]);
  165. X    }
  166. X    $that = pack($pat,$af_inet,$syslog,@bytes);
  167. X
  168. X    socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
  169. X    bind(SYSLOG,$this) || die "bind: $!\n";
  170. X    connect(SYSLOG,$that) || die "connect: $!\n";
  171. X
  172. X    local($old) = select(SYSLOG); $| = 1; select($old);
  173. X    $connected = 1;
  174. X}
  175. X
  176. Xsub disconnect {
  177. X    close SYSLOG;
  178. X    $connected = 0;
  179. X}
  180. X
  181. X1;
  182. !STUFFY!FUNK!
  183. echo ""
  184. echo "End of kit"
  185. : I do not append .signature, but someone might mail this.
  186. exit
  187.  
  188.