File Coverage

blib/lib/Log/Dispatch/WarnDie.pm
Criterion Covered Total %
statement 10 15 66.6
branch 1 10 10.0
condition n/a
subroutine 3 4 75.0
pod 0 1 0.0
total 14 30 46.6


line stmt bran cond sub pod time code
1             package Log::Dispatch::WarnDie;
2              
3             # Make sure we have version info for this module
4             # Be strict from now on
5              
6             $VERSION = '0.04';
7 1     1   592 use strict;
  1         1  
  1         161  
8              
9             # The logging dispatcher that should be used
10              
11             my $DISPATCHER;
12              
13             # Old settings of standard Perl logging mechanisms
14              
15             my $WARN;
16             my $DIE;
17              
18             # At compile time
19             # Save current __WARN__ setting
20             # Replace it with a sub that
21             # Dispatches a warning message if there is a dispatcher
22             # Executes the standard system warn() or whatever was there before
23              
24             BEGIN {
25 1     1   4 $WARN = $SIG{__WARN__};
26             $SIG{__WARN__} = sub {
27 0 0       0 $DISPATCHER->warning( $_[0] ) if $DISPATCHER;
28 0 0       0 $WARN ? $WARN->( $_[0] ) : CORE::warn( $_[0] );
29 1         6 };
30              
31             # Save current __DIE__ setting
32             # Replace it with a sub that
33             # Dispatches an error message if there is a dispatcher
34             # Executes the standard system die() or whatever was there before
35              
36 1         3 $DIE = $SIG{__DIE__};
37             $SIG{__DIE__} = sub {
38 0 0       0 $DISPATCHER->error( $_[0] ) if $DISPATCHER;
39 0 0       0 $DIE ? $DIE->( $_[0] ) : CORE::die( $_[0] );
40 1         3 };
41              
42             # Make sure we won't be listed ourselves by Carp::
43              
44             $Carp::Internal{$_} = 1
45 1         83 foreach 'Log::Dispatch','Log::Dispatch::Output',__PACKAGE__;
46             } #BEGIN
47              
48             # Satisfy require
49              
50             1;
51              
52             #---------------------------------------------------------------------------
53              
54             # Class methods
55              
56             #---------------------------------------------------------------------------
57             # dispatcher
58             #
59             # Set and/or return the current dispatcher
60             #
61             # IN: 1 class (ignored)
62             # 2 new dispatcher (optional)
63             # OUT: 1 current dispatcher
64              
65             sub dispatcher {
66              
67             # Set the new dispatcher if there is any
68             # Return the current dispatcher
69              
70 1 50   1 0 11 $DISPATCHER = $_[1] if @_ > 1;
71 1         7 $DISPATCHER;
72             } #dispatcher
73              
74             #---------------------------------------------------------------------------
75              
76             # Perl standard features
77              
78             #---------------------------------------------------------------------------
79             # import
80             #
81             # Called whenever a -use- is done.
82             #
83             # IN: 1 class (ignored)
84             # 2 new dispatcher (optional)
85              
86             *import = \&dispatcher;
87              
88             #---------------------------------------------------------------------------
89             # unimport
90             #
91             # Called whenever a -use- is done.
92             #
93             # IN: 1 class (ignored)
94              
95 0     0     sub unimport { import( undef ) } #unimport
96              
97             #---------------------------------------------------------------------------
98              
99             __END__