File Coverage

blib/lib/Anego/Logger.pm
Criterion Covered Total %
statement 18 29 62.0
branch 0 4 0.0
condition 0 3 0.0
subroutine 6 10 60.0
pod 0 3 0.0
total 24 49 48.9


line stmt bran cond sub pod time code
1             package Anego::Logger;
2 1     1   4 use strict;
  1         1  
  1         26  
3 1     1   4 use warnings;
  1         2  
  1         28  
4 1     1   4 use utf8;
  1         1  
  1         4  
5 1     1   502 use parent qw/ Exporter /;
  1         267  
  1         5  
6              
7 1     1   656 use Term::ANSIColor qw/ colored /;
  1         6781  
  1         456  
8              
9             our @EXPORT = qw/ infof warnf errorf /;
10              
11             use constant {
12 1         269 INFO => 1,
13             WARN => 2,
14             ERROR => 3,
15 1     1   7 };
  1         2  
16              
17             our $COLORS = {
18             INFO, => 'cyan',
19             WARN, => 'yellow',
20             ERROR, => 'red',
21             };
22              
23             sub _print {
24 0     0     my ($type, $string, @args) = @_;
25              
26 0 0         my $message = sprintf($string, (map { defined($_) ? $_ : '-' } @args));
  0            
27 0           my $colored_message = colored $message, $COLORS->{$type};
28              
29 0 0 0       my $fh = $type && $type <= INFO ? *STDOUT : *STDERR;
30 0           print {$fh} $colored_message;
  0            
31             }
32              
33 0     0 0   sub infof { _print(INFO, @_) }
34              
35 0     0 0   sub warnf { _print(WARN, @_) }
36              
37             sub errorf {
38 0     0 0   _print(ERROR, @_);
39 0           exit 1;
40             }
41              
42             1;