/usr/share/perl5/Log/Any/Manager.pm is in liblog-any-perl 1.038-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | use 5.008001;
use strict;
use warnings;
package Log::Any::Manager;
our $VERSION = '1.038';
sub new {
my $class = shift;
my $self = {
entries => [],
category_cache => {},
default_adapter => {},
};
bless $self, $class;
return $self;
}
sub get_adapter {
my ( $self, $category ) = @_;
# Create a new adapter for this category if it is not already in cache
#
my $category_cache = $self->{category_cache};
if ( !defined( $category_cache->{$category} ) ) {
my $entry = $self->_choose_entry_for_category($category);
my $adapter = $self->_new_adapter_for_entry( $entry, $category );
$category_cache->{$category} = { entry => $entry, adapter => $adapter };
}
return $category_cache->{$category}->{adapter};
}
{
no warnings 'once';
*get_logger = \&get_adapter; # backwards compatibility
}
sub _choose_entry_for_category {
my ( $self, $category ) = @_;
foreach my $entry ( @{ $self->{entries} } ) {
if ( $category =~ $entry->{pattern} ) {
return $entry;
}
}
# nothing requested so fallback to default
my $default = $self->{default_adapter}{$category}
|| [ $self->_get_adapter_class("Null"), [] ];
my ($adapter_class, $adapter_params) = @$default;
_require_dynamic($adapter_class);
return {
adapter_class => $adapter_class,
adapter_params => $adapter_params,
};
}
sub _new_adapter_for_entry {
my ( $self, $entry, $category ) = @_;
return $entry->{adapter_class}
->new( @{ $entry->{adapter_params} }, category => $category );
}
sub set_default {
my ( $self, $category, $adapter_name, @adapter_params ) = @_;
my $adapter_class = $self->_get_adapter_class($adapter_name);
$self->{default_adapter}{$category} = [$adapter_class, \@adapter_params];
}
sub set {
my $self = shift;
my $options;
if ( ref( $_[0] ) eq 'HASH' ) {
$options = shift(@_);
}
my ( $adapter_name, @adapter_params ) = @_;
unless ( defined($adapter_name) && $adapter_name =~ /\S/ ) {
require Carp;
Carp::croak("expected adapter name");
}
my $pattern = $options->{category};
if ( !defined($pattern) ) {
$pattern = qr/.*/;
}
elsif ( !ref($pattern) ) {
$pattern = qr/^\Q$pattern\E$/;
}
my $adapter_class = $self->_get_adapter_class($adapter_name);
_require_dynamic($adapter_class);
my $entry = $self->_new_entry( $pattern, $adapter_class, \@adapter_params );
unshift( @{ $self->{entries} }, $entry );
$self->_reselect_matching_adapters($pattern);
if ( my $lex_ref = $options->{lexically} ) {
$$lex_ref = Log::Any::Manager::_Guard->new(
sub { $self->remove($entry) unless _in_global_destruction() } );
}
return $entry;
}
sub remove {
my ( $self, $entry ) = @_;
my $pattern = $entry->{pattern};
$self->{entries} = [ grep { $_ ne $entry } @{ $self->{entries} } ];
$self->_reselect_matching_adapters($pattern);
}
sub _new_entry {
my ( $self, $pattern, $adapter_class, $adapter_params ) = @_;
return {
pattern => $pattern,
adapter_class => $adapter_class,
adapter_params => $adapter_params,
};
}
sub _reselect_matching_adapters {
my ( $self, $pattern ) = @_;
return if _in_global_destruction();
# Reselect adapter for each category matching $pattern
#
while ( my ( $category, $category_info ) =
each( %{ $self->{category_cache} } ) )
{
my $new_entry = $self->_choose_entry_for_category($category);
if ( $new_entry ne $category_info->{entry} ) {
my $new_adapter =
$self->_new_adapter_for_entry( $new_entry, $category );
%{ $category_info->{adapter} } = %$new_adapter;
bless( $category_info->{adapter}, ref($new_adapter) );
$category_info->{entry} = $new_entry;
}
}
}
sub _get_adapter_class {
my ( $self, $adapter_name ) = @_;
return $Log::Any::OverrideDefaultAdapterClass if $Log::Any::OverrideDefaultAdapterClass;
$adapter_name =~ s/^Log:://; # Log::Dispatch -> Dispatch, etc.
my $adapter_class = (
substr( $adapter_name, 0, 1 ) eq '+'
? substr( $adapter_name, 1 )
: "Log::Any::Adapter::$adapter_name"
);
return $adapter_class;
}
# This is adapted from the pure perl parts of Devel::GlobalDestruction
if ( defined ${^GLOBAL_PHASE} ) {
eval 'sub _in_global_destruction () { ${^GLOBAL_PHASE} eq q[DESTRUCT] }; 1' ## no critic
or die $@;
}
else {
require B;
my $started = !B::main_start()->isa(q[B::NULL]);
unless ($started) {
eval '0 && $started; CHECK { $started = 1 }; 1' ## no critic
or die $@;
}
eval ## no critic
'0 && $started; sub _in_global_destruction () { $started && B::main_start()->isa(q[B::NULL]) }; 1'
or die $@;
}
# XXX not DRY and not a great way to do this, but oh, well.
sub _require_dynamic {
my ($class) = @_;
return 1 if $class->can('new'); # duck-type that class is loaded
unless ( defined( eval "require $class; 1" ) )
{ ## no critic (ProhibitStringyEval)
die $@;
}
}
package # hide from PAUSE
Log::Any::Manager::_Guard;
sub new { bless $_[1], $_[0] }
sub DESTROY { $_[0]->() }
1;
|