| 1 | package Log::Any::Adapter::Daemontools; | ||||||||||||||||||||||||||||||||||||||||
| 2 | 29 | ▶ | use 5.008; # need weak reference support | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 3 | 29 | ▶ | our @ISA; BEGIN { require Log::Any::Adapter::Base; @ISA= 'Log::Any::Adapter::Base' }; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 4 | 29 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 5 | 29 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 6 | 29 | ▶ | use Log::Any::Adapter::Util 'numeric_level'; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 7 | 29 | ▶ | use Log::Any 1.03; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 8 | 29 | ▶ | use Log::Any::Adapter::Daemontools::Config; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||
| 10 | # ABSTRACT: Logging adapter suitable for use in a Daemontools-style logging chain | ||||||||||||||||||||||||||||||||||||||||
| 11 | 29 | our $VERSION = '0.102'; # VERSION | |||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||
| 14 | 29 | our $global_config; | |||||||||||||||||||||||||||||||||||||||
| 15 | 32 | ▶ | sub global_config { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
global_config: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 16 | 32 | ▶ | $global_config ||= shift->new_config; | ||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$Log::Any::Adapter::Daemontools::global_config ||= (shift())->new_config
MC/DC0%0/2
$Log::Any::Adapter::Daemontools::global_config ||= (shift())->new_config
$Log::Any::Adapter::Daemontools::global_config
(shift())->new_config
Truth table66%2/3
A ||= B
A= $Log::Any::Adapter::Daemontools::global_configB= (shift())->new_config
| |||||||||||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||
| 19 | 31 | ▶ | sub new_config { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_config: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 20 | 31 | my $class= shift; | |||||||||||||||||||||||||||||||||||||||
| 21 | 31 | ▶ | $class= ref($class) || $class; | ||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table66%2/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||
| 22 | 31 | my $cfg= "${class}::Config"->new(@_); | |||||||||||||||||||||||||||||||||||||||
| 23 | 31 | return $cfg; | |||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||
| 27 | 6 | ▶ | sub category { shift->{category} } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
category: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 28 | 132 | ▶ | sub config { shift->{config} } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
config: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||
| 31 | # Special carp/croak that ignore Log::Any infrastructure | ||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | ▶ | sub carp { Log::Any::Adapter::Daemontools::Config::carp(@_) } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
carp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub croak { Log::Any::Adapter::Daemontools::Config::croak(@_) } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
croak: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||
| 35 | # Log::Any::Adapter constructor, also named 'init' | ||||||||||||||||||||||||||||||||||||||||
| 36 | 33 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||
| 37 | 33 | my $self= shift; | |||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||
| 39 | 33 | ▶ | $self->{config} ||= $self->global_config; | ||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"config"} ||= $self->global_config
MC/DC0%0/2
$self->{"config"} ||= $self->global_config
$self->{"config"}
$self->global_config
Truth table66%2/3
A ||= B
A= $self->{"config"}B= $self->global_config
| |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||
| 41 | # Warn about unsupported/deprecated features from 0.002 | ||||||||||||||||||||||||||||||||||||||||
| 42 | 33 | ▶ | carp "filter is deprecated. Use config->log_level" if exists $self->{filter}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 43 | 33 | ▶ | carp "dumper is unsupported. See Log::Any::Proxy" if exists $self->{dumper}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||
| 46 | # This constructor gets called for each Adapter instance, so we need | ||||||||||||||||||||||||||||||||||||||||
| 47 | # to track whether we applied the -init to the config yet. | ||||||||||||||||||||||||||||||||||||||||
| 48 | 33 | ▶ | if ($self->{'-init'} && !$self->{config}{_adapter_init_applied}) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"-init"} and not $self->{"config"}{"_adapter_init_applied"}
MC/DC100%2/2
$self->{"-init"} and not $self->{"config"}{"_adapter_init_applied"}
$self->{"-init"}
not $self->{"config"}{"_adapter_init_applied"}
Truth table100%3/3
A and not(B)
A= $self->{"-init"}B= not $self->{"config"}{"_adapter_init_applied"}
| |||||||||||||||||||||||||||||||||||||||||
| 49 | 21 | ++$self->{config}{_adapter_init_applied}; | |||||||||||||||||||||||||||||||||||||||
| 50 | 21 | $self->{config}->init( $self->{'-init'} ); | |||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||
| 53 | # Set up our lazy caching system (re-blesses current object) | ||||||||||||||||||||||||||||||||||||||||
| 54 | 33 | $self->_uncache_config; | |||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||
| 58 | 33 | ▶ | sub _squelch_base_class { ref($_[0]) || $_[0] } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_squelch_base_class: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||
| 60 | # Create per-squelch-level subclasses of a given package | ||||||||||||||||||||||||||||||||||||||||
| 61 | # This is an optimization for minimizing overhead when using disabled levels | ||||||||||||||||||||||||||||||||||||||||
| 62 | 29 | ▶ | sub _build_squelch_subclasses { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_squelch_subclasses: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||
| 63 | 29 | my $class= shift; | |||||||||||||||||||||||||||||||||||||||
| 64 | 29 | my %numeric_levels= ( map { $_ => 1 } -1, map { numeric_level($_) } Log::Any->logging_methods() ); | |||||||||||||||||||||||||||||||||||||||
| 65 | 29 | my %subclass; | |||||||||||||||||||||||||||||||||||||||
| 66 | 29 | foreach my $level_num (keys %numeric_levels) { | |||||||||||||||||||||||||||||||||||||||
| 67 | 290 | my $package= $class.'::Squelch'.($level_num+1); | |||||||||||||||||||||||||||||||||||||||
| 68 | 290 | ▶ | $subclass{$package}{_squelch_base_class}= sub { $class }; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 69 | 290 | foreach my $method (Log::Any->logging_methods(), 'fatal') { | |||||||||||||||||||||||||||||||||||||||
| 70 | 2900 | ▶ | if ($level_num < numeric_level($method)) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 71 | 1392 | ▶ | $subclass{$package}{$method}= sub {}; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 72 | 1392 | ▶ | $subclass{$package}{"is_$method"}= sub { 0 }; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||
| 76 | 29 | ▶ | $subclass{"${class}::Lazy"}{_squelch_base_class}= sub { $class }; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 77 | 29 | foreach my $method (Log::Any->logging_and_detection_methods(), 'fatal', 'is_fatal') { | |||||||||||||||||||||||||||||||||||||||
| 78 | # Trampoline code that lazily re-caches an adaptor the first time it is used | ||||||||||||||||||||||||||||||||||||||||
| 79 | 44 | ▶ | $subclass{"${class}::Lazy"}{$method}= sub { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 80 | 44 | $_[0]->_cache_config; | |||||||||||||||||||||||||||||||||||||||
| 81 | 44 | goto $_[0]->can($method) | |||||||||||||||||||||||||||||||||||||||
| 82 | 580 | }; | |||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||
| 85 | # Create subclasses and install methods | ||||||||||||||||||||||||||||||||||||||||
| 86 | 29 | for my $pkg (keys %subclass) { | |||||||||||||||||||||||||||||||||||||||
| 87 | 29 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 88 | 319 | @{$pkg.'::ISA'}= ( $class ); | |||||||||||||||||||||||||||||||||||||||
| 89 | 319 | for my $method (keys %{ $subclass{$pkg} }) { | |||||||||||||||||||||||||||||||||||||||
| 90 | 3683 | *{$pkg.'::'.$method}= $subclass{$pkg}{$method}; | |||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||
| 93 | 29 | 1; | |||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||
| 96 | # The set of adapters which have been "squelch-cached" | ||||||||||||||||||||||||||||||||||||||||
| 97 | # (i.e. blessed into a subclass) | ||||||||||||||||||||||||||||||||||||||||
| 98 | 29 | our %_squelch_cached_adapters; | |||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||
| 100 | 29 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||
| 101 | 29 | foreach my $method ( Log::Any->logging_methods() ) { | |||||||||||||||||||||||||||||||||||||||
| 102 | 261 | ▶ | my $m= sub { my $self= shift; $self->{_writer}->($self, $method, @_); }; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 103 | 29 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 104 | 261 | *{__PACKAGE__ . "::$method"}= $m; | |||||||||||||||||||||||||||||||||||||||
| 105 | 261 | ▶ | *{__PACKAGE__ . "::is_$method"}= sub { 1 }; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||
| 107 | 29 | __PACKAGE__->_build_squelch_subclasses(); | |||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||
| 110 | # Cache the ->config settings into this adapter, which also | ||||||||||||||||||||||||||||||||||||||||
| 111 | # re-blesses it based on the current log level. | ||||||||||||||||||||||||||||||||||||||||
| 112 | 44 | ▶ | sub _cache_config { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cache_config: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||
| 113 | 44 | my $self= shift; | |||||||||||||||||||||||||||||||||||||||
| 114 | 44 | $self->{_writer}= $self->config->compiled_writer; | |||||||||||||||||||||||||||||||||||||||
| 115 | 44 | my $lev= $self->config->log_level_num; | |||||||||||||||||||||||||||||||||||||||
| 116 | # Backward compatibility with version 0.002 | ||||||||||||||||||||||||||||||||||||||||
| 117 | 44 | ▶ | if (exists $self->{filter}) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | $lev= Log::Any::Adapter::Util::NOTICE - _coerce_filter_level($self->{filter}); | |||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||
| 120 | 44 | bless $self, $self->_squelch_base_class.'::Squelch'.($lev+1); | |||||||||||||||||||||||||||||||||||||||
| 121 | 44 | $self->config->_register_cached_adapter($self); | |||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||
| 124 | # Re-bless adapter back to its "Lazy" config cacher class | ||||||||||||||||||||||||||||||||||||||||
| 125 | 49 | ▶ | sub _uncache_config { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_uncache_config: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 126 | 49 | bless $_[0], $_[0]->_squelch_base_class . '::Lazy'; | |||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||
| 129 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||
| 130 | # Backward compatibility with version 0.002. Do not use in new code. | ||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub write_msg { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_msg: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | my ($self, $level, $message)= @_; | |||||||||||||||||||||||||||||||||||||||
| 134 | # Don't bother optimizing and caching | ||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | $self->config->compiled_writer->($self, $level, $message); | |||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | sub _default_dumper { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_default_dumper: called CC=3 SCAR=13.6
| |||||||||||||||||||||||||||||||||||||||||
| 139 | 1 | require Data::Dumper; | |||||||||||||||||||||||||||||||||||||||
| 140 | 1 | my $val= shift; | |||||||||||||||||||||||||||||||||||||||
| 141 | 1 | local $@; | |||||||||||||||||||||||||||||||||||||||
| 142 | 1 | my $dump= eval { Data::Dumper->new([$val])->Indent(0)->Terse(1)->Useqq(1)->Quotekeys(0)->Maxdepth(4)->Sortkeys(1)->Dump }; | |||||||||||||||||||||||||||||||||||||||
| 143 | 1 | ▶ | if (!defined $dump) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | my $x= "$@"; | |||||||||||||||||||||||||||||||||||||||
| 145 | 0 | $x =~ s/\n//; | |||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | substr($x, 50)= '...' if length $x >= 50; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | $dump= "<exception $x>"; | |||||||||||||||||||||||||||||||||||||||
| 148 | }; | ||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | return $dump; | |||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||
| 152 | 3 | ▶ | sub _coerce_filter_level { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_coerce_filter_level: called CC=6 SCAR=19.2
| |||||||||||||||||||||||||||||||||||||||||
| 153 | 3 | my $val= shift; | |||||||||||||||||||||||||||||||||||||||
| 154 | 3 | my %level_map= ( | |||||||||||||||||||||||||||||||||||||||
| 155 | trace => -2, | ||||||||||||||||||||||||||||||||||||||||
| 156 | debug => -1, | ||||||||||||||||||||||||||||||||||||||||
| 157 | info => 0, | ||||||||||||||||||||||||||||||||||||||||
| 158 | notice => 1, | ||||||||||||||||||||||||||||||||||||||||
| 159 | warning => 2, | ||||||||||||||||||||||||||||||||||||||||
| 160 | error => 3, | ||||||||||||||||||||||||||||||||||||||||
| 161 | critical => 4, | ||||||||||||||||||||||||||||||||||||||||
| 162 | fatal => 4, | ||||||||||||||||||||||||||||||||||||||||
| 163 | ); | ||||||||||||||||||||||||||||||||||||||||
| 164 | return (!defined $val || $val eq 'none')? $level_map{trace}-1 | ||||||||||||||||||||||||||||||||||||||||
| 165 | : Scalar::Util::looks_like_number($val)? $val | ||||||||||||||||||||||||||||||||||||||||
| 166 | : exists $level_map{$val}? $level_map{$val} | ||||||||||||||||||||||||||||||||||||||||
| 167 | 3 | ▶ | : ($val =~ /^debug-(\d+)$/)? $level_map{debug} - $1 | ||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition66%2/3
not defined $val or $val eq "none"
MC/DC50%1/2
not defined $val or $val eq "none"
not defined $val
$val eq "none"
Truth table66%2/3
not(A) or B
A= not defined $valB= $val eq "none"
| |||||||||||||||||||||||||||||||||||||||||
| 168 | : croak "unknown log level '$val'"; | ||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||
| 171 | 29 | 1; | |||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||
| 173 | __END__ | ||||||||||||||||||||||||||||||||||||||||