File Coverage

blib/lib/JMAP/Tester/LogWriter.pm
Criterion Covered Total %
statement 24 35 68.5
branch 0 4 0.0
condition n/a
subroutine 8 12 66.6
pod 0 3 0.0
total 32 54 59.2


line stmt bran cond sub pod time code
1             package JMAP::Tester::LogWriter 0.101;
2              
3 1     1   7 use Moo::Role;
  1         3  
  1         6  
4              
5             requires 'write';
6              
7 1     1   335 use namespace::clean;
  1         2  
  1         7  
8              
9             {
10             package JMAP::Tester::LogWriter::Code 0.101;
11              
12 1     1   341 use Moo;
  1         20  
  1         7  
13 1     1   363 use namespace::clean;
  1         17  
  1         8  
14             with 'JMAP::Tester::LogWriter';
15             has code => (is => 'ro', required => 1);
16 0     0 0   sub write { $_[0]->code->($_[1]) }
17             }
18              
19             {
20             package JMAP::Tester::LogWriter::Handle 0.101;
21              
22 1     1   612 use Moo;
  1         2  
  1         4  
23 1     1   341 use namespace::clean;
  1         3  
  1         5  
24             with 'JMAP::Tester::LogWriter';
25             has handle => (is => 'ro', required => 1);
26 0     0 0   sub write { $_[0]->handle->print($_[1]) }
27             }
28              
29             {
30             package JMAP::Tester::LogWriter::Filename 0.101;
31              
32 1     1   593 use Moo;
  1         2  
  1         5  
33 1     1   396 use namespace::clean;
  1         10  
  1         6  
34             with 'JMAP::Tester::LogWriter';
35             has filename_template => (
36             is => 'ro',
37             default => 'jmap-tester-{T}-{PID}.log',
38             );
39              
40             has _handle => (is => 'rw');
41             has _pid => (is => 'rw', init_arg => undef, default => -1);
42              
43 0     0 0   sub write { $_[0]->_ensure_handle->print($_[1]) }
44              
45             sub _ensure_handle {
46 0     0     my ($self) = @_;
47 0 0         return $self->_handle if $self->_pid == $$;
48              
49 0           my $fn = $self->filename_template =~ s/\{T\}/$^T/gr =~ s/\{PID\}/$$/gr;
50 0 0         open my $fh, '>>', $fn or Carp::confess("can't open $fn for writing: $!");
51              
52 0           $fh->autoflush(1);
53              
54 0           $self->_handle($fh);
55 0           $self->_pid($$);
56 0           return $fh;
57             }
58             }
59              
60             1;
61              
62             __END__