| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Log::Any::Plugin::History; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: Add a message history to a Log::Any adapter | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | our $VERSION = '0.02'; | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 19471 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 23 |  | 
| 7 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 25 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 1 |  |  | 1 |  | 5 | use Log::Any::Adapter::Util qw( log_level_aliases logging_methods ); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 46 |  | 
| 10 | 1 |  |  | 1 |  | 428 | use Class::Method::Modifiers qw( install_modifier ); | 
|  | 1 |  |  |  |  | 1139 |  | 
|  | 1 |  |  |  |  | 335 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub install { | 
| 13 | 1 |  |  | 1 | 0 | 56 | my ($class, $adapter_class, %args) = @_; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 1 |  |  |  |  | 3 | my $history = []; | 
| 16 | 1 |  | 50 |  |  | 5 | my $history_size = $args{size} // 10; | 
| 17 | 1 |  | 50 | 3 |  | 8 | my $timestamp = $args{timestamp} // sub { time }; | 
|  | 3 |  |  |  |  | 12 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 1 |  |  |  |  | 4 | my $aliases = { log_level_aliases() }; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | # Create history attribute if it doesn't exist | 
| 22 | 1 | 50 |  |  |  | 19 | unless ($adapter_class->can('history')) { | 
| 23 |  |  |  |  |  |  | install_modifier( $adapter_class, 'fresh', history => sub { | 
| 24 | 4 |  |  | 4 |  | 39 | my ($self, $arg) = @_; | 
| 25 | 4 | 50 | 33 |  |  | 12 | $history = $arg if defined $arg and ref $arg eq 'ARRAY'; | 
| 26 | 4 |  |  |  |  | 7 | return $history; | 
| 27 | 1 |  |  |  |  | 5 | }); | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | # Create max_history_size attribute if it doesn't exist | 
| 31 | 1 | 50 |  |  |  | 148 | unless ($adapter_class->can('max_history_size')) { | 
| 32 |  |  |  |  |  |  | install_modifier( $adapter_class, 'fresh', max_history_size => sub { | 
| 33 | 3 |  |  | 3 |  | 12 | my ($self, $arg) = @_; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 3 | 50 |  |  |  | 11 | return $history_size unless $arg; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 0 |  |  |  |  | 0 | $history_size = $arg; | 
| 38 | 0 |  |  |  |  | 0 | return $self; | 
| 39 | 1 |  |  |  |  | 6 | }); | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | # Push to history from logging methods | 
| 43 | 1 |  |  |  |  | 128 | for my $method ( logging_methods() ) { | 
| 44 |  |  |  |  |  |  | install_modifier( $adapter_class, 'around', $method => sub { | 
| 45 | 4 |  |  | 4 |  | 473 | my $orig = shift; | 
| 46 | 4 |  |  |  |  | 8 | my $self = shift; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 4 |  | 33 |  |  | 22 | my $level   = $aliases->{$method} // $method; | 
| 49 | 4 |  |  |  |  | 11 | my $check   = "is_$level"; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 4 | 100 |  |  |  | 21 | return unless $self->$check; | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 3 |  |  |  |  | 103 | my $history = $self->history; | 
| 54 | 3 |  |  |  |  | 45 | my $max     = $self->max_history_size; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 3 |  |  |  |  | 11 | my $msg = $self->$orig( @_ ); | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 3 |  |  |  |  | 415 | push @{$history}, [ $timestamp->(), $level, $msg ]; | 
|  | 3 |  |  |  |  | 10 |  | 
| 59 | 3 |  |  |  |  | 6 | shift @{$history} while scalar @{$history} > $max; | 
|  | 4 |  |  |  |  | 13 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 3 |  |  |  |  | 8 | return $msg; | 
| 62 | 9 |  |  |  |  | 1897 | }); | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | # Make aliases call their counterparts | 
| 66 | 1 |  |  |  |  | 263 | for my $alias ( keys %{$aliases} ) { | 
|  | 1 |  |  |  |  | 5 |  | 
| 67 |  |  |  |  |  |  | install_modifier( $adapter_class, 'around', $alias => sub { | 
| 68 | 1 |  |  | 1 |  | 41 | my $orig = shift; | 
| 69 | 1 |  |  |  |  | 1 | my $self = shift; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 1 |  |  |  |  | 3 | my $method = $aliases->{$alias}; | 
| 72 | 1 |  |  |  |  | 16 | return $self->$method(@_); | 
| 73 | 5 |  |  |  |  | 821 | }); | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | 1; | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | __END__ |