LogLite.pm

filter: Statementstmt 84.6% 77 / 91 Branchbran 37.5% 6 / 16 Conditioncond 66.6% 10 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 83.3% 10 / 12 Podpod 75.0% 6 / 8 totaltotal 74.8% 113 / 151 CC 16 SCAR 14.1
CC
16
Cov
76%
new
6.8
level
6.0
log_line_numbers
6.0
CRAP 4.1
↑ Summary NullLogLite.pm »
1 package Log::LogLite;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 $VERSION = 0.82;
7  
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::LockedFile 0.21;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 my $TEMPLATE = '[<date>] <<level>> <called_by><default_message><message>
12 ';
13 1 my $LOG_LINE_NUMBERS = 0; # by default we do not log the line numbers
14  
15 ##########################################
16 # new($filepath)
17 # new($filepath,$level)
18 # new($filepath,$level,$default_message)
19 ##########################################
20 # the constructor
21 4 ▶sub new {
Subroutine new: called CC=6 SCAR=19.2
Pod documented
22 4 my $proto = shift; # get the class name
23 4 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
24 4 my $self = {};
25 # private data
26 4 $self->{FILE_PATH} = shift; # get the file path of the config file
27 4 ▶ $self->{LEVEL} = shift || 5; # the default level is 5
Condition100%2/2
shift() || 5
l!l
13
MC/DC100%1/1
shift() || 5
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
28 # report when:
29 # 0 the application is unusable
30 # 1 the application is going to be unusable
31 # 2 critical conditions
32 # 3 error conditions
33 # 4 warning conditions
34 # 5 normal but significant condition
35 # 6 informational
36 # 7+ debug-level messages
37 4 ▶ $self->{DEFAULT_MESSAGE} = shift || ""; # the default message
Condition50%1/2
shift() || ''
l!l
04
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
38 4 ▶ $self->{TEMPLATE} = shift || $TEMPLATE; # the template
Condition33%1/3
shift() || $TEMPLATE
l!l&&r!l&&!r
040
MC/DC0%0/2
shift() || $TEMPLATE
shift() $TEMPLATE
Truth table33%1/3
A || B
A= shift()B= $TEMPLATE
ABresult
1X1
011
000
39 4 $self->{LOG_LINE_NUMBERS} = $LOG_LINE_NUMBERS;
40 # we create IO::LockedFile object that can be locked later
41 4 $self->{FH} = new IO::LockedFile({ lock => 0 }, ">>".$self->{FILE_PATH});
42 4 ▶ unless ($self->{FH}->opened) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"FH"}->opened) 0 4 4
43 0 croak("Log::LogLite: Cannot open the log file $self->{FILE_PATH}");
44 }
45 4 bless ($self, $class);
46 4 return $self;
47 } # of new
48  
49 ##########################
50 # write($message, $level)
51 ##########################
52 # will log the message in the log file only if $level>=LEVEL
53 8 ▶sub write {
Subroutine write: called CC=4 SCAR=13.9
Pod documented
54 8 my $self = shift;
55 8 my $message = shift; # get the message are informational
56 8 ▶ my $level = shift || "-";
Condition100%2/2
shift() || '-'
l!l
53
MC/DC100%1/1
shift() || '-'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
57 8 ▶ if ($level ne "-" && $level > $self->{LEVEL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level ne "-" and $level > $self->{"LEVEL"}) 2 6 8
Condition100%3/3
$level ne "-" and $level > $self->{"LEVEL"}
!ll&&!rl&&r
332
MC/DC100%2/2
$level ne "-" and $level > $self->{"LEVEL"}
$level ne "-" $level > $self->{"LEVEL"}
Truth table100%3/3
A and B
A= $level ne "-"B= $level > $self->{"LEVEL"}
ABresult
0X0
100
111
58 # if the level of this message is higher
59 # then the deafult level - do nothing
60 2 return;
61 }
62  
63 # lock the log file before we append
64 6 $self->{FH}->lock();
65  
66 # parse the template
67 6 my $line = $self->{TEMPLATE};
68 6 $line =~ s!<date>!date_string()!igoe;
69 6 $line =~ s!<level>!$level!igo;
70 6 $line =~ s!<called_by>!$self->called_by()!igoe;
71 6 $line =~ s!<default_message>!$self->{DEFAULT_MESSAGE}!igo;
72 6 $line =~ s!<message>!$message!igo;
73 6 print {$self->{FH}} $line;
74
75 # unlock the file
76 6 $self->{FH}->unlock();
77 } # of write
78  
79 ##########################
80 # template()
81 # template($template)
82 ##########################
83 1 ▶sub template {
Subroutine template: called CC=2 SCAR=7.0
Pod documented
84 1 my $self = shift;
85 1 ▶ if (@_) { $self->{TEMPLATE} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
86 1 return $self->{TEMPLATE};
87 } # of template
88  
89 ##########################
90 # level()
91 # level($level)
92 ##########################
93 # an interface to LEVEL
94 0 ▶sub level {
Subroutine level: not called CC=2 SCAR=17.9
Pod documented
95 0 my $self = shift;
96 0 ▶ if (@_) { $self->{LEVEL} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
97 0 return $self->{LEVEL};
98 } # of level
99  
100 ###########################
101 # default_message()
102 # default_message($message)
103 ###########################
104 # an interface to DEFAULT_MESSAGE
105 1 ▶sub default_message {
Subroutine default_message: called CC=2 SCAR=7.0
Pod documented
106 1 my $self = shift;
107 1 ▶ if (@_) { $self->{DEFAULT_MESSAGE} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
108 1 return $self->{DEFAULT_MESSAGE};
109 } # of default_message
110  
111 ##########################
112 # log_line_numbers()
113 # log_line_numbers($log_line_numbers)
114 ##########################
115 # an interface to LOG_LINE_NUMBERS
116 0 ▶sub log_line_numbers {
Subroutine log_line_numbers: not called CC=2 SCAR=17.9
Pod documented
117 0 my $self = shift;
118 0 ▶ if (@_) { $self->{LOG_LINE_NUMBERS} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
119 0 return $self->{LOG_LINE_NUMBERS};
120 } # of log_line_numbers
121  
122 #######################
123 # date_string()
124 #######################
125 6 ▶sub date_string {
Subroutine date_string: called CC=1 SCAR=0.0
Pod undocumented
126 6 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
127 # note that there is no Y2K bug here. see localtime in perlfunc.
128 6 return sprintf("%02d/%02d/%04d %02d:%02d:%02d",
129 $mday, $mon + 1, $year + 1900, $hour, $min, $sec);
130 } # of date_string
131  
132 #######################
133 # called_by
134 #######################
135 5 ▶sub called_by {
Subroutine called_by: called CC=4 SCAR=14.5
Pod undocumented
136 5 my $self = shift;
137 5 my $depth = 2;
138 5 my $args;
139 my $pack;
140 5 my $file;
141 5 my $line;
142 5 my $subr;
143 5 my $has_args;
144 5 my $wantarray;
145 5 my $evaltext;
146 5 my $is_require;
147 5 my $hints;
148 5 my $bitmask;
149 5 my @subr;
150 5 my $str = "";
151 5 while (1) {
152 5 ($pack, $file, $line, $subr, $has_args, $wantarray, $evaltext,
153 $is_require, $hints, $bitmask) = caller($depth);
154 5 ▶ unless (defined($subr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $subr) 5 0 5
155 5 last;
156 }
157 0 $depth++;
158 0 ▶ $line = ($self->{LOG_LINE_NUMBERS}) ? "$file:".$line."-->" : "";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'LOG_LINE_NUMBERS'} ? : 0 0 0
159 0 push(@subr, $line.$subr);
160 }
161 5 @subr = reverse(@subr);
162 5 foreach $subr (@subr) {
163 0 $str .= $subr;
164 0 $str .= " > ";
165 }
166 5 $str =~ s/ > $/: /;
167 5 return $str;
168 } # of called_by
169  
170 1 1;
171 __END__
↑ Summary NullLogLite.pm »