« blib/lib/Bio/Gonzales/Graphics/Glyph/colorfulBox.pm
↑ Summary
blib/lib/Bio/Gonzales/Tools/SeqMask.pm »
| 1 | package Bio::Gonzales::Util::Log; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # shamelessly stolen from Mojo::Log, thanks to Sebastian Riedel & contributors for creating it. | ||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use Fcntl ':flock'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use POSIX qw/strftime/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 13 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 13 | our $VERSION = '0.062'; # VERSION | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Supported log level | ||||||||||||||||||||||||||||
| 18 | 13 | my $LEVEL = { debug => 1, info => 2, warn => 3, error => 4, fatal => 5 }; | |||||||||||||||||||||||||||
| 19 | 13 | my $is_thread = eval '$threads::threads'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 13 | has path => ( is => 'rw' ); | |||||||||||||||||||||||||||
| 22 | 1 | ▶ | has level => ( is => 'rw', default => sub {'debug'} ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 13 | has namespace => ( is => 'rw' ); | |||||||||||||||||||||||||||
| 24 | 13 | has _fh => ( is => 'lazy' ); | |||||||||||||||||||||||||||
| 25 | 13 | has tee_stderr => ( is => 'rw' ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 13 | has append => ( is => 'rw', default => 1 ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub _build__fh { | ||||||||||||||||||||||||||||
| 30 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build__fh: called CC=4 SCAR=20.4
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # File | ||||||||||||||||||||||||||||
| 33 | 1 | ▶ | if ( my $path = $self->path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | my $mode = $self->append ? '>>' : '>'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | croak qq{Can't open log file "$path": $!} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | unless open( my $fh, $mode, $path ); | ||||||||||||||||||||||||||||
| 38 | 0 | return $fh; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # STDERR | ||||||||||||||||||||||||||||
| 42 | 1 | $self->tee_stderr(0); | |||||||||||||||||||||||||||
| 43 | 1 | return \*STDERR; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | sub debug { shift->log( debug => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
debug: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub error { shift->log( error => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub warn { shift->log( warn => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
warn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub fatal { shift->log( fatal => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
fatal: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub fatal_confess { shift->log( fatal => @_ ) and confess(@_) } | ||||||||||||||||||||||||||
|
Subroutine
fatal_confess: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub fatal_die { shift->log( fatal => @_ ) and die(@_) } | ||||||||||||||||||||||||||
|
Subroutine
fatal_die: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub fatal_croak { shift->log( fatal => @_ ) and croak(@_) } | ||||||||||||||||||||||||||
|
Subroutine
fatal_croak: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub format { | ||||||||||||||||||||||||||||
| 56 | 1 | ▶ | my ( $self, $level, @lines ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
format: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | @lines = map { split /\n/, $_ } @lines; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | my $txt = strftime( "[%d %b %H:%M:%S]", localtime ) . " [" . uc($level) . "]"; | |||||||||||||||||||||||||||
| 61 | 1 | ▶ | $txt .= " " . $self->namespace if ( $self->namespace ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | $txt .= ' (t' . threads->tid() . ')' if ($is_thread); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | $txt .= ": "; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | $txt .= join( ( "\n" . ( " " x length($txt) ) ), @lines ); | |||||||||||||||||||||||||||
| 66 | 1 | $txt .= "\n"; | |||||||||||||||||||||||||||
| 67 | 1 | return $txt; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub info { shift->log( info => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
info: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub is_debug { shift->is_level('debug') } | ||||||||||||||||||||||||||
|
Subroutine
is_debug: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub is_error { shift->is_level('error') } | ||||||||||||||||||||||||||
|
Subroutine
is_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub is_fatal { shift->is_level('fatal') } | ||||||||||||||||||||||||||
|
Subroutine
is_fatal: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub is_info { shift->is_level('info') } | ||||||||||||||||||||||||||
|
Subroutine
is_info: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub is_warn { shift->is_level('warn') } | ||||||||||||||||||||||||||
|
Subroutine
is_warn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub is_level { | ||||||||||||||||||||||||||||
| 79 | 1 | ▶ | my ( $self, $level ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_level: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | croak "level not specified" unless ($level); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | return $LEVEL->{ lc $level } >= $LEVEL->{ $ENV{GONZALES_LOG_LEVEL} || $self->level }; | ||||||||||||||||||||||||||
|
Condition33%1/3
$ENV{'GONZALES_LOG_LEVEL'} || $self->level
MC/DC0%0/2
$ENV{'GONZALES_LOG_LEVEL'} || $self->level
$ENV{'GONZALES_LOG_LEVEL'}
$self->level
Truth table33%1/3
A || B
A= $ENV{'GONZALES_LOG_LEVEL'}B= $self->level
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub log { | ||||||||||||||||||||||||||||
| 85 | 1 | ▶ | my ( $self, $level ) = ( shift, shift ); | ||||||||||||||||||||||||||
|
Subroutine
log: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | ▶ | return unless $self->is_level($level) && ( my $handle = $self->_fh ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->is_level($level) and my $handle = $self->_fh
MC/DC0%0/2
$self->is_level($level) and my $handle = $self->_fh
$self->is_level($level)
my $handle = $self->_fh
Truth table33%1/3
A and B
A= $self->is_level($level)B= my $handle = $self->_fh
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | my $msg = $self->format( $level, @_ ); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | _print( $handle, $msg ); | |||||||||||||||||||||||||||
| 92 | 1 | ▶ | _print( \*STDERR, $msg ) if ( $self->tee_stderr ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 1 | return $self; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub _print { | ||||||||||||||||||||||||||||
| 97 | 1 | ▶ | my ( $handle, $msg ) = ( shift, shift ); | ||||||||||||||||||||||||||
|
Subroutine
_print: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | flock $handle, LOCK_EX; | |||||||||||||||||||||||||||
| 100 | 1 | ▶ | $handle->print($msg) or croak "Can't write to log: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 1 | $handle->flush; | |||||||||||||||||||||||||||
| 102 | 1 | flock $handle, LOCK_UN; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 13 | 1; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 NAME | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Bio::Gonzales::Util::Log - basic logging for Bio::Gonzales | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # logs to stderr by default | ||||||||||||||||||||||||||||
| 116 | my $l = Bio::Gonzales::Util::Log->new(); | ||||||||||||||||||||||||||||
| 117 | $l->info("started application"); | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | First of all: Shamelessly stolen from Mojo::Log, thanks to Sebastian Riedel & contributors for creating it. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 METHODS | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =over 4 | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =item B<< $log->path($file) >> | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Sets or gets the log file path. If not set, STDERR is used. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =item B<< $log->level($level) >> | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Sets or gets the threshold level for logging. Everything lower than this level will not be logged. By default C<debug>. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =item B<< $log->namespace($namespace) >> | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Sets or gets the namespace of the logger. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =item B<< $log->tee_stderr($bool) >> | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Log to file and STDERR. If no path is set, setting this option has no effect. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =item B<< $log->append($bool) >> | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | If 1, the logger appends the log output to the log file. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =item B<< $log->debug(@lines) >> | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Log debug message. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =item B<< $log->error(@lines) >> | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Log error message. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =item B<< $log->warn(@lines) >> | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Log warning message. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =item B<< $log->fatal(@lines) >> | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Log fatal message. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =item B<< $log->info(@lines) >> | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Log info message. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =item B<< $log_text = $log->format($level, @lines) >> | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Format C<@lines> and return the formatted text. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =item B<< $log->is_debug >> | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Return true if log level is debug. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =item B<< $log->is_error >> | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Return true if log level is error. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =item B<< $log->is_fatal >> | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Return true if log level is fatal. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =item B<< $log->is_info >> | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Return true if log level is info. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =item B<< $log->is_warn >> | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Return true if log level is warn. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =item B<< $log->is_level($level) >> | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Return true if log level is C<$level> | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =item B<< $log->log($level, @lines) >> | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Logs C<$lines> with C<$level> to log destination. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =back | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | L<Mojo::Log> | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | jw bargsten, C<< <jwb at cpan dot org> >> | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =cut | ||||||||||||||||||||||||||||