blib/lib/Log/FileSimple.pm

filter: Statementstmt 98.2% 55 / 56 Branchbran 72.7% 16 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 3 totaltotal 85.5% 83 / 97 CC 15 SCAR 16.1
CC
15
Cov
89%
new
9.2
log
6.0
_init
2.0
CRAP 5.0
↑ Summary
1 package Log::FileSimple;
2 # questo componente utilizzando un file di log può essere usato per fare
3 # il debug di componenti
4  
5  
6 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 $Log::FileSimple::VERSION = '0.02';
15  
16  
17 # Fields that can be set in new method, with defaults
18 1 my %fields =(
19 file => '/tmp/log.log',
20 name => undef,
21 mask => 0xFFFF,
22 autoflush => 0,
23 );
24  
25 sub new
26 1 ▶{
Subroutine new: called CC=8 SCAR=22.2
Pod undocumented
27 1 my ($proto,%options) = @_;
28 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
29 1 my $self = {
30 %fields};
31 1 ▶ while (my ($key,$value) = each(%options)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %options) 4 1 5
32 4 ▶ if (exists($fields{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fields{$key}) { } 4 0 4
33 4 ▶ $self->{$key} = $value if (defined $value);
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 4 0 4
34 } else {
35 0 die ref($class) . "::new: invalid option '$key'\n";
36 }
37 }
38 1 foreach (keys(%fields)) {
39 die ref($class) . "::new: must specify value for $_"
40 4 ▶ if (!defined $self->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$_} 0 4 4
41 }
42 1 ▶ $self->{mask} = 0xFFFF if ($self->{mask} == -1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mask"} == -1 1 0 1
43 1 bless $self, $class;
44 1 $self->_init;
45 1 return $self;
46 }
47  
48 1 ▶sub _init {
Subroutine _init: called CC=2 SCAR=7.0
49 1 my $self = shift;
50 1 $self->{fh} = new FileHandle ">>$self->{file}";
51 1 $self->{fh}->autoflush($self->{autoflush});
52 1 ▶ die "Unable to write to $self->{file}" if (!defined $self->{fh});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"fh"} 0 1 1
53 }
54  
55 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
56 1 my $self = shift;
57 1 $self->{fh}->close;
58 1 undef $self->{fh};
59 # Enter here your code
60 }
61  
62 6 ▶sub log {
Subroutine log: called CC=6 SCAR=17.9
Pod undocumented
63 6 my $self = shift;
64 6 my %log_data = @_;
65
66 $log_data{'id'} = $self->{mask}
67 6 ▶ if (!exists $log_data{'id'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $log_data{"id"} 1 5 6
68 #$self->{fh}->print("Data :" . $log_data{'id'} . "-" .
69 # $self->{mask} . "-" . ($log_data{'id'} & $self->{mask}) ."\n");
70 6 ▶ return if (($log_data{'id'} & $self->{mask}) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if ($log_data{"id"} & $self->{"mask"}) == 0 1 5 6
71 5 my $timestamp = localtime;
72 5 my $sep = '-' x 80;
73 my $log_data = $log_data{'message'} . "\n"
74 5 ▶ if (exists $log_data{'message'});
Branch: 1 branch
BranchTrueFalseTotal
if exists $log_data{"message"} 5 0 5
75
76 5 ▶ if (exists $log_data{'objects'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $log_data{"objects"}) 2 3 5
77 2 foreach (@{$log_data{'objects'}}) {
78 3 $log_data .= Data::Dumper::Dumper($_) . "\n";
79 }
80 }
81  
82 5 my $print_data =<<EOF;
83 [$timestamp] -> $self->{name}
84 $log_data
85 $sep
86 EOF
87 5 $self->{fh}->print($print_data);
88 }
89  
90 2 ▶sub mask { my $s = shift; if (@_) { $s->{mask} = shift; } return $s->{mask}; }
Subroutine mask: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
91  
↑ Summary