File Coverage

blib/lib/Ukigumo/Client/Logger.pm
Criterion Covered Total %
statement 35 37 94.5
branch 1 2 50.0
condition n/a
subroutine 10 10 100.0
pod 0 2 0.0
total 46 51 90.2


line stmt bran cond sub pod time code
1             package Ukigumo::Client::Logger;
2 4     4   26 use strict;
  4         8  
  4         193  
3 4     4   21 use warnings;
  4         7  
  4         98  
4 4     4   15 use utf8;
  4         7  
  4         22  
5 4     4   2841 use Ukigumo::Logger;
  4         84492  
  4         126  
6              
7 4     4   43 use Mouse;
  4         7  
  4         18  
8              
9             has logfh => (
10             is => 'ro',
11             required => 1,
12             );
13              
14             has branch => (
15             is => 'ro',
16             required => 1,
17             );
18              
19             has quiet => (
20             is => 'ro',
21             required => 1,
22             );
23              
24             has _logger => (
25             is => 'ro',
26             isa => 'Ukigumo::Logger',
27             default => sub {
28             my $self = shift;
29             Ukigumo::Logger->new(prefix => ['[' . $self->branch . ']']);
30             },
31             );
32              
33             has _format => (
34             is => 'ro',
35             default => sub {
36             return sub {
37             my ($time, $type, $message) = @_;
38             warn "$time [$type] $message\n";
39             };
40             },
41             );
42              
43 4     4   1744 no Mouse;
  4         8  
  4         19  
44              
45             sub infof {
46 9     9 0 23 my $self = shift;
47 9         37 $self->_output_log_by('infof', @_);
48             }
49              
50             sub warnf {
51 7     7 0 62 my $self = shift;
52 7         116 $self->_output_log_by('warnf', @_);
53             }
54              
55             sub _output_log_by {
56 16     16   45 my $self = shift;
57 16         132 my $method = shift;
58              
59 16         145 my $STDERR = *STDERR;
60              
61 16     3   623 open my $fh, '>', \my $log;
  3         34  
  3         8  
  3         31  
62 16         4949 local *STDERR = $fh;
63              
64 16         136 local $Log::Minimal::PRINT = $self->_format;
65 16         222 $self->_logger->$method(@_);
66              
67 16         270 my $logfh = $self->logfh;
68 16         105 print $logfh $log;
69              
70 16 50       219 unless ($self->quiet) {
71 0         0 local *STDERR = $STDERR;
72 0         0 warn $log;
73             }
74             }
75              
76             1;
77