line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bot::Cobalt::Logger::Output; |
2
|
|
|
|
|
|
|
$Bot::Cobalt::Logger::Output::VERSION = '0.021003'; |
3
|
7
|
|
|
7
|
|
13978
|
use Carp; |
|
7
|
|
|
|
|
10
|
|
|
7
|
|
|
|
|
386
|
|
4
|
7
|
|
|
7
|
|
395
|
use strictures 2; |
|
7
|
|
|
|
|
1145
|
|
|
7
|
|
|
|
|
228
|
|
5
|
|
|
|
|
|
|
|
6
|
7
|
|
|
7
|
|
1271
|
use Bot::Cobalt::Common qw/:types :string/; |
|
7
|
|
|
|
|
10
|
|
|
7
|
|
|
|
|
37
|
|
7
|
7
|
|
|
7
|
|
893
|
use POSIX (); |
|
7
|
|
|
|
|
7781
|
|
|
7
|
|
|
|
|
117
|
|
8
|
7
|
|
|
7
|
|
23
|
use Try::Tiny; |
|
7
|
|
|
|
|
8
|
|
|
7
|
|
|
|
|
271
|
|
9
|
|
|
|
|
|
|
|
10
|
7
|
|
|
7
|
|
864
|
use Moo; |
|
7
|
|
|
|
|
11628
|
|
|
7
|
|
|
|
|
41
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has time_format => ( |
13
|
|
|
|
|
|
|
is => 'rw', |
14
|
|
|
|
|
|
|
isa => Str, |
15
|
3
|
|
|
3
|
|
86
|
builder => sub { "%Y-%m-%d %H:%M:%S" }, # strftime |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has log_format => ( |
19
|
|
|
|
|
|
|
is => 'rw', |
20
|
|
|
|
|
|
|
isa => Str, |
21
|
3
|
|
|
3
|
|
100
|
builder => sub { "%level %time (%pkg%) %msg" }, # rplprintf |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has _outputs => ( |
26
|
|
|
|
|
|
|
is => 'rwp', |
27
|
|
|
|
|
|
|
isa => HashRef, |
28
|
|
|
|
|
|
|
default => sub { +{} }, |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub add { |
33
|
4
|
|
|
4
|
1
|
2943
|
my ($self, @args) = @_; |
34
|
|
|
|
|
|
|
|
35
|
4
|
100
|
100
|
|
|
27
|
unless (@args && @args % 2 == 0) { |
36
|
2
|
|
|
|
|
260
|
confess "add() expects an even number of arguments, ", |
37
|
|
|
|
|
|
|
"mapping an Output class to a HASH of constructor arguments" |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
2
|
|
|
|
|
4
|
my $prefix = 'Bot::Cobalt::Logger::Output::' ; |
41
|
|
|
|
|
|
|
|
42
|
2
|
|
|
|
|
39
|
CONFIG: while (my ($alias, $opts) = splice @args, 0, 2) { |
43
|
3
|
50
|
|
|
|
13
|
confess "Can't add $alias, opts are not a HASH" |
44
|
|
|
|
|
|
|
unless ref $opts eq 'HASH'; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
confess "Can't add $alias, no type specified" |
47
|
3
|
50
|
|
|
|
7
|
unless $opts->{type}; |
48
|
|
|
|
|
|
|
|
49
|
3
|
|
|
|
|
8
|
my $target_pkg = $prefix . delete $opts->{type}; |
50
|
|
|
|
|
|
|
|
51
|
3
|
|
|
|
|
4
|
{ local $@; |
|
3
|
|
|
|
|
3
|
|
52
|
3
|
|
|
|
|
189
|
eval "require $target_pkg"; |
53
|
|
|
|
|
|
|
|
54
|
3
|
50
|
|
|
|
20
|
if (my $err = $@) { |
55
|
0
|
|
|
|
|
0
|
carp "Could not add logger $alias: $err"; |
56
|
|
|
|
|
|
|
next CONFIG |
57
|
0
|
|
|
|
|
0
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $new_obj = try { |
61
|
3
|
|
|
3
|
|
126
|
$target_pkg->new(%$opts) |
62
|
|
|
|
|
|
|
} catch { |
63
|
0
|
|
|
0
|
|
0
|
carp "Could not add logger $alias; new() died: $_"; |
64
|
|
|
|
|
|
|
undef |
65
|
3
|
50
|
|
|
|
23
|
} or next CONFIG; |
|
0
|
|
|
|
|
0
|
|
66
|
|
|
|
|
|
|
|
67
|
3
|
|
|
|
|
75
|
$self->_outputs->{$alias} = $new_obj; |
68
|
|
|
|
|
|
|
} ## CONFIG |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1 |
71
|
2
|
|
|
|
|
7
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub del { |
74
|
1
|
|
|
1
|
1
|
3
|
my ($self, @aliases) = @_; |
75
|
1
|
|
|
|
|
1
|
my $x; |
76
|
|
|
|
|
|
|
|
77
|
1
|
|
|
|
|
2
|
for my $alias (@aliases) { |
78
|
2
|
50
|
|
|
|
27
|
++$x if delete $self->_outputs->{$alias} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
$x |
82
|
1
|
|
|
|
|
4
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub get { |
85
|
1
|
|
|
1
|
1
|
319
|
my ($self, $alias) = @_; |
86
|
1
|
|
|
|
|
6
|
$self->_outputs->{$alias} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
## Private. |
91
|
|
|
|
|
|
|
sub _format { |
92
|
6
|
|
|
6
|
|
12
|
my ($self, $level, $caller, @strings) = @_; |
93
|
|
|
|
|
|
|
|
94
|
6
|
|
|
|
|
83
|
rplprintf( $self->log_format, { |
95
|
|
|
|
|
|
|
level => $level, |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
## Actual message. |
98
|
|
|
|
|
|
|
msg => join(' ', @strings), |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
time => POSIX::strftime( $self->time_format, localtime ), |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
pkg => $caller->[0], |
103
|
|
|
|
|
|
|
file => $caller->[1], |
104
|
|
|
|
|
|
|
line => $caller->[2], |
105
|
|
|
|
|
|
|
sub => $caller->[3], |
106
|
|
|
|
|
|
|
}) . "\n" |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub _write { |
110
|
7
|
|
|
7
|
|
712
|
my $self = shift; |
111
|
|
|
|
|
|
|
|
112
|
7
|
|
|
|
|
8
|
for my $alias (keys %{ $self->_outputs }) { |
|
7
|
|
|
|
|
27
|
|
113
|
6
|
|
|
|
|
12
|
my $output = $self->_outputs->{$alias}; |
114
|
6
|
50
|
|
|
|
42
|
$output->_write( |
115
|
|
|
|
|
|
|
## Output classes can provide their own _format |
116
|
|
|
|
|
|
|
$output->can('_format') ? $output->_format( @_ ) |
117
|
|
|
|
|
|
|
: $self->_format( @_ ) |
118
|
|
|
|
|
|
|
) |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1 |
122
|
7
|
|
|
|
|
16
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
1; |
125
|
|
|
|
|
|
|
__END__ |