/usr/share/perl5/Log/Any/Manager.pm is in liblog-any-adapter-perl 0.11-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 | package Log::Any::Manager;
{
$Log::Any::Manager::VERSION = '0.11';
}
use strict;
use warnings;
use Carp qw(croak);
use Devel::GlobalDestruction;
use Log::Any::Adapter::Util qw(require_dynamic);
use Guard;
sub new {
my $class = shift;
my $self = { entries => [] };
bless $self, $class;
# Create the initial Null entry (this is always present)
#
$self->set('Null');
my $null_entry = $self->{entries}->[0];
# Start our category cache with any null adapters already returned from raw Log::Any
#
$self->{category_cache} = {
map {
(
$_ => {
adapter => $Log::Any::NullAdapters{$_},
entry => $null_entry
}
)
} keys(%Log::Any::NullAdapters)
};
return $self;
}
sub get_logger {
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};
}
sub _choose_entry_for_category {
my ( $self, $category ) = @_;
foreach my $entry ( @{ $self->{entries} } ) {
if ( $category =~ $entry->{pattern} ) {
return $entry;
}
}
die "no entries matched '$category' - should not get here!";
}
sub _new_adapter_for_entry {
my ( $self, $entry, $category ) = @_;
return $entry->{adapter_class}
->new( @{ $entry->{adapter_params} }, category => $category );
}
sub set {
my $self = shift;
my $options;
if ( ref( $_[0] ) eq 'HASH' ) {
$options = shift(@_);
}
my ( $adapter_name, @adapter_params ) = @_;
croak "expected adapter name"
unless defined($adapter_name) && $adapter_name =~ /\S/;
my $pattern = $options->{category};
if ( !defined($pattern) ) {
$pattern = qr/.*/;
}
elsif ( !ref($pattern) ) {
$pattern = qr/^\Q$pattern\E$/;
}
$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"
);
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 =
Guard::guard { $self->remove($entry) if !in_global_destruction };
}
return $entry;
}
sub remove {
my ( $self, $entry ) = @_;
my $pattern = $entry->{pattern};
my $size = scalar( @{ $self->{entries} } );
die "cannot remove bottom entry"
if $entry eq $self->{entries}->[ $size - 1 ];
$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;
}
}
}
1;
|