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