line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
25
|
|
|
25
|
|
421
|
use 5.008001; |
|
25
|
|
|
|
|
79
|
|
2
|
25
|
|
|
25
|
|
133
|
use strict; |
|
25
|
|
|
|
|
47
|
|
|
25
|
|
|
|
|
1935
|
|
3
|
25
|
|
|
25
|
|
143
|
use warnings; |
|
25
|
|
|
|
|
69
|
|
|
25
|
|
|
|
|
6955
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Log::Any::Manager; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '1.716'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
25
|
|
|
25
|
0
|
66
|
my $class = shift; |
11
|
25
|
|
|
|
|
150
|
my $self = { |
12
|
|
|
|
|
|
|
# The stack of adapter entries |
13
|
|
|
|
|
|
|
entries => [], |
14
|
|
|
|
|
|
|
# A cache of keys with category names and values of a hashref |
15
|
|
|
|
|
|
|
# with stack entries (from the entries attribute) and adapters |
16
|
|
|
|
|
|
|
category_cache => {}, |
17
|
|
|
|
|
|
|
# The adapter to use if no other adapter is appropriate |
18
|
|
|
|
|
|
|
default_adapter => {}, |
19
|
|
|
|
|
|
|
# The context hashref that is passed to all proxies |
20
|
|
|
|
|
|
|
context => {}, |
21
|
|
|
|
|
|
|
}; |
22
|
25
|
|
|
|
|
66
|
bless $self, $class; |
23
|
|
|
|
|
|
|
|
24
|
25
|
|
|
|
|
85
|
return $self; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub has_consumer { |
28
|
37
|
|
|
37
|
0
|
176
|
my ( $self ) = @_; |
29
|
37
|
|
100
|
|
|
72
|
return !!( @{ $self->{entries} } || keys %{ $self->{default_adapter} } || $ENV{LOG_ANY_DEFAULT_ADAPTER} ); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub get_adapter { |
33
|
64
|
|
|
64
|
0
|
203
|
my ( $self, $category ) = @_; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Create a new adapter for this category if it is not already in cache |
36
|
|
|
|
|
|
|
# |
37
|
64
|
|
|
|
|
149
|
my $category_cache = $self->{category_cache}; |
38
|
64
|
100
|
|
|
|
192
|
if ( !defined( $category_cache->{$category} ) ) { |
39
|
34
|
|
|
|
|
106
|
my $entry = $self->_choose_entry_for_category($category); |
40
|
34
|
|
|
|
|
126
|
my $adapter = $self->_new_adapter_for_entry( $entry, $category ); |
41
|
34
|
|
|
|
|
163
|
$category_cache->{$category} = { entry => $entry, adapter => $adapter }; |
42
|
|
|
|
|
|
|
} |
43
|
64
|
|
|
|
|
176
|
return $category_cache->{$category}->{adapter}; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
{ |
47
|
25
|
|
|
25
|
|
192
|
no warnings 'once'; |
|
25
|
|
|
|
|
65
|
|
|
25
|
|
|
|
|
30516
|
|
48
|
|
|
|
|
|
|
*get_logger = \&get_adapter; # backwards compatibility |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub get_context { |
52
|
64
|
|
|
64
|
0
|
132
|
my ( $self ) = @_; |
53
|
64
|
|
|
|
|
175
|
return $self->{context}; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub _choose_entry_for_category { |
57
|
129
|
|
|
129
|
|
238
|
my ( $self, $category ) = @_; |
58
|
|
|
|
|
|
|
|
59
|
129
|
|
|
|
|
190
|
foreach my $entry ( @{ $self->{entries} } ) { |
|
129
|
|
|
|
|
270
|
|
60
|
83
|
100
|
|
|
|
427
|
if ( $category =~ $entry->{pattern} ) { |
61
|
74
|
|
|
|
|
196
|
return $entry; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
# nothing requested so fallback to default |
65
|
55
|
|
100
|
|
|
228
|
my $default_adapter_name = $ENV{LOG_ANY_DEFAULT_ADAPTER} || "Null"; |
66
|
55
|
|
100
|
|
|
232
|
my $default = $self->{default_adapter}{$category} |
67
|
|
|
|
|
|
|
|| [ $self->_get_adapter_class($default_adapter_name), [] ]; |
68
|
55
|
|
|
|
|
145
|
my ($adapter_class, $adapter_params) = @$default; |
69
|
55
|
|
|
|
|
144
|
_require_dynamic($adapter_class); |
70
|
|
|
|
|
|
|
return { |
71
|
55
|
|
|
|
|
258
|
adapter_class => $adapter_class, |
72
|
|
|
|
|
|
|
adapter_params => $adapter_params, |
73
|
|
|
|
|
|
|
}; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub _new_adapter_for_entry { |
77
|
125
|
|
|
125
|
|
262
|
my ( $self, $entry, $category ) = @_; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
return $entry->{adapter_class} |
80
|
125
|
|
|
|
|
200
|
->new( @{ $entry->{adapter_params} }, category => $category ); |
|
125
|
|
|
|
|
534
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub set_default { |
84
|
4
|
|
|
4
|
0
|
67
|
my ( $self, $category, $adapter_name, @adapter_params ) = @_; |
85
|
4
|
|
|
|
|
28
|
Log::Any::Proxy::Null->inflate_nulls; |
86
|
4
|
|
|
|
|
13
|
my $adapter_class = $self->_get_adapter_class($adapter_name); |
87
|
4
|
|
|
|
|
36
|
$self->{default_adapter}{$category} = [$adapter_class, \@adapter_params]; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# =head2 set |
91
|
|
|
|
|
|
|
# |
92
|
|
|
|
|
|
|
# $mgr->set( $options ); |
93
|
|
|
|
|
|
|
# |
94
|
|
|
|
|
|
|
# Set the current adapter. Called from |
95
|
|
|
|
|
|
|
# L, the |
96
|
|
|
|
|
|
|
# standard API for setting the current adapter. Adds a new entry to the |
97
|
|
|
|
|
|
|
# C stack and refreshes all the matching adapters. |
98
|
|
|
|
|
|
|
# |
99
|
|
|
|
|
|
|
# See L |
100
|
|
|
|
|
|
|
# for available options. |
101
|
|
|
|
|
|
|
# |
102
|
|
|
|
|
|
|
# Returns the newly-created entry in the stack. |
103
|
|
|
|
|
|
|
sub set { |
104
|
44
|
|
|
44
|
0
|
81
|
my $self = shift; |
105
|
44
|
|
|
|
|
71
|
my $options; |
106
|
44
|
100
|
|
|
|
152
|
if ( ref( $_[0] ) eq 'HASH' ) { |
107
|
18
|
|
|
|
|
30
|
$options = shift(@_); |
108
|
|
|
|
|
|
|
} |
109
|
44
|
|
|
|
|
127
|
my ( $adapter_name, @adapter_params ) = @_; |
110
|
|
|
|
|
|
|
|
111
|
44
|
100
|
66
|
|
|
331
|
unless ( defined($adapter_name) && $adapter_name =~ /\S/ ) { |
112
|
1
|
|
|
|
|
6
|
require Carp; |
113
|
1
|
|
|
|
|
198
|
Carp::croak("expected adapter name"); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
43
|
|
|
|
|
146
|
my $pattern = $options->{category}; |
117
|
43
|
100
|
|
|
|
128
|
if ( !defined($pattern) ) { |
|
|
100
|
|
|
|
|
|
118
|
40
|
|
|
|
|
159
|
$pattern = qr/.*/; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
elsif ( !ref($pattern) ) { |
121
|
1
|
|
|
|
|
14
|
$pattern = qr/^\Q$pattern\E$/; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
43
|
|
|
|
|
130
|
my $adapter_class = $self->_get_adapter_class($adapter_name); |
125
|
43
|
|
|
|
|
138
|
_require_dynamic($adapter_class); |
126
|
|
|
|
|
|
|
|
127
|
41
|
|
|
|
|
153
|
my $entry = $self->_new_entry( $pattern, $adapter_class, \@adapter_params ); |
128
|
41
|
|
|
|
|
75
|
unshift( @{ $self->{entries} }, $entry ); |
|
41
|
|
|
|
|
178
|
|
129
|
|
|
|
|
|
|
|
130
|
41
|
|
|
|
|
128
|
$self->_reselect_matching_adapters($pattern); |
131
|
|
|
|
|
|
|
|
132
|
37
|
100
|
|
|
|
123
|
if ( my $lex_ref = $options->{lexically} ) { |
133
|
|
|
|
|
|
|
$$lex_ref = Log::Any::Manager::_Guard->new( |
134
|
16
|
50
|
|
16
|
|
95
|
sub { $self->remove($entry) unless _in_global_destruction() } ); |
|
16
|
|
|
|
|
415
|
|
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
37
|
|
|
|
|
259
|
Log::Any::Proxy::Null->inflate_nulls; |
138
|
37
|
|
|
|
|
5309
|
return $entry; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub remove { |
142
|
18
|
|
|
18
|
0
|
40
|
my ( $self, $entry ) = @_; |
143
|
|
|
|
|
|
|
|
144
|
18
|
|
|
|
|
35
|
my $pattern = $entry->{pattern}; |
145
|
18
|
|
|
|
|
27
|
$self->{entries} = [ grep { $_ ne $entry } @{ $self->{entries} } ]; |
|
38
|
|
|
|
|
118
|
|
|
18
|
|
|
|
|
80
|
|
146
|
18
|
|
|
|
|
80
|
$self->_reselect_matching_adapters($pattern); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub _new_entry { |
150
|
41
|
|
|
41
|
|
109
|
my ( $self, $pattern, $adapter_class, $adapter_params ) = @_; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
return { |
153
|
41
|
|
|
|
|
160
|
pattern => $pattern, |
154
|
|
|
|
|
|
|
adapter_class => $adapter_class, |
155
|
|
|
|
|
|
|
adapter_params => $adapter_params, |
156
|
|
|
|
|
|
|
}; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# =head2 _reselect_matching_adapters |
160
|
|
|
|
|
|
|
# |
161
|
|
|
|
|
|
|
# $self->_reselect_matching_adapters( $pattern ) |
162
|
|
|
|
|
|
|
# |
163
|
|
|
|
|
|
|
# Given a pattern, reselect which adapter should match. This is called |
164
|
|
|
|
|
|
|
# after entries are added/removed from the C attribute. |
165
|
|
|
|
|
|
|
# |
166
|
|
|
|
|
|
|
# XXX Does not actually use $pattern, so do we need to pass it in? |
167
|
|
|
|
|
|
|
sub _reselect_matching_adapters { |
168
|
59
|
|
|
59
|
|
127
|
my ( $self, $pattern ) = @_; |
169
|
|
|
|
|
|
|
|
170
|
59
|
50
|
|
|
|
1308
|
return if _in_global_destruction(); |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# Reselect adapter for each category matching $pattern |
173
|
|
|
|
|
|
|
# |
174
|
59
|
|
|
|
|
115
|
for my $category ( keys %{ $self->{category_cache} } ) { |
|
59
|
|
|
|
|
217
|
|
175
|
95
|
|
|
|
|
176
|
my $category_info = $self->{category_cache}->{$category}; |
176
|
95
|
|
|
|
|
771
|
my $new_entry = $self->_choose_entry_for_category($category); |
177
|
95
|
100
|
|
|
|
287
|
if ( $new_entry ne $category_info->{entry} ) { |
178
|
91
|
|
|
|
|
181
|
my $new_adapter = |
179
|
|
|
|
|
|
|
$self->_new_adapter_for_entry( $new_entry, $category ); |
180
|
|
|
|
|
|
|
# Replace existing references to the adapter with the new |
181
|
|
|
|
|
|
|
# adapter |
182
|
87
|
|
|
|
|
243
|
%{ $category_info->{adapter} } = %$new_adapter; |
|
87
|
|
|
|
|
410
|
|
183
|
87
|
|
|
|
|
280
|
bless( $category_info->{adapter}, ref($new_adapter) ); |
184
|
87
|
|
|
|
|
821
|
$category_info->{entry} = $new_entry; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub _get_adapter_class { |
190
|
100
|
|
|
100
|
|
219
|
my ( $self, $adapter_name ) = @_; |
191
|
100
|
100
|
|
|
|
2451
|
return $Log::Any::OverrideDefaultAdapterClass if $Log::Any::OverrideDefaultAdapterClass; |
192
|
94
|
|
|
|
|
188
|
$adapter_name =~ s/^Log:://; # Log::Dispatch -> Dispatch, etc. |
193
|
94
|
100
|
|
|
|
392
|
my $adapter_class = ( |
194
|
|
|
|
|
|
|
substr( $adapter_name, 0, 1 ) eq '+' |
195
|
|
|
|
|
|
|
? substr( $adapter_name, 1 ) |
196
|
|
|
|
|
|
|
: "Log::Any::Adapter::$adapter_name" |
197
|
|
|
|
|
|
|
); |
198
|
94
|
|
|
|
|
321
|
return $adapter_class; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
# This is adapted from the pure perl parts of Devel::GlobalDestruction |
202
|
|
|
|
|
|
|
if ( defined ${^GLOBAL_PHASE} ) { |
203
|
75
|
|
|
75
|
|
394
|
eval 'sub _in_global_destruction () { ${^GLOBAL_PHASE} eq q[DESTRUCT] }; 1' ## no critic |
204
|
|
|
|
|
|
|
or die $@; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
else { |
207
|
|
|
|
|
|
|
require B; |
208
|
|
|
|
|
|
|
my $started = !B::main_start()->isa(q[B::NULL]); |
209
|
|
|
|
|
|
|
unless ($started) { |
210
|
|
|
|
|
|
|
eval '0 && $started; CHECK { $started = 1 }; 1' ## no critic |
211
|
|
|
|
|
|
|
or die $@; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
eval ## no critic |
214
|
|
|
|
|
|
|
'0 && $started; sub _in_global_destruction () { $started && B::main_start()->isa(q[B::NULL]) }; 1' |
215
|
|
|
|
|
|
|
or die $@; |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# XXX not DRY and not a great way to do this, but oh, well. |
219
|
|
|
|
|
|
|
sub _require_dynamic { |
220
|
98
|
|
|
98
|
|
208
|
my ($class) = @_; |
221
|
|
|
|
|
|
|
|
222
|
98
|
100
|
|
|
|
773
|
return 1 if $class->can('new'); # duck-type that class is loaded |
223
|
|
|
|
|
|
|
|
224
|
30
|
100
|
|
|
|
2218
|
unless ( defined( eval "require $class; 1" ) ) |
225
|
|
|
|
|
|
|
{ ## no critic (ProhibitStringyEval) |
226
|
2
|
|
|
|
|
17
|
die $@; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
package # hide from PAUSE |
231
|
|
|
|
|
|
|
Log::Any::Manager::_Guard; |
232
|
|
|
|
|
|
|
|
233
|
16
|
|
|
16
|
|
82
|
sub new { bless $_[1], $_[0] } |
234
|
|
|
|
|
|
|
|
235
|
16
|
|
|
16
|
|
8554
|
sub DESTROY { $_[0]->() } |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
1; |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
__END__ |