| 1 | package Log::Any::Adapter::Duperr; | ||||||||||
| 2 | |||||||||||
| 3 | # | ||||||||||
| 4 | # Cunning adapter for logging to a duplicate of STDERR | ||||||||||
| 5 | # | ||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use 5.008001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use utf8::all; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use Log::Any::Adapter::Util (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 2 | ▶ | use base qw/Log::Any::Adapter::Base/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 2 | our $VERSION = '0.05'; | |||||||||
| 18 | |||||||||||
| 19 | 1 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=4 SCAR=15.9
Pod
undocumented
| |||||||||||
| 20 | 1 | my ($self) = @_; | |||||||||
| 21 | |||||||||||
| 22 | # Duplicate STDERR | ||||||||||
| 23 | 1 | ▶ | open( $self->{fh}, '>&', STDERR ) or croak "Can't dup STDERR: $!"; ## no critic [InputOutput::RequireBriefOpen] | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | |||||||||||
| 25 | 1 | $self->{fh}->autoflush(1); | |||||||||
| 26 | |||||||||||
| 27 | 1 | ▶ | if ( exists $self->{log_level} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | $self->{log_level} = Log::Any::Adapter::Util::numeric_level( $self->{log_level} ) | ||||||||||
| 29 | 0 | ▶ | unless $self->{log_level} =~ /^\d+$/x; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | } | ||||||||||
| 31 | else { | ||||||||||
| 32 | 1 | $self->{log_level} = Log::Any::Adapter::Util::numeric_level('trace'); | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 1 | return; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 2 | foreach my $method ( Log::Any::Adapter::Util::logging_methods() ) { | |||||||||
| 39 | 2 | ▶ | no strict 'refs'; ## no critic (ProhibitNoStrict) | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | |||||||||||
| 41 | 18 | my $method_level = Log::Any::Adapter::Util::numeric_level($method); | |||||||||
| 42 | |||||||||||
| 43 | 18 | ▶ | *{$method} = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=14.6
| |||||||||||
| 44 | 0 | my ( $self, $text ) = @_; | |||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | return if $method_level > $self->{log_level}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | |||||||||||
| 48 | 0 | $self->{fh}->print("$text\n"); | |||||||||
| 49 | 18 | }; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 2 | foreach my $method ( Log::Any::Adapter::Util::detection_methods() ) { | |||||||||
| 53 | 2 | ▶ | no strict 'refs'; ## no critic (ProhibitNoStrict) | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 54 | |||||||||||
| 55 | 18 | my $base = substr( $method, 3 ); | |||||||||
| 56 | |||||||||||
| 57 | 18 | my $method_level = Log::Any::Adapter::Util::numeric_level($base); | |||||||||
| 58 | |||||||||||
| 59 | 18 | ▶ | *{$method} = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 60 | 0 | return !!( $method_level <= $_[0]->{log_level} ); | |||||||||
| 61 | 18 | }; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 2 | 1; | |||||||||
| 65 | |||||||||||
| 66 | __END__ | ||||||||||