File Coverage

lib/BalanceOfPower/Role/Logger.pm
Criterion Covered Total %
statement 22 23 95.6
branch 3 4 75.0
condition n/a
subroutine 7 7 100.0
pod 0 3 0.0
total 32 37 86.4


line stmt bran cond sub pod time code
1             package BalanceOfPower::Role::Logger;
2             $BalanceOfPower::Role::Logger::VERSION = '0.400110';
3 13     13   4087 use strict;
  13         17  
  13         265  
4 13     13   122 use v5.10;
  13         34  
5 13     13   38 use Moo::Role;
  13         13  
  13         64  
6              
7 13     13   2721 use Cwd;
  13         14  
  13         2782  
8              
9             has log_active => (
10             is => 'rw',
11             default => 1
12             );
13              
14             has log_name => (
15             is => 'rw',
16             default => "bop.log"
17             );
18              
19             has log_dir => (
20             is => 'rw',
21             default => sub { getcwd }
22             );
23              
24             has log_on_stdout => (
25             is => 'rw',
26             default => 0
27             );
28              
29             sub log_path
30             {
31 4735     4735 0 3715 my $self = shift;
32 4735         110827 return $self->log_dir . "/" .$self->log_name;
33             }
34              
35             sub log
36             {
37 4667     4667 0 3885 my $self = shift;
38 4667 100       8181 return if(! $self->log_active);
39 4627         3525 my $message = shift;
40 4627         13049 open(my $log, ">>", $self->log_path);
41 4627         16580 print $log $message . "\n";
42 4627         55243 close($log);
43 4627 50       22069 if($self->log_on_stdout)
44             {
45 0         0 print $message . "\n";
46             }
47             }
48              
49             sub delete_log
50             {
51 108     108 0 117 my $self = shift;
52 108         176 unlink $self->log_path;
53             }
54              
55             1;