blib/lib/Log/Funlog.pm

filter: Statementstmt 83.1% 163 / 196 Branchbran 56.9% 66 / 116 Conditioncond 48.7% 19 / 39 MC/DCmcdc 28.0% 7 / 25 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 5 totaltotal 68.0% 268 / 394 CC 74 SCAR 44.0
CC
74
Cov
71%
new
107.9
wr
76.4
VERSION
2.0
CRAP 81.2
↑ Summary
1 package Log::Funlog;
2 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶ use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK );
9 2 @ISA=qw(Exporter);
10 2 @EXPORT=qw( );
11 2 @EXPORT_OK=qw( &error $VERBOSE $LEVELMAX $VERSION );
12 2 $VERSION='0.90';
13 2 ▶ sub VERSION {
Subroutine VERSION: called CC=2 SCAR=7.1
Pod undocumented
14 2 (my $me, my $askedver)=@_;
15 2 $VERSION=~s/(.*)_\d+/$1/;
16 2 ▶ croak "Please update: $me is version $VERSION and you asked version $askedver" if ($VERSION < $askedver);
Branch: 1 branch
BranchTrueFalseTotal
if $Log::Funlog::VERSION < $askedver 0 2 2
17 }
18 }
19 2 my @fun;
20 2 our %args;
21 0 ▶eval 'use Log::Funlog::Lang 0.4';
Subroutine BEGIN: called CC=1 SCAR=1.2
22 2 ▶if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 2 0 2
23 2 @fun=();
24 } else {
25 0 @fun=@{ (Log::Funlog::Lang->new())[1] };
26 }
27 #use Sys::Syslog;
28 2 my $count=0;
29 2 ▶use vars qw( %args $me $error_header $error $metaheader);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # Defined here, used later!
32 #####################################
33 2 my $rexpleft=q/<>{}()[]/; #Regular expression that are supposed to be on the left of the thing to print
34 2 my $rexprite=$rexpleft;
35 2 $rexprite=~tr/><}{)(][/<>{}()[]/; #tr same for right
36 2 my $rexpsym=q'+-=|!.\/'; #These can by anywhere (left or right)
37 2 $rexpleft=quotemeta $rexpleft;
38 2 $rexprite=quotemeta $rexprite;
39 2 $rexpsym=quotemeta $rexpsym;
40 2 my $level;
41 2 my $LOCK_SH=1;
42 2 my $LOCK_EX=2;
43 2 my $LOCK_NB=4;
44 2 my $LOCK_UN=8;
45 2 my $handleout; #Handle of the output
46 my %whattoprint;
47 2 my %colortable=(
48 'black' => "\e[30;1m",
49 'red' => "\e[31;1m",
50 'green' => "\e[32;1m",
51 'yellow' => "\e[33;1m",
52 'blue' => "\e[34;1m",
53 'magenta' => "\e[35;1m",
54 'cyan' => "\e[36;1m",
55 'white' => "\e[37;1m",
56 'none' => "\e[0m"
57 );
58 my %defaultcolors=(
59 'level' => $colortable{'red'},
60 'caller' => $colortable{'none'},
61 'date' => $colortable{'none'},
62 'prog' => $colortable{'magenta'},
63 2 'msg' => $colortable{'yellow'}
64 );
65 2 my @authorized_level_types=('numeric','sequential'); #Level types
66 2 my %colors; #will contain the printed colors. It is the same than %defaultcolors, but probably different :)
67 2 our $hadnocr=0; #Remember if previous call had $nocr (to print header at first call with $nocr, but not further)
68  
69 ################################################################################################################################
70 27 ▶sub replace { #replace things like %l<-->l by things like <-** ->
Subroutine replace: called CC=2 SCAR=6.9
Pod undocumented
71 27 my $header=shift;
72 27 my $what=shift;
73 27 my $center=shift;
74 27 ▶ if ($center) {
Branch: 1 branch
BranchTrueFalseTotal
if ($center) { } 21 6 27
75 21 $header=~s/\%$what$what/$center/; # for cases like %dd
76 #
77 # Now, for complicated cases like %d<-->d or %d-<>-d
78 #
79 21 $header=~s/\%$what(.*[$rexpleft]+)([$rexprite]+.*)$what/$1$center$2/; #%d-<>-d -> -<plop>-
80 #%d<-->d -> <-->
81 21 $header=~s/\%$what(.*[$rexpsym]+)([$rexpsym]+.*)$what/$1$center$2/; #-<plop>- -> -<plop>-
82 #<--> -> <-plop->
83 } else {
84 6 $header=~s/\%$what.*$what//;
85 }
86 27 return $header;
87 }
88 ################################################################################################################################
89 ################################################################################################################################
90 13 ▶sub new {
Subroutine new: called CC=44 SCAR=46.8
Pod undocumented
91 13 my $this = shift;
92 13 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0130
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
93 13 %args=@_; #getting args to a hash
94  
95  
96 # Okay, now sanity checking!
97 # This is cool because we have time, so we can do all kind of checking, calculating, things like that
98 #########################################
99 13 ▶ if (defined $args{daemon} and $args{daemon}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Log::Funlog::args{"daemon"} and $Log::Funlog::args{"daemon"}) 3 10 13
Condition100%3/3
defined $Log::Funlog::args{"daemon"} and $Log::Funlog::args{"daemon"}
!ll&&!rl&&r
913
MC/DC100%2/2
defined $Log::Funlog::args{"daemon"} and $Log::Funlog::args{"daemon"}
defined $Log::Funlog::args{"daemon"} $Log::Funlog::args{"daemon"}
Truth table100%3/3
A and B
A= defined $Log::Funlog::args{"daemon"}B= $Log::Funlog::args{"daemon"}
ABresult
0X0
100
111
100 3 ▶ croak 'You want me to be a daemon, but you didn\'t specifie a file to log to...' unless (defined $args{file});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Log::Funlog::args{"file"} 1 2 3
101 }
102 12 ▶ croak "'verbose' option is mandatory." if (! $args{'verbose'});
Branch: 1 branch
BranchTrueFalseTotal
unless $Log::Funlog::args{"verbose"} 1 11 12
103 11 ▶ croak "'verbose' should be of the form n/m or max/m" if (($args{'verbose'} !~ /^\d+\/\d+$/) and ($args{'verbose'} !~ /^[mM][aA][xX]\/\d+$/));
Branch: 1 branch
BranchTrueFalseTotal
unless $Log::Funlog::args{"verbose"} =~ m[^\d+/\d+$] or $Log::Funlog::args{"verbose"} =~ m[^[mM][aA][xX]/\d+$] 1 10 11
Condition100%3/3
$Log::Funlog::args{"verbose"} =~ m[^\d+/\d+$] or $Log::Funlog::args{"verbose"} =~ m[^[mM][aA][xX]/\d+$]
l!l&&r!l&&!r
911
MC/DC100%2/2
$Log::Funlog::args{"verbose"} =~ m[^\d+/\d+$] or $Log::Funlog::args{"verbose"} =~ m[^[mM][aA][xX]/\d+$]
$Log::Funlog::args{"verbose"} =~ m[^\d+/\d+$] $Log::Funlog::args{"verbose"} =~ m[^[mM][aA][xX]/\d+$]
Truth table100%3/3
A or B
A= $Log::Funlog::args{"verbose"} =~ m[^\d+/\d+$]B= $Log::Funlog::args{"verbose"} =~ m[^[mM][aA][xX]/\d+$]
ABresult
1X1
011
000
104  
105 # Parsing 'ltype' option
106 #########################################
107 10 ▶ if (defined $args{ltype}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Log::Funlog::args{'ltype'}) { } 0 10 10
108 0 ▶ if (! grep(/$args{ltype}/,@authorized_level_types)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /$Log::Funlog::args{"ltype"}/, @authorized_level_types) 0 0 0
109 0 croak "Unknow ltype '$args{ltype}'";
110 }
111 } else {
112 10 $args{ltype}='sequential';
113 }
114  
115 # Parsing 'verbose' option...
116 #########################################
117 10 my ($verbose,$levelmax)=split('/',$args{verbose});
118 10 ▶ $levelmax=$levelmax ? $levelmax : ""; #in case it is not defined...
Branch: 1 branch
BranchTrueFalseTotal
$levelmax ? : 10 0 10
119 10 ▶ $verbose=$levelmax if ($verbose =~ /^[mM][aA][xX]$/);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose =~ /^[mM][aA][xX]$/ 1 9 10
120 10 ▶ if (($verbose !~ /\d+/) or ($levelmax !~ /\d+/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $verbose =~ /\d+/ && $levelmax =~ /\d+/) { } 0 10 10
Condition33%1/3
$verbose =~ /\d+/ && $levelmax =~ /\d+/
!ll&&!rl&&r
0010
MC/DC0%0/2
$verbose =~ /\d+/ && $levelmax =~ /\d+/
$verbose =~ /\d+/ $levelmax =~ /\d+/
Truth table33%1/3
A && B
A= $verbose =~ /\d+/B= $levelmax =~ /\d+/
ABresult
0X0
100
111
121 0 carp "Arguments in 'verbose' should be of the form n/m, where n and m are numerics.\nAs this is a new feature, I'll assume you didn't upgraded your script so I'll make it compatible...\nAnyhow, consider upgrading soon!\n";
122 0 ▶ croak "No 'levelmax' provided" unless ($args{levelmax});
Branch: 1 branch
BranchTrueFalseTotal
unless $Log::Funlog::args{"levelmax"} 0 0 0
123 } else {
124 10 $args{verbose}=$verbose;
125 10 $args{levelmax}=$levelmax;
126 }
127 10 ▶ if ($args{verbose} > $args{levelmax}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Funlog::args{"verbose"} > $Log::Funlog::args{"levelmax"}) 0 10 10
128 0 carp "You ask verbose $args{verbose} and the max is $args{levelmax}. I set your verbose at $args{levelmax}.\n";
129 0 $args{verbose}=$args{levelmax};
130 }
131  
132  
133 # Time for fun!
134 #########################################
135 10 ▶ if (defined $args{fun}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Log::Funlog::args{"fun"}) 0 10 10
136 0 ▶ croak "'fun' should only be a number (between 0 and 100, bounds excluded)." if ($args{fun} !~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $Log::Funlog::args{"fun"} =~ /^\d+$/ 0 0 0
137 0 ▶ croak "0<fun<=100" if ($args{fun}>100 or $args{fun}<=0);
Branch: 1 branch
BranchTrueFalseTotal
if $Log::Funlog::args{"fun"} > 100 or $Log::Funlog::args{"fun"} <= 0 0 0 0
Condition0%0/3
$Log::Funlog::args{"fun"} > 100 or $Log::Funlog::args{"fun"} <= 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$Log::Funlog::args{"fun"} > 100 or $Log::Funlog::args{"fun"} <= 0
$Log::Funlog::args{"fun"} > 100 $Log::Funlog::args{"fun"} <= 0
Truth table0%0/3
A or B
A= $Log::Funlog::args{"fun"} > 100B= $Log::Funlog::args{"fun"} <= 0
ABresult
1X1
011
000
138 0 ▶ croak "You want fun but Log::Funlog::Lang is not available, or is too old." if ($#fun <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $#fun <= 0 0 0 0
139 }
140  
141 # Colors
142 #########################################
143 #We will build %colors here.
144 #If color is wanted:
145 # if default is wanted, %colors = %defaultcolors
146 # if not, %colors = %defaultcolors, overriden by the parameters provided
147 #If no colors is wanted, %colors will be filled with the 'none' colors.
148 #
149 #This way of doing should be quicker :)
150 #
151 10 ▶ if (exists $args{'colors'} and $args{'colors'} ) { #If color is wanted
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Log::Funlog::args{'colors'} and $Log::Funlog::args{'colors'}) { } 3 7 10
Condition66%2/3
exists $Log::Funlog::args{"colors"} and $Log::Funlog::args{"colors"}
!ll&&!rl&&r
703
MC/DC50%1/2
exists $Log::Funlog::args{"colors"} and $Log::Funlog::args{"colors"}
exists $Log::Funlog::args{"colors"} $Log::Funlog::args{"colors"}
Truth table66%2/3
A and B
A= exists $Log::Funlog::args{"colors"}B= $Log::Funlog::args{"colors"}
ABresult
0X0
100
111
152 2 ▶ use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
153 3 ▶ if ($Config{'osname'} eq 'MSWin32') { #Oh oh!
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Funlog::Config{'osname'} eq 'MSWin32') { } 0 3 3
154 0 carp 'Colors wanted, but MSwin detected. Colors deactivated (because not implemented yet)';
155 0 delete $args{'colors'};
156 0 $colortable{'none'}=''; #putting 'none' color to void
157 0 foreach my $color (keys %defaultcolors) {
158 0 $colors{$color}=$colortable{'none'}; #and propagating it
159 }
160 # no Config;
161 } else { #We are not in MSWin...
162 3 ▶ if (ref(\$args{'colors'}) eq 'SCALAR') { #default colors?
Branch: 2 branches
BranchTrueFalseTotal
if (ref \$Log::Funlog::args{'colors'} eq 'SCALAR') { } 1 2 3
elsif (ref $Log::Funlog::args{'colors'} eq 'HASH') { } 1 1 2
163 1 ▶ %colors=%defaultcolors if ($args{'colors'});
Branch: 1 branch
BranchTrueFalseTotal
if $Log::Funlog::args{"colors"} 1 0 1
164 } elsif(ref($args{'colors'}) eq 'HASH') { #No... Overridden colors :)
165 1 foreach my $item (keys %defaultcolors) {
166 $colors{$item}=exists ${ #If the color is provided
167 5 $args{'colors'}
168 }{$item}?
169 $colortable{
170 ${
171 3 $args{'colors'} #we take it
172 }{$item}
173 5 ▶ }:$defaultcolors{$item}; #if not, we take the default one
Branch: 1 branch
BranchTrueFalseTotal
exists do { $Log::Funlog::args{'colors'} }->{$item} ? : 3 2 5
174 }
175 } else {
176 1 croak("'colors' must be type of SCALAR or HASH, not ".ref($args{'colors'})."\n");
177 }
178 }
179 } else { #no colors? so the color table will contain the color 'none'
180 7 $colortable{'none'}=''; #Avoid printing "\e[0m" :)
181 7 foreach my $item (keys %defaultcolors) {
182 35 $colors{$item}=$colortable{'none'};
183 }
184 }
185  
186  
187 # Error handler
188 #########################################
189 9 ▶ $error_header=defined $args{error_header} ? $args{error_header} : '## Oops! ##';
Branch: 1 branch
BranchTrueFalseTotal
defined $Log::Funlog::args{'error_header'} ? : 0 9 9
190  
191 # We define default cosmetic if no one was defined
192 #########################################
193 9 ▶ if (not defined $args{cosmetic}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $Log::Funlog::args{'cosmetic'}) { } 5 4 9
elsif (not $Log::Funlog::args{'cosmetic'} =~ /^[[:^cntrl:]]$/) { } 3 1 4
194 5 $args{'cosmetic'}='x';
195 } elsif ($args{'cosmetic'} !~ /^[[:^cntrl:]]$/) {
196 3 croak("'cosmetic' must be one character long, and printable.");
197 }
198  
199 # Parsing header. Goal is to avoid work in the wr() function
200 #########################################
201 6 ▶ if (defined $args{header}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Log::Funlog::args{'header'}) { } 1 5 6
202  
203 1 $metaheader=$args{header};
204  
205 # if %ll is present, we can be sure that it will always be, but it will vary so we replace by a variable
206 1 ▶ if ($metaheader=~/\%l.*l/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Funlog::metaheader =~ /\%l.*l/) 1 0 1
207 1 $whattoprint{'l'}=1;
208 1 $metaheader=replace($metaheader,"l","\$level");
209 }
210  
211 # same for %dd
212 1 ▶ $whattoprint{'d'}=1 if ($metaheader=~/\%d.*d/);
Branch: 1 branch
BranchTrueFalseTotal
if $Log::Funlog::metaheader =~ /\%d.*d/ 1 0 1
213 1 $metaheader=replace($metaheader,"d",$colors{'date'}."\$date".$colortable{'none'});
214  
215 # but %pp won't vary
216 1 $me=basename("$0");
217 1 chomp $me;
218 1 ▶ $whattoprint{'p'}=1 if ($metaheader=~/\%p.*p/);
Branch: 1 branch
BranchTrueFalseTotal
if $Log::Funlog::metaheader =~ /\%p.*p/ 1 0 1
219 1 $metaheader=replace($metaheader,"p",$colors{'prog'}.$me.$colortable{'none'});
220 # and stack will be present or not, depending of the state of the stack
221 1 ▶ $whattoprint{'s'}=1 if ($metaheader=~/\%s.*s/);
Branch: 1 branch
BranchTrueFalseTotal
if $Log::Funlog::metaheader =~ /\%s.*s/ 1 0 1
222  
223 1 ▶ if ((! defined $args{'caller'}) and ($metaheader=~/\%s.*s/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $Log::Funlog::args{"caller"} and $Log::Funlog::metaheader =~ /\%s.*s/) 0 1 1
Condition33%1/3
not defined $Log::Funlog::args{"caller"} and $Log::Funlog::metaheader =~ /\%s.*s/
!ll&&!rl&&r
100
MC/DC0%0/2
not defined $Log::Funlog::args{"caller"} and $Log::Funlog::metaheader =~ /\%s.*s/
not defined $Log::Funlog::args{"caller"} $Log::Funlog::metaheader =~ /\%s.*s/
Truth table33%1/3
not(A) and B
A= not defined $Log::Funlog::args{"caller"}B= $Log::Funlog::metaheader =~ /\%s.*s/
ABresult
0X0
100
111
224 0 carp "\%ss is defined but 'caller' option is not specified.\nI assume 'caller => 1'";
225 0 $args{'caller'}=1;
226 }
227 } else {
228 5 $metaheader="";
229 }
230  
231 # Daemon. We calculate here the output handle to use
232 ##########################################
233 6 ▶ if ($args{'daemon'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Funlog::args{'daemon'}) { } 2 4 6
234 2 ▶ open($handleout,">>$args{'file'}") or croak "$args{'file'}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $handleout, qq[>>$Log::Funlog::args{"file"}] or croak(qq[$Log::Funlog::args{"file"}: $!]) 2 0 2
235 } else {
236 4 $handleout=\*STDERR;
237 }
238 # -n handling
239 ##########################################
240 6 ▶ $args{'-n'}='-n' unless $args{'-n'};
Branch: 1 branch
BranchTrueFalseTotal
unless $Log::Funlog::args{"-n"} 6 0 6
241  
242 ##########################################
243 # End of parsing
244 ##########################################
245  
246 6 my $self = \&wr;
247 6 bless $self, $class; #The function's address is now a Log::Funlog object
248 # return $self; #Return the function's address, that is an object Log::Funlog
249 }
250  
251 ########################################################################################
252 ########################################################################################
253 # This is the main function
254 ########################################################################################
255 ########################################################################################
256 26 ▶sub wr {
Subroutine wr: called CC=29 SCAR=43.4
Pod undocumented
257 26 my $level=shift; #log level wanted by the user
258 26 ▶ return if ($level > $args{verbose} or $level == 0); #and exit if it is greater than the verbosity
Branch: 1 branch
BranchTrueFalseTotal
if $level > $Log::Funlog::args{"verbose"} or $level == 0 1 25 26
Condition66%2/3
$level > $Log::Funlog::args{"verbose"} or $level == 0
l!l&&r!l&&!r
1025
MC/DC50%1/2
$level > $Log::Funlog::args{"verbose"} or $level == 0
$level > $Log::Funlog::args{"verbose"} $level == 0
Truth table66%2/3
A or B
A= $level > $Log::Funlog::args{"verbose"}B= $level == 0
ABresult
1X1
011
000
259  
260 25 my $prevhandle=select $handleout;
261  
262 25 my $return_code;
263 my $nocr;
264  
265 # Header building!!
266 #####################################
267 25 ▶ if (defined $_[0] and $_[0] eq $args{'-n'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] eq $Log::Funlog::args{'-n'}) { } 0 25 25
Condition33%1/3
defined $_[0] and $_[0] eq $Log::Funlog::args{"-n"}
!ll&&!rl&&r
0250
MC/DC0%0/2
defined $_[0] and $_[0] eq $Log::Funlog::args{"-n"}
defined $_[0] $_[0] eq $Log::Funlog::args{"-n"}
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] eq $Log::Funlog::args{"-n"}
ABresult
0X0
100
111
268 0 shift;
269 0 $nocr=1;
270 } else {
271 25 $nocr=0;
272 };
273 25 ▶ if ($metaheader and not $hadnocr) { #Hey hey! Won't calculate anything if there is nothing to print!
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Funlog::metaheader and not $Log::Funlog::hadnocr) 24 1 25
Condition66%2/3
$Log::Funlog::metaheader and not $Log::Funlog::hadnocr
!ll&&!rl&&r
1024
MC/DC50%1/2
$Log::Funlog::metaheader and not $Log::Funlog::hadnocr
$Log::Funlog::metaheader not $Log::Funlog::hadnocr
Truth table66%2/3
A and not(B)
A= $Log::Funlog::metaheaderB= not $Log::Funlog::hadnocr
ABresult
0X0
100
111
274 24 my $header=$metaheader;
275 24 ▶ if ($whattoprint{'s'}) { #if the user want to print the call stack
Branch: 1 branch
BranchTrueFalseTotal
if ($whattoprint{'s'}) { } 24 0 24
276 24 my $caller;
277 24 ▶ if (($args{'caller'} =~ /^last$/) or ($args{'caller'} =~ /^1$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Funlog::args{'caller'} =~ /^last$/ or $Log::Funlog::args{'caller'} =~ /^1$/) { } 0 24 24
Condition33%1/3
$Log::Funlog::args{"caller"} =~ /^last$/ or $Log::Funlog::args{"caller"} =~ /^1$/
l!l&&r!l&&!r
0024
MC/DC0%0/2
$Log::Funlog::args{"caller"} =~ /^last$/ or $Log::Funlog::args{"caller"} =~ /^1$/
$Log::Funlog::args{"caller"} =~ /^last$/ $Log::Funlog::args{"caller"} =~ /^1$/
Truth table33%1/3
A or B
A= $Log::Funlog::args{"caller"} =~ /^last$/B= $Log::Funlog::args{"caller"} =~ /^1$/
ABresult
1X1
011
000
278 0 ▶ $caller=(caller($error?2:1))[3];
Branch: 1 branch
BranchTrueFalseTotal
$Log::Funlog::error ? : 0 0 0
279 } else { #okay... I will have to unstack all the calls to an array...
280 24 my @stack;
281 24 my $i=1;
282 24 ▶ while (my $tmp=(caller($error?$i+1:$i))[3]) { #turn as long as there is something on the stack
Branch: 2 branches
BranchTrueFalseTotal
if (my $tmp = (caller($Log::Funlog::error ? $i + 1 : $i))[3]) 36 24 60
$Log::Funlog::error ? : 9 51 60
283 36 push @stack,($tmp);
284 36 $i++;
285 };
286 24 @stack=reverse @stack;
287 24 ▶ if ($args{'caller'} eq "all") {; #all the calls
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Funlog::args{'caller'} eq 'all') { } 24 0 24
288 24 $caller=join(':',@stack);
289 } else {
290 0 ▶ if ($#stack >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#stack >= 0) 0 0 0
291 0 my $num=$args{'caller'};
292 0 ▶ $num=$#stack if ($num>=$#stack); #in case the stack is greater that the number of call we want to print
Branch: 1 branch
BranchTrueFalseTotal
if $num >= $#stack 0 0 0
293 0 ▶ if ($args{'caller'} eq "all") { #all the cals
Branch: 3 branches
BranchTrueFalseTotal
if ($Log::Funlog::args{'caller'} eq 'all') { } 0 0 0
elsif ($Log::Funlog::args{'caller'} =~ /^-\d+$/) { } 0 0 0
elsif ($Log::Funlog::args{'caller'} =~ /^\d+$/) { } 0 0 0
294 0 $caller=join(':',@stack);
295 } elsif ($args{'caller'} =~ /^-\d+$/) { #the n first calls
296 0 $caller=join(':',splice(@stack,0,-$num));
297 } elsif ($args{'caller'} =~ /^\d+$/) { #just the n last calls
298 0 $caller=join(':',splice(@stack,1+$#stack-$num));
299 }
300 }
301 }
302 }
303  
304 24 ▶ if ($caller) { #if there were something on the stack (ie: we are not in 'main')
Branch: 1 branch
BranchTrueFalseTotal
if ($caller) { } 18 6 24
305 18 $caller=~s/main\:\://g; #wipe 'main'
306 18 my @a=split(/\//,$caller); #split..
307 18 @a=reverse @a; #reverse...
308 18 $header=replace($header,"s",$colors{'caller'}.join(':',@a).$colortable{'none'});
309 } else {
310 6 $header=replace($header,"s");
311 }
312 } else {
313 0 $header=replace($header,"s");
314 }
315 24 ▶ if ($whattoprint{'d'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($whattoprint{"d"}) 24 0 24
316 24 my $tmp=scalar localtime;
317 24 $header=~s/\$date/$tmp/;
318 }
319 24 ▶ if ($whattoprint{'l'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($whattoprint{"l"}) 24 0 24
320 24 my $tmp;
321 24 ▶ if ($args{ltype} eq 'numeric') {
Branch: 2 branches
BranchTrueFalseTotal
if ($Log::Funlog::args{'ltype'} eq 'numeric') { } 0 24 24
elsif ($Log::Funlog::args{'ltype'} eq 'sequential') { } 24 0 24
322 0 $tmp=$colors{'level'}.$level.$colortable{'none'};
323 } elsif ($args{ltype} eq 'sequential') {
324 24 $tmp=$colors{'level'}.$args{cosmetic} x $level. " " x ($args{levelmax} - $level).$colortable{'none'}; # [xx ]
325 }
326 24 $header=~s/\$level/$tmp/;
327 }
328  
329 #####################################
330 # End of header building
331 #####################################
332 24 print $header; #print the header
333 }
334 25 print $colors{'msg'};
335 #and then print all the things the user wants me to print
336 25 print @_;
337 25 $return_code=join('',@_);
338 25 print $colortable{'none'};
339 25 ▶ print "\n" unless $nocr;
Branch: 1 branch
BranchTrueFalseTotal
unless $nocr 25 0 25
340 #Passe le fun autour de toi!
341 25 ▶ print $fun[1+int(rand $#fun)],"\n" if ($args{fun} and (rand(100)<$args{fun}) and ($count>10)); #write a bit of fun, but not in the first 10 lines
Branch: 1 branch
BranchTrueFalseTotal
if $Log::Funlog::args{"fun"} and rand 100 < $Log::Funlog::args{"fun"} and $count > 10 0 25 25
Condition33%2/6
$Log::Funlog::args{"fun"} and rand 100 < $Log::Funlog::args{"fun"} and $count > 10
!ll&&!rl&&r
2500
$Log::Funlog::args{"fun"} and rand 100 < $Log::Funlog::args{"fun"}
!ll&&!rl&&r
2500
MC/DC0%0/3
$Log::Funlog::args{"fun"} and rand 100 < $Log::Funlog::args{"fun"} and $count > 10
$Log::Funlog::args{"fun"} rand 100 < $Log::Funlog::args{"fun"} $count > 10
Truth table25%1/4
A and B and C
A= $Log::Funlog::args{"fun"}B= rand 100 < $Log::Funlog::args{"fun"}C= $count > 10
ABCresult
0XX0
10X0
1100
1111
342 #print "nc:$nocr\n";
343 25 $count++;
344 25 ▶ if ($nocr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nocr) { } 0 25 25
345 0 $hadnocr=1;
346 } else {
347 25 $hadnocr=0;
348 }
349 #print "hnc:$hadnocr\n";
350  
351 25 select($prevhandle);
352 25 return $return_code;
353 }
354 3 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod undocumented
355 3 $error=1;
356 3 my $ec=wr(1,$error_header," ",@_);
357 3 $error=0;
358 3 return $ec;
359 }
360 2 1;
361 =head1 NAME
362  
363 Log::Funlog - Log module with fun inside!
364  
365 =head1 SYNOPSIS
366  
367 use Log::Funlog;
368 *my_sub=Log::Funlog->new(
369 parameter => value,
370 ...
371 );
372  
373 [$string=]my_sub($priority [,$string | @array [,$string | @array [, ... ] ]] );
374  
375 =head1 DESCRIPTION
376  
377 This is a Perl module intended ton manage the logs you want to do from your Perl scripts.
378  
379 It should be easy to use, and provide all the fonctionalities you want.
380  
381 Just initialize the module, then use is as if it was an ordinary function!
382  
383 When you want to log something, just write:
384  
385 your-sub-log(priority,"what"," I ","wanna log is: ",@an_array)
386  
387 then the module will analyse if the priority if higher enough (seeing L<verbose> option). If yes, your log will be written with the format you decided on STDERR (default) or a file.
388  
389 As more, the module can write funny things to your logs, if you want ;) It can be very verbose, or just ... shy :)
390  
391 L<Log::Funlog|Log::Funlog> may export an 'error' function: it logs your message with a priority of 1 and with an specific (parametrable) string. You can use it when you want to highlight error messages in your logsi with a pattern.
392  
393 Parameters are: L<header>, L<error_header>, L<cosmetic> ,L<verbose>, L<file>, L<daemon>, L<fun>, L<colors>, L<splash>, L<-n>, L<caller>, L<ltype>
394  
395 L<verbose> is mandatory.
396  
397 =head2 MANDATORES OPTIONS
398  
399 =over
400  
401 =item B<verbose>
402  
403 In the form B<n>/B<m>, where B<n><B<m> or B<n>=max.
404  
405 B<n> is the wanted verbosity of your script, B<m> if the maximum verbosity of your script.
406  
407 B<n> can by superior to B<m>. It will just set B<n>=B<m>
408  
409 Everything that is logged with a priority more than B<n> (in case B<n> is numeric) will not be logged.
410  
411 0 if you do not want anything to be printed.
412  
413 The common way to define B<n> is to take it from the command line with Getopt:
414  
415 use Getopt::Long;
416 use Log::Funlog;
417 &GetOptions("verbose=s",\$verbose);
418 *Log=Log::Funlog->new(
419 [...]
420 verbose => "$verbose/5",
421 [...]
422 )
423  
424 In this case, you can say --verbose=max so that it will log with the max verbosity level available (5, here)
425  
426 This option is backward compatible with 0.7.x.x versions.
427  
428 See L</EXAMPLE>
429  
430 =back
431  
432 =head2 NON MANDATORIES OPTIONS
433  
434 =over
435  
436 =item B<caller>
437  
438 'all' if you want the stack of subs.
439  
440 'last' if you want the last call.
441  
442 If you specify a number B<n>, it will print the B<n> last calls (yes, if you specify '1', it is equivalent to 'last')
443  
444 If this number is negative, it will print the B<n> first calls.
445  
446 Of course, nothing will happen if no L<header> is specified, nor %ss in the L<header> ...
447  
448 =item B<colors>
449  
450 Put colors in the logs :)
451  
452 If you just put '1', it will use default colors:
453  
454 colors => '1',
455  
456 If you want to override default colors, specify a hash containing item => color
457  
458 colors => {'prog' => 'white', 'date' => 'yellow' },
459  
460 Items are:
461  
462 caller: for the stack of calls,
463 prog: for the name of the program,
464 date: for the current date,
465 level: for the log level,
466 msg: for the log message
467  
468 Colors are:
469 black, red, green, yellow, blue, magenta, cyan, white and none
470  
471 =item B<cosmetic>
472  
473 An alphanumeric char to indicate the log level in your logs.
474  
475 There will be as many as these chars as the log level of the string being logged. See L</EXAMPLE>
476  
477 Should be something like 'x', or '*', or '!', or any printable single character.
478  
479 =item B<daemon>
480  
481 1 if the script should be a daemon. (default is 0: not a daemon)
482  
483 When B<daemon>=1, L<Log::Funlog> write to L<file> instead of B<STDERR>
484  
485 If you specify B<daemon>, you must specify L<file>
486  
487 The common way to do is the same that with L<verbose>: with Getopt
488  
489 =item B<error_header>
490  
491 Header you want to see in the logs when you call the B<error> function (if you import it, of course)
492  
493 Default is '## Oops! ##'.
494  
495 =item B<file>
496  
497 File to write logs to.
498  
499 MUST be specified if you specify L<daemon>
500  
501 File is opened when initializing, and never closed by the module. That is mainly to avoid open and close the file each time you log something and then increase speed.
502  
503 Side effect is that if you tail -f the log file, you won't see them in real time.
504  
505 =item B<fun>
506  
507 Probability of fun in your logs.
508  
509 Should be: 0<fun<=100
510  
511 It use Log::Funlog::Lang
512  
513 =item B<header>
514  
515 Pattern specifying the header of your logs.
516  
517 The fields are made like this: %<B<letter>><B<delimiters1>><B<delimiters2>><B<same_letter>>
518  
519 The B<letter> is, for now:
520  
521 s: stack calls
522 d: date
523 p: name of the prog
524 l: verbosity level
525  
526 B<delimiters1> is something taken from +-=|!./\<{([ and B<delimiters2> is take from +-=|!./\>})] (replacement regexp is s/\%<letter>([<delimiters1>]*)([<delimiters2>*)<letter>/$1<field>$2/ ). B<delimiters1> will be put before the field once expanded, B<delimiters2> after.
527  
528 Example:
529 '%dd %p::p hey %l[]l %s{}s '
530  
531 should produce something like:
532  
533 Wed Sep 22 18:50:34 2004 :gna.pl: hey [x ] {sub48} Something happened
534 ^------this is %dd-----^ ^%p::p^ ^%l[]l^ ^%s{}s^
535  
536 If no header is specified, no header will be written, and you would have:
537  
538 Something happened
539  
540 Although you can specify a pattern like that:
541 ' -{(%d(<>)d)}%p-<>-p %l-<()>-l '
542  
543 is not advisable because the code that whatch for the header is not that smart and will probably won't do what you expect.
544  
545 Putting things in %?? is good only for %ss because stack won't be printed if there is nothing to print:
546 ' {%ss} '
547  
548 will print something like that if you log from elsewhere than a sub:
549 {}
550  
551 Although
552 ' %s{}s '
553  
554 won't print anything if you log from outside a sub. Both will have the same effect if you log from inside a sub.
555  
556 You should probably always write things like:
557 ' -{((<%dd>))}-<%pp>- -<(%ll)>- '
558  
559 =item B<ltype>
560  
561 Level printing type. Can be B<sequential> or B<numeric>.
562  
563 B<sequential> will print level like that: [xx ]. This is the default.
564  
565 B<numeric> will print level like that: [2]
566  
567 =item B<splash>
568  
569 1 if you want a 'splash log'
570  
571 =item B<-n>
572  
573 You can write stuff like that:
574  
575 Log(1,'-n',"plop");
576 Log(1,"plop");
577  
578 This will output something like:
579  
580 [x] plopplop
581  
582 '-n' parameter allows you to use something else than '-n' to copy the behaviour of the '-n' parameter of L<echo(3)>
583  
584 =back
585  
586 =cut
587  
588  
589 =pod
590  
591 =head1 EXAMPLE
592  
593 Here is an example with almost all of the options enabled:
594  
595 $ vi gna.pl
596 #!/usr/bin/perl -w
597 use Log::Funlog qw( error );
598 *Log=new Log::Funlog(
599 file => "zou.log", #name of the file
600 verbose => "3/5", #verbose 3 out of a maximum of 5
601 daemon => 0, #I am not a daemon
602 cosmetic => 'x', #crosses for the level
603 fun => 10, #10% of fun (que je passe autour de moi)
604 error_header => 'Groumpf... ', #Header for true errors
605 header => '%dd %p[]p %l[]l %s{}s ', #The header
606 caller => 1); #and I want the name of the last sub
607  
608 Log(1,"I'm logged...");
609 Log(3,"Me too...");
610 Log(4,"Me not!"); #because 4>verbose
611 sub ze_sub {
612 $hop=1;
613 Log(1,"One","two",$hop,"C"."++");
614 error("oups!");
615 }
616 ze_sub;
617 error("Zut");
618  
619 :wq
620  
621 $ perl gna.pl
622 Tue Jul 26 15:39:41 2005 [gna.pl] [x ] I'm logged...
623 Tue Jul 26 15:39:41 2005 [gna.pl] [xxx ] Me too...
624 Tue Jul 26 15:39:41 2005 [gna.pl] [x ] {ze_sub} Onetwo1C++
625 Tue Jul 26 15:39:41 2005 [gna.pl] [x ] {ze_sub} Groumpf... oups!
626 Tue Jul 26 15:39:41 2005 [gna.pl] [x ] Groumpf... Zut
627  
628 =head1 BUGS
629  
630 =over
631  
632 =item 1-
633  
634 This:
635  
636 header => '-(%dd)--( %p)><(p )-( %l)-<>-(l %s)<>(s '
637  
638 won't do what you expect ( this is the ')><(' )
639  
640 Workaround is:
641  
642 header => '-(%dd)--( )>%pp<( )-( %l)-<>-(l %s)<>(s '
643  
644 And this kind of workaround work for everything but %ss, as it is not calculated during initialization.
645  
646 =item 2-
647  
648 *Log=Log::Funlog->new(
649 colors => 1,
650 colors => {
651 date => 'white'
652 }
653 )
654  
655 Is not the same as:
656  
657 *Log=Log::Funlog->new(
658 colors => {
659 date => 'white'
660 },
661 colors => 1,
662 )
663  
664 First case will do what you expect, second case will put default colors.
665  
666 To avoid that, specify EITHER colors => 1 OR colors => {<something>}
667  
668 =back
669  
670 =head1 DEPENDENCIES
671  
672 Log::Funlog::Lang > 0.3 : provide the funny messages.
673  
674  
675 =head1 DISCUSSION
676  
677 As you can see, the 'new' routine return a pointer to a sub. It's the easiest way I found to make this package as easy as possible to use.
678  
679 I guess that calling the sub each time you want to log something (and even if it won't print anything due to the too low level of the priority given) is not really fast...
680  
681 Especially if you look at the code, and you see all the stuffs the module do before printing something.
682  
683 But in fact, I tried to make it rather fast, that mean that if the module try to know as fast as possible if it will write something, and what to write
684  
685 If you want a I<really> fast routine of log, please propose me a way to do it, or do it yourself, or do not log :)
686  
687 You can probably say:
688  
689 my Log::Funlog $log = new Log::Funlog; # $log is now an Log::Funlog object. $log contain the address of the sub used to write.
690  
691 Then:
692  
693 &{$log}(1,'plop');
694  
695 But it is probably not convenient.
696  
697 =head1 HISTORY
698  
699 I'm doing quite a lot of Perl scripts, and I wanted the scripts talk to me. So I searched a log routine.
700  
701 As I didn't found it on the web, and I wanted something more 'personnal' than syslog (I didn't want my script write to syslog), I started to write a very little routine, that I copied to all the scripts I made.
702  
703 As I copied this routine, I added some stuff to match my needs; I wanted something rather fast, easy to use, easy to understand (even for me :P ), quite smart and ... a little bit funny :)
704  
705 The I wrote this module, that I 'use Log::Funlog' in each of my scripts.
706  
707 =head1 CHANGELOG
708  
709 See Changelog
710  
711 =head1 AUTHOR
712  
713 Gabriel Guillon, from Cashew team
714  
715 korsani-spam@caramail(spaaaaaammmm).com[spppam]
716  
717 (remove you-know-what :)
718  
719 =head1 LICENCE
720  
721 As Perl itself.
722  
723 Let me know if you have added some features, or removed some bugs ;)
724  
725 =cut
726  
↑ Summary