| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Carp::Syslog; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
52565
|
use v5.10; |
|
|
2
|
|
|
|
|
7
|
|
|
|
2
|
|
|
|
|
114
|
|
|
4
|
2
|
|
|
2
|
|
12
|
use strict; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
62
|
|
|
5
|
2
|
|
|
2
|
|
12
|
use Sys::Syslog; |
|
|
2
|
|
|
|
|
17
|
|
|
|
2
|
|
|
|
|
1066
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub import { |
|
10
|
4
|
|
|
4
|
|
5128
|
my ( $class, $args ) = @_; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Defaults. |
|
13
|
4
|
|
|
|
|
10
|
my $ident = $0; |
|
14
|
4
|
|
|
|
|
8
|
my $logopt = ''; |
|
15
|
4
|
|
|
|
|
5
|
my $facility = 'user'; |
|
16
|
|
|
|
|
|
|
|
|
17
|
4
|
100
|
|
|
|
14
|
if ( defined $args ) { |
|
18
|
2
|
100
|
|
|
|
8
|
if ( ref $args eq 'HASH' ) { |
|
19
|
1
|
50
|
|
|
|
5
|
$ident = $args->{'ident'} if exists $args->{'ident'}; |
|
20
|
1
|
50
|
|
|
|
5
|
$logopt = $args->{'logopt'} if exists $args->{'logopt'}; |
|
21
|
1
|
50
|
|
|
|
6
|
$facility = $args->{'facility'} if exists $args->{'facility'}; |
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
else { |
|
24
|
1
|
|
|
|
|
2
|
$facility = $args; |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
|
|
28
|
4
|
|
|
|
|
13
|
openlog( $ident, $logopt, $facility ); |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
$SIG{'__WARN__'} = sub { |
|
31
|
3
|
100
|
|
3
|
|
43
|
if ( ( caller 0 )[10]->{'Carp::Syslog'} ) { # hint on? |
|
32
|
2
|
|
|
|
|
13
|
( my $message = $_[0] ) =~ s/\n$//; |
|
33
|
2
|
|
|
|
|
10
|
syslog( 'warning', $message ); |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
|
|
36
|
3
|
|
|
|
|
298
|
warn $_[0]; |
|
37
|
4
|
|
|
|
|
35
|
}; |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$SIG{'__DIE__'} = sub { |
|
40
|
7
|
50
|
|
7
|
|
3196
|
if ( ( caller 0 )[10]->{'Carp::Syslog'} ) { # hint on? |
|
41
|
3
|
|
|
|
|
4
|
my $message; |
|
42
|
|
|
|
|
|
|
|
|
43
|
3
|
100
|
|
|
|
9
|
if ( ref $_[0] ) { |
|
44
|
|
|
|
|
|
|
# We only want to to log references if they can stringify. |
|
45
|
2
|
|
|
|
|
12
|
require overload; |
|
46
|
2
|
100
|
|
|
|
46
|
if ( "$_[0]" ne overload::StrVal( $_[0] ) ) { |
|
47
|
1
|
|
|
|
|
10
|
$message = "$_[0]"; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
else { |
|
51
|
1
|
|
|
|
|
2
|
$message = $_[0]; |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
3
|
100
|
|
|
|
15
|
if ( defined $message ) { |
|
55
|
2
|
|
|
|
|
5
|
$message =~ s/\n$//; |
|
56
|
2
|
|
|
|
|
6
|
syslog( 'err', $message ); |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
3
|
|
|
|
|
24
|
die $_[0]; |
|
61
|
4
|
|
|
|
|
29
|
}; |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Also export Carp's defaults to calling namespace. |
|
64
|
4
|
|
|
|
|
36
|
require Carp; |
|
65
|
|
|
|
|
|
|
{ |
|
66
|
2
|
|
|
2
|
|
21
|
no strict 'refs'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
376
|
|
|
|
4
|
|
|
|
|
39
|
|
|
67
|
4
|
|
|
|
|
8
|
my $caller = caller 0; |
|
68
|
|
|
|
|
|
|
|
|
69
|
4
|
|
|
|
|
15
|
*{ $caller . '::carp' } = \&Carp::carp; |
|
|
4
|
|
|
|
|
17
|
|
|
70
|
4
|
|
|
|
|
23
|
*{ $caller . '::croak' } = \&Carp::croak; |
|
|
4
|
|
|
|
|
16
|
|
|
71
|
4
|
|
|
|
|
8
|
*{ $caller . '::confess' } = \&Carp::confess; |
|
|
4
|
|
|
|
|
19
|
|
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
4
|
|
|
|
|
242
|
$^H{'Carp::Syslog'} = 1; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub unimport { |
|
78
|
1
|
|
|
1
|
|
97
|
$^H{'Carp::Syslog'} = 0; |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
END { |
|
82
|
2
|
|
|
2
|
|
2876
|
closelog(); |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 NAME |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Carp::Syslog - Send warn and die messages to syslog |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Defaults shown. |
|
94
|
|
|
|
|
|
|
use Carp::Syslog { ident => $0, logopt => '', facility => 'user' }; |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
warn '...'; # logs to user:warning |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
die '...'; # logs to user:err |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# Shortcut for simplicity. |
|
101
|
|
|
|
|
|
|
use Carp::Syslog 'user'; |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
{ |
|
104
|
|
|
|
|
|
|
no Carp::Syslog; |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
warn '...'; # doesn't log to syslog |
|
107
|
|
|
|
|
|
|
die '...'; # ditto |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Also useful on the command line. |
|
111
|
|
|
|
|
|
|
perl -MCarp::Syslog=user script.pl |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
I got tired of writing this all the time: |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
use Sys::Syslog; |
|
118
|
|
|
|
|
|
|
use File::Basename qw( basename ); |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
BEGIN { |
|
121
|
|
|
|
|
|
|
openlog( basename($0), 'pid', 'local1' ); |
|
122
|
|
|
|
|
|
|
$SIG{'__WARN__'} = sub { syslog( 'warning', @_ ); warn @_ }; |
|
123
|
|
|
|
|
|
|
$SIG{'__DIE__'} = sub { syslog( 'err', @_ ); die @_ }; |
|
124
|
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
END { closelog() } |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Sure, there are modules like L and L, but those |
|
128
|
|
|
|
|
|
|
are overly complicated for quick, system administrator style scripts. The |
|
129
|
|
|
|
|
|
|
C module allows, in one line (or less if used on the command |
|
130
|
|
|
|
|
|
|
line), to send all warn() and die() calls to the system's syslog. |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 CAVEATS |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
The C<__WARN__> and C<__DIE__> signal handlers are overridden. |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Calling cluck() or confess() will really fill up your logs. |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 AUTHOR |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Chris Grau L |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Copyright (c) 2011-2012, Chris Grau. |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
L |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |