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.01'; |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
19591
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
26
|
|
7
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
4
|
use Log::Any::Adapter::Util qw( log_level_aliases logging_methods ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
51
|
|
10
|
1
|
|
|
1
|
|
395
|
use Class::Method::Modifiers qw( install_modifier ); |
|
1
|
|
|
|
|
1136
|
|
|
1
|
|
|
|
|
334
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub install { |
13
|
1
|
|
|
1
|
0
|
64
|
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
|
|
10
|
my $timestamp = $args{timestamp} // sub { time }; |
|
3
|
|
|
|
|
11
|
|
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
|
|
5
|
my $aliases = { log_level_aliases() }; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Create history attribute if it doesn't exist |
22
|
1
|
50
|
|
|
|
21
|
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
|
|
|
|
|
8
|
return $history; |
27
|
1
|
|
|
|
|
5
|
}); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Create max_history_size attribute if it doesn't exist |
31
|
1
|
50
|
|
|
|
143
|
unless ($adapter_class->can('max_history_size')) { |
32
|
|
|
|
|
|
|
install_modifier( $adapter_class, 'fresh', max_history_size => sub { |
33
|
3
|
|
|
3
|
|
13
|
my ($self, $arg) = @_; |
34
|
|
|
|
|
|
|
|
35
|
3
|
50
|
|
|
|
10
|
return $history_size unless $arg; |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
0
|
$history_size = $arg; |
38
|
0
|
|
|
|
|
0
|
return $self; |
39
|
1
|
|
|
|
|
5
|
}); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# Push to history from logging methods |
43
|
1
|
|
|
|
|
114
|
for my $method ( logging_methods() ) { |
44
|
|
|
|
|
|
|
install_modifier( $adapter_class, 'around', $method => sub { |
45
|
4
|
|
|
4
|
|
431
|
my $orig = shift; |
46
|
4
|
|
|
|
|
7
|
my $self = shift; |
47
|
|
|
|
|
|
|
|
48
|
4
|
|
33
|
|
|
20
|
my $level = $aliases->{$method} // $method; |
49
|
4
|
|
|
|
|
9
|
my $check = "is_$level"; |
50
|
|
|
|
|
|
|
|
51
|
4
|
100
|
|
|
|
17
|
return unless $self->$check; |
52
|
|
|
|
|
|
|
|
53
|
3
|
|
|
|
|
100
|
my $history = $self->history; |
54
|
3
|
|
|
|
|
45
|
my $max = $self->max_history_size; |
55
|
|
|
|
|
|
|
|
56
|
3
|
|
|
|
|
8
|
my $msg = $self->$orig( @_ ); |
57
|
|
|
|
|
|
|
|
58
|
3
|
|
|
|
|
431
|
push @{$history}, [ $timestamp->(), $level, $msg ]; |
|
3
|
|
|
|
|
9
|
|
59
|
3
|
|
|
|
|
6
|
shift @{$history} while scalar @{$history} > $max; |
|
4
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
3
|
|
60
|
|
|
|
|
|
|
|
61
|
3
|
|
|
|
|
9
|
return $msg; |
62
|
9
|
|
|
|
|
1799
|
}); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Make aliases call their counterparts |
66
|
1
|
|
|
|
|
205
|
for my $alias ( keys %{$aliases} ) { |
|
1
|
|
|
|
|
4
|
|
67
|
|
|
|
|
|
|
install_modifier( $adapter_class, 'around', $alias => sub { |
68
|
1
|
|
|
1
|
|
42
|
my $orig = shift; |
69
|
1
|
|
|
|
|
2
|
my $self = shift; |
70
|
|
|
|
|
|
|
|
71
|
1
|
|
|
|
|
3
|
my $method = $aliases->{$alias}; |
72
|
1
|
|
|
|
|
16
|
return $self->$method(@_); |
73
|
5
|
|
|
|
|
828
|
}); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__END__ |