| 1 | package BalanceOfPower::Role::Logger; | ||
| 2 | $BalanceOfPower::Role::Logger::VERSION = '0.200000'; | ||
| 3 | use strict; | ||
| 4 | use v5.10; | ||
| 5 | use Moo::Role; | ||
| 6 | |||
| 7 | use Cwd; | ||
| 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 | my $self = shift; | ||
| 32 | return $self->log_dir . "/" .$self->log_name; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub log | ||
| 36 | { | ||
| 37 | my $self = shift; | ||
| 38 | return if(! $self->log_active); | ||
| 39 | my $message = shift; | ||
| 40 | open(my $log, ">>", $self->log_path); | ||
| 41 | print $log $message . "\n"; | ||
| 42 | close($log); | ||
| 43 | if($self->log_on_stdout) | ||
| 44 | { | ||
| 45 | print $message . "\n"; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | sub delete_log | ||
| 50 | { | ||
| 51 | my $self = shift; | ||
| 52 | unlink $self->log_path; | ||
| 53 | } | ||
| 54 | |||
| 55 | 1; |