| 1 | package Carp::Syslog; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use v5.10; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Sys::Syslog; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | our $VERSION = '0.01'; | |||||||||
| 8 | |||||||||||
| 9 | 4 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=6 SCAR=17.9
| |||||||||||
| 10 | 4 | my ( $class, $args ) = @_; | |||||||||
| 11 | |||||||||||
| 12 | # Defaults. | ||||||||||
| 13 | 4 | my $ident = $0; | |||||||||
| 14 | 4 | my $logopt = ''; | |||||||||
| 15 | 4 | my $facility = 'user'; | |||||||||
| 16 | |||||||||||
| 17 | 4 | ▶ | if ( defined $args ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 2 | ▶ | if ( ref $args eq 'HASH' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 1 | ▶ | $ident = $args->{'ident'} if exists $args->{'ident'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 1 | ▶ | $logopt = $args->{'logopt'} if exists $args->{'logopt'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 1 | ▶ | $facility = $args->{'facility'} if exists $args->{'facility'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | } | ||||||||||
| 23 | else { | ||||||||||
| 24 | 1 | $facility = $args; | |||||||||
| 25 | } | ||||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 4 | openlog( $ident, $logopt, $facility ); | |||||||||
| 29 | |||||||||||
| 30 | 3 | ▶ | $SIG{'__WARN__'} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 31 | 3 | ▶ | if ( ( caller 0 )[10]->{'Carp::Syslog'} ) { # hint on? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 2 | ( my $message = $_[0] ) =~ s/\n$//; | |||||||||
| 33 | 2 | syslog( 'warning', $message ); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 3 | warn $_[0]; | |||||||||
| 37 | 4 | }; | |||||||||
| 38 | |||||||||||
| 39 | 3 | ▶ | $SIG{'__DIE__'} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 40 | 3 | ▶ | if ( ( caller 0 )[10]->{'Carp::Syslog'} ) { # hint on? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 3 | my $message; | |||||||||
| 42 | |||||||||||
| 43 | 3 | ▶ | if ( ref $_[0] ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | # We only want to to log references if they can stringify. | ||||||||||
| 45 | 2 | require overload; | |||||||||
| 46 | 2 | ▶ | if ( "$_[0]" ne overload::StrVal( $_[0] ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 1 | $message = "$_[0]"; | |||||||||
| 48 | } | ||||||||||
| 49 | } | ||||||||||
| 50 | else { | ||||||||||
| 51 | 1 | $message = $_[0]; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 3 | ▶ | if ( defined $message ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 2 | $message =~ s/\n$//; | |||||||||
| 56 | 2 | syslog( 'err', $message ); | |||||||||
| 57 | } | ||||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 3 | die $_[0]; | |||||||||
| 61 | 4 | }; | |||||||||
| 62 | |||||||||||
| 63 | # Also export Carp's defaults to calling namespace. | ||||||||||
| 64 | 4 | require Carp; | |||||||||
| 65 | { | ||||||||||
| 66 | 2 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 67 | 4 | my $caller = caller 0; | |||||||||
| 68 | |||||||||||
| 69 | 4 | *{ $caller . '::carp' } = \&Carp::carp; | |||||||||
| 70 | 4 | *{ $caller . '::croak' } = \&Carp::croak; | |||||||||
| 71 | 4 | *{ $caller . '::confess' } = \&Carp::confess; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 4 | $^H{'Carp::Syslog'} = 1; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 1 | ▶ | sub unimport { | ||||||||
|
Subroutine
unimport: called CC=1 SCAR=0.0
| |||||||||||
| 78 | 1 | $^H{'Carp::Syslog'} = 0; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 2 | ▶ | END { | ||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||
| 82 | 2 | closelog(); | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 2 | 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<Log::Log4perl> and L<Log::Dispatch>, but those | ||||||||||
| 128 | are overly complicated for quick, system administrator style scripts. The | ||||||||||
| 129 | C<Carp::Syslog> 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<mailto:cgrau@cpan.org> | ||||||||||
| 141 | |||||||||||
| 142 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 143 | |||||||||||
| 144 | Copyright (c) 2011-2012, Chris Grau. | ||||||||||
| 145 | |||||||||||
| 146 | =head1 SEE ALSO | ||||||||||
| 147 | |||||||||||
| 148 | L<Sys::Syslog> | ||||||||||
| 149 | |||||||||||
| 150 | =cut | ||||||||||