Qgoda/Logger.pm

filter: Statementstmt 73.0% 65 / 89 Branchbran 45.8% 11 / 24 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 72.2% 13 / 18 Podpod 0.0% 0 / 8 totaltotal 61.5% 93 / 151 CC 18 SCAR 17.3
CC
18
Cov
66%
safeWarning
12.0
__makeMessage
11.5
client
6.0
CRAP 5.6
« Qgoda/Command.pm ↑ Summary Qgoda/Repository/Fetcher/Git.pm »
1 #! /bin/false
2  
3 # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15  
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18  
19 package Qgoda::Logger;
20 6 $Qgoda::Logger::VERSION = 'v0.9.2';
21 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 6 ▶use POSIX qw (setlocale LC_TIME strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use Term::ANSIColor qw(colored);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use IO::Interactive;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 sub new {
29 26 ▶ my ($class, %args) = @_;
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
30  
31 my $self = {
32 __debug => $args{debug},
33 __quiet => $args{quiet},
34 __prefix => $args{prefix},
35 __client => $args{client},
36 __logfile => $args{logfile},
37 __reqid => $args{reqid},
38 26 ▶ __log_fh => $args{log_fh} || \*STDOUT,
Condition50%1/2
$args{'log_fh'} || \*STDOUT
l!l
026
MC/DC0%0/1
$args{'log_fh'} || \*STDOUT
$args{'log_fh'}
Truth table50%1/2
A
A= $args{'log_fh'}
Aresult
11
00
39 };
40  
41 26 bless $self, $class;
42 }
43  
44 sub __logFunc {
45 14 ▶ my ($self, $type, @msg) = @_;
Subroutine __logFunc: called CC=1 SCAR=0.0
46  
47 14 my $msg = $self->__makeMessage($type, @msg);
48  
49 14 $self->{__log_fh}->print($msg);
50  
51 14 return 1;
52 }
53  
54 sub __makeMessage {
55 14 ▶ my ($self, $type, @msgs) = @_;
Subroutine __makeMessage: called CC=10 SCAR=24.4
56  
57 14 my $prefix = $self->{__prefix};
58 14 ▶ $prefix = '' unless $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 8 6 14
59  
60 14 my ($whole, $trailing) = split(/[^0-9]/, scalar gettimeofday());
61 14 ▶ $trailing ||= '';
Condition50%1/2
$trailing ||= ""
l!l
140
MC/DC0%0/1
$trailing ||= ""
$trailing
Truth table50%1/2
A
A= $trailing
Aresult
11
00
62 14 ▶ $trailing .= length($trailing) < 5
Branch: 1 branch
BranchTrueFalseTotal
length $trailing < 5 ? : 2 12 14
63 ? '0' x (5 - length($trailing))
64 : '';
65  
66 14 my $timefmt = "\%a \%b \%d \%H:\%M:\%S.$trailing \%Y";
67  
68 14 my $saved_locale = setlocale LC_TIME;
69 14 setlocale LC_TIME, 'POSIX';
70 14 my $timestamp = strftime $timefmt, localtime;
71 14 setlocale LC_TIME, $saved_locale;
72  
73 14 ▶ my $client = $self->{__client} || '';
Condition50%1/2
$self->{'__client'} || ''
l!l
014
MC/DC0%0/1
$self->{'__client'} || ''
$self->{'__client'}
Truth table50%1/2
A
A= $self->{'__client'}
Aresult
11
00
74 14 ▶ $client = "client $client" if $client;
Branch: 1 branch
BranchTrueFalseTotal
if $client 0 14 14
75  
76 14 ▶ my $reqid = $self->{__reqid} || '';
Condition50%1/2
$self->{'__reqid'} || ''
l!l
014
MC/DC0%0/1
$self->{'__reqid'} || ''
$self->{'__reqid'}
Truth table50%1/2
A
A= $self->{'__reqid'}
Aresult
11
00
77  
78 my $pre = join '',
79 34 map { "[$_]" }
80 14 grep { $_ } $timestamp, $reqid, $client, $type, $prefix;
81 14 ▶ $pre .= ' ' unless $msgs[0] =~ /^\[/;;
Branch: 1 branch
BranchTrueFalseTotal
unless $msgs[0] =~ /^\[/ 14 0 14
82  
83 14 ▶ my $colored = sub { $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
84  
85 14 ▶ if (IO::Interactive::is_interactive()) {
Branch: 1 branch
BranchTrueFalseTotal
if (IO::Interactive::is_interactive()) 0 14 14
86 0 my %colors = (
87 error => 'bold bright_red',
88 warning => 'red',
89 info => 'blue',
90 fatal => 'bold red',
91 );
92 0 ▶ if (exists $colors{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $colors{$type}) 0 0 0
93 0 ▶ $colored = sub { colored([$colors{$type}], $_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 }
95 }
96  
97 14 my @chomped = map { $pre . $colored->($_) }
98 14 grep { $_ ne '' }
99 14 map { $self->__trim($_) } @msgs;
100  
101 14 my $msg = join "\n", @chomped, '';
102  
103 14 return $msg;
104 }
105  
106 sub info {
107 9 ▶ my ($self, @msgs) = @_;
Subroutine info: called CC=2 SCAR=7.0
Pod undocumented
108 9 ▶ return if $self->{__quiet};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__quiet"} 0 9 9
109  
110 9 $self->__logFunc(info => @msgs);
111  
112 9 return 1;
113 }
114  
115 sub debug {
116 42 ▶ my ($self, @msgs) = @_;
Subroutine debug: called CC=2 SCAR=9.2
Pod undocumented
117 42 ▶ return unless $self->{__debug};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__debug"} 42 0 42
118  
119 0 $self->__logFunc(debug => @msgs);
120  
121 0 return 1;
122 }
123  
124 sub error {
125 0 ▶ my ($self, @msgs) = @_;
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
126  
127 0 $self->__logFunc (error => @msgs);
128  
129 0 return 1;
130 }
131  
132 sub warning {
133 5 ▶ my ($self, @msgs) = @_;
Subroutine warning: called CC=2 SCAR=7.0
Pod undocumented
134  
135 5 ▶ return if $self->{__quiet};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__quiet"} 0 5 5
136  
137 5 $self->__logFunc (warning => @msgs);
138  
139 5 return 1;
140 }
141  
142 sub safeWarning {
143 0 ▶ my ($self, @msgs) = @_;
Subroutine safeWarning: not called CC=3 SCAR=24.8
Pod undocumented
144  
145 # Print them even in quite mode.
146  
147 # And now escape them for logging in vim style.
148 0 foreach my $msg (@msgs) {
149 0 chomp $msg;
150 0 $msg =~ s{([\x00-\x1f^])}{
151 0 ▶ if ('^' eq $1) {
Branch: 1 branch
BranchTrueFalseTotal
if ('^' eq $1) { } 0 0 0
152 0 '^^';
153 } else {
154 0 '^' . chr (ord($1) + ord('@'));
155 }
156 }gexs;
157 }
158  
159 0 $self->__logFunc(warning => @msgs);
160  
161 0 return 1;
162 }
163  
164 sub fatal {
165 0 ▶ my ($self, @msgs) = @_;
Subroutine fatal: not called CC=1 SCAR=6.9
Pod undocumented
166  
167 0 $self->__logFunc (fatal => @msgs);
168  
169 0 exit 1;
170 }
171  
172 sub __trim {
173 14 ▶ my ($self, $line) = @_;
Subroutine __trim: called CC=2 SCAR=7.0
174 14 ▶ return '' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 14 14
175 14 $line =~ s/\s+$//mg;
176 14 return split /\n/, $line;
177 }
178  
179 sub client {
180 0 ▶ my ($self, $client) = @_;
Subroutine client: not called CC=2 SCAR=17.9
Pod undocumented
181  
182 0 ▶ $self->{__client} = $client if defined $client;
Branch: 1 branch
BranchTrueFalseTotal
if defined $client 0 0 0
183  
184 0 return $self->{__client};
185 }
186  
187 6 1;
« Qgoda/Command.pm ↑ Summary Qgoda/Repository/Fetcher/Git.pm »