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.400105';
3 13     13   4751 use strict;
  13         20  
  13         288  
4 13     13   134 use v5.10;
  13         34  
5 13     13   42 use Moo::Role;
  13         19  
  13         84  
6              
7 13     13   3078 use Cwd;
  13         21  
  13         3225  
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 4713     4713 0 3718 my $self = shift;
32 4713         113550 return $self->log_dir . "/" .$self->log_name;
33             }
34              
35             sub log
36             {
37 4645     4645 0 3872 my $self = shift;
38 4645 100       8604 return if(! $self->log_active);
39 4605         3851 my $message = shift;
40 4605         12963 open(my $log, ">>", $self->log_path);
41 4605         16885 print $log $message . "\n";
42 4605         57388 close($log);
43 4605 50       22535 if($self->log_on_stdout)
44             {
45 0         0 print $message . "\n";
46             }
47             }
48              
49             sub delete_log
50             {
51 108     108 0 163 my $self = shift;
52 108         204 unlink $self->log_path;
53             }
54              
55             1;