| 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
MC/DC0%0/1
$args{'log_fh'} || \*STDOUT
$args{'log_fh'}
Truth table50%1/2
A
A= $args{'log_fh'}
| |||||||||||||
| 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
| |||||||||||||
| 59 | |||||||||||||
| 60 | 14 | my ($whole, $trailing) = split(/[^0-9]/, scalar gettimeofday()); | |||||||||||
| 61 | 14 | ▶ | $trailing ||= ''; | ||||||||||
|
Condition50%1/2
$trailing ||= ""
MC/DC0%0/1
$trailing ||= ""
$trailing
Truth table50%1/2
A
A= $trailing
| |||||||||||||
| 62 | 14 | ▶ | $trailing .= length($trailing) < 5 | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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'} || ''
MC/DC0%0/1
$self->{'__client'} || ''
$self->{'__client'}
Truth table50%1/2
A
A= $self->{'__client'}
| |||||||||||||
| 74 | 14 | ▶ | $client = "client $client" if $client; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | |||||||||||||
| 76 | 14 | ▶ | my $reqid = $self->{__reqid} || ''; | ||||||||||
|
Condition50%1/2
$self->{'__reqid'} || ''
MC/DC0%0/1
$self->{'__reqid'} || ''
$self->{'__reqid'}
Truth table50%1/2
A
A= $self->{'__reqid'}
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 183 | |||||||||||||
| 184 | 0 | return $self->{__client}; | |||||||||||
| 185 | } | ||||||||||||
| 186 | |||||||||||||
| 187 | 6 | 1; | |||||||||||