| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Games::Lacuna::Task::Role::Logger; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1372
|
use 5.010; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
62
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = $Games::Lacuna::Task::VERSION; |
|
5
|
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
567
|
use Moose::Role; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Games::Lacuna::Task::Utils qw(pretty_dump format_date); |
|
9
|
|
|
|
|
|
|
use IO::Interactive qw(is_interactive); |
|
10
|
|
|
|
|
|
|
use Term::ANSIColor qw(color); |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @LEVELS = qw(debug info notice warn error); |
|
13
|
|
|
|
|
|
|
our @COLORS = qw(white cyan magenta yellow red); |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has 'loglevel' => ( |
|
16
|
|
|
|
|
|
|
is => 'rw', |
|
17
|
|
|
|
|
|
|
isa => Moose::Util::TypeConstraints::enum(\@LEVELS), |
|
18
|
|
|
|
|
|
|
default => 'info', |
|
19
|
|
|
|
|
|
|
documentation => 'Print all messages equal or above the given level [Default: info, Available: '.join(',',@LEVELS).']', |
|
20
|
|
|
|
|
|
|
); |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has 'debug' => ( |
|
23
|
|
|
|
|
|
|
is => 'rw', |
|
24
|
|
|
|
|
|
|
isa => 'Bool', |
|
25
|
|
|
|
|
|
|
default => 0, |
|
26
|
|
|
|
|
|
|
documentation => 'Log all messages to debug.log', |
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub abort { |
|
30
|
|
|
|
|
|
|
my ( $self, @msgs ) = @_; |
|
31
|
|
|
|
|
|
|
my ($level_name,$logmessage) = $self->_format_message('error',@msgs); |
|
32
|
|
|
|
|
|
|
die $logmessage; |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _format_message { |
|
36
|
|
|
|
|
|
|
my ( $self, @msgs ) = @_; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $level_name = shift(@msgs) |
|
39
|
|
|
|
|
|
|
if $msgs[0] ~~ \@LEVELS; |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
@msgs = map { pretty_dump($_) } @msgs; |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $format = shift(@msgs) // ''; |
|
44
|
|
|
|
|
|
|
my $logmessage = sprintf( $format, map { $_ // 'UNDEF' } @msgs ); |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
return ($level_name,$logmessage); |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub log { |
|
50
|
|
|
|
|
|
|
my ( $self, @msgs ) = @_; |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my ($level_name,$logmessage) = $self->_format_message(@msgs); |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
if (is_interactive()) { |
|
55
|
|
|
|
|
|
|
my ($level_pos) = grep { $LEVELS[$_] eq $level_name } 0 .. $#LEVELS; |
|
56
|
|
|
|
|
|
|
my ($level_max) = grep { $LEVELS[$_] eq $self->loglevel } 0 .. $#LEVELS; |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
binmode STDOUT, ":utf8"; |
|
59
|
|
|
|
|
|
|
if ($level_pos >= $level_max) { |
|
60
|
|
|
|
|
|
|
print color 'bold '.($COLORS[$level_pos] || 'white'); |
|
61
|
|
|
|
|
|
|
printf "%6s: ",$level_name; |
|
62
|
|
|
|
|
|
|
print color 'reset'; |
|
63
|
|
|
|
|
|
|
say $logmessage; |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
if ($self->debug && $self->can('configdir')) { |
|
68
|
|
|
|
|
|
|
state $fh; |
|
69
|
|
|
|
|
|
|
$fh ||= Path::Class::File->new($self->configdir,'debug.log')->open('a',':encoding(UTF-8)'); |
|
70
|
|
|
|
|
|
|
say $fh sprintf("%s\t%s\t%s",format_date(time),$level_name,$logmessage); |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
return ($level_name,$logmessage); |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
no Moose::Role; |
|
77
|
|
|
|
|
|
|
1; |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=encoding utf8 |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 NAME |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Games::Lacuna::Role::Logger -Â Prints log messages |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 ACCESSORS |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head2 loglevel |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Specify the loglevel. Will print all log messages equal or above the given |
|
90
|
|
|
|
|
|
|
level if running in an interactive shell. |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 METHODS |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 log |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Print a log message. You can use the sprintf syntax. |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$self->log($loglevel,$message,@sprintf_params); |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 abort |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Dies with a pretty error message |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
$self->abort($message,@sprintf_params); |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=cut |