| 1 | package Log::Any::Plugin::ContextStack; | ||||||||||
| 2 | # ABSTRACT: Stack of context items that get prepended to each log message | ||||||||||
| 3 | 2 | $Log::Any::Plugin::ContextStack::VERSION = '0.012'; | |||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Carp qw( confess ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Log::Any; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Scope::Guard qw( guard ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | use Log::Any::Plugin::Util qw( get_old_method set_new_method ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use Log::Any::Adapter::Util qw( logging_methods ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 2 | my @context_stack; | |||||||||
| 14 | 2 | my $stringify_context = \&_default_stringify_context; | |||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | sub install { | ||||||||
|
Subroutine
install: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||
| 17 | 1 | my ($class, $adapter_class, %args) = @_; | |||||||||
| 18 | |||||||||||
| 19 | 1 | ▶ | if ($args{stringify}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 0 | $stringify_context = delete $args{stringify}; | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | 1 | ▶ | if (%args) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 0 | my $keys = join(', ', sort keys %args); | |||||||||
| 25 | 0 | confess("Unexpected arguments $keys"); | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | # Create the $log->push method | ||||||||||
| 29 | 2 | ▶ | set_new_method($adapter_class, push_context => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 2 | my $self = shift; | |||||||||
| 31 | 2 | push(@context_stack, @_); | |||||||||
| 32 | 2 | return $self; | |||||||||
| 33 | 1 | }); | |||||||||
| 34 | |||||||||||
| 35 | # Create the $log->pop method | ||||||||||
| 36 | 2 | ▶ | set_new_method($adapter_class, pop_context => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 2 | my $self = shift; | |||||||||
| 38 | 2 | return pop(@context_stack); | |||||||||
| 39 | 1 | }); | |||||||||
| 40 | |||||||||||
| 41 | # Create the $log->context method | ||||||||||
| 42 | 1 | ▶ | set_new_method($adapter_class, push_scoped_context => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 43 | 1 | my $self = shift; | |||||||||
| 44 | 1 | ▶ | return unless @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 1 | my $count = scalar @_; | |||||||||
| 46 | 1 | push(@context_stack, @_); | |||||||||
| 47 | 1 | ▶ | return guard { splice(@context_stack, -$count) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 1 | }); | |||||||||
| 49 | |||||||||||
| 50 | # Augment the main $log->debug methods (not the aliases) | ||||||||||
| 51 | 1 | for my $method_name ( logging_methods() ) { | |||||||||
| 52 | 9 | my $old_method = get_old_method($adapter_class, $method_name); | |||||||||
| 53 | 6 | ▶ | set_new_method($adapter_class, $method_name, sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 54 | 6 | my $self = shift; | |||||||||
| 55 | 6 | ▶ | if (@context_stack) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 4 | unshift(@_, $stringify_context->(\@context_stack)); | |||||||||
| 57 | } | ||||||||||
| 58 | 6 | $old_method->($self, @_); | |||||||||
| 59 | 9 | }); | |||||||||
| 60 | } | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 4 | ▶ | sub _default_stringify_context { | ||||||||
|
Subroutine
_default_stringify_context: called CC=2 SCAR=7.1
| |||||||||||
| 64 | 4 | my ($context) = @_; | |||||||||
| 65 | 4 | ▶ | return '' unless @$context; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 4 | return '[' . join(':', @$context) . ']'; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 2 | 1; | |||||||||
| 70 | |||||||||||
| 71 | __END__ | ||||||||||