« blib/lib/Bio/Gonzales/Feat/IO/BED.pm
↑ Summary
blib/lib/Bio/Gonzales/Seq/Filter/DuplicateSeqs.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 'croak'; | ||||||||||||||||||||||||||
|
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.058'; # 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 | 13 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build__fh: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # File | ||||||||||||||||||||||||||||
| 33 | 13 | ▶ | if ( my $path = $self->path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 12 | ▶ | my $mode = $self->append ? '>>' : '>'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 12 | ▶ | croak qq{Can't open log file "$path": $!} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | unless open( my $fh, $mode, $path ); | ||||||||||||||||||||||||||||
| 38 | 12 | 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 | |||||||||||||||||||||||||||||
| 53 | sub format { | ||||||||||||||||||||||||||||
| 54 | 13 | ▶ | my ( $self, $level, @lines ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
format: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 13 | @lines = map { split /\n/, $_ } @lines; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 13 | my $txt = strftime( "[%d %b %H:%M:%S]", localtime ) . " [" . uc($level) . "]"; | |||||||||||||||||||||||||||
| 59 | 13 | ▶ | $txt .= " " . $self->namespace if ( $self->namespace ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 13 | ▶ | $txt .= ' (t' . threads->tid() . ')' if ($is_thread); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 13 | $txt .= ": "; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 13 | $txt .= join( ( "\n" . ( " " x length($txt) ) ), @lines ); | |||||||||||||||||||||||||||
| 64 | 13 | $txt .= "\n"; | |||||||||||||||||||||||||||
| 65 | 13 | return $txt; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 12 | ▶ | sub info { shift->log( info => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
info: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub is_debug { shift->is_level('debug') } | ||||||||||||||||||||||||||
|
Subroutine
is_debug: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub is_error { shift->is_level('error') } | ||||||||||||||||||||||||||
|
Subroutine
is_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub is_fatal { shift->is_level('fatal') } | ||||||||||||||||||||||||||
|
Subroutine
is_fatal: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub is_info { shift->is_level('info') } | ||||||||||||||||||||||||||
|
Subroutine
is_info: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub is_warn { shift->is_level('warn') } | ||||||||||||||||||||||||||
|
Subroutine
is_warn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub is_level { | ||||||||||||||||||||||||||||
| 77 | 13 | ▶ | my ( $self, $level ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_level: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 13 | ▶ | croak "level not specified" unless($level); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 13 | ▶ | 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
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub log { | ||||||||||||||||||||||||||||
| 83 | 13 | ▶ | my ( $self, $level ) = ( shift, shift ); | ||||||||||||||||||||||||||
|
Subroutine
log: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 13 | ▶ | 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
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 13 | my $msg = $self->format( $level, @_ ); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 13 | _print( $handle, $msg ); | |||||||||||||||||||||||||||
| 90 | 13 | ▶ | _print( \*STDERR, $msg ) if ( $self->tee_stderr ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub _print { | ||||||||||||||||||||||||||||
| 94 | 13 | ▶ | my ( $handle, $msg ) = ( shift, shift ); | ||||||||||||||||||||||||||
|
Subroutine
_print: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 13 | flock $handle, LOCK_EX; | |||||||||||||||||||||||||||
| 97 | 13 | ▶ | $handle->print($msg) or croak "Can't write to log: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 13 | $handle->flush; | |||||||||||||||||||||||||||
| 99 | 13 | flock $handle, LOCK_UN; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 13 | 1; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | __END__ | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 NAME | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Bio::Gonzales::Util::Log - basic logging for Bio::Gonzales | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # logs to stderr by default | ||||||||||||||||||||||||||||
| 113 | my $l = Bio::Gonzales::Util::Log->new(); | ||||||||||||||||||||||||||||
| 114 | $l->info("started application"); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | First of all: Shamelessly stolen from Mojo::Log, thanks to Sebastian Riedel & contributors for creating it. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 METHODS | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =over 4 | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =item B<< $log->path($file) >> | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Sets or gets the log file path. If not set, STDERR is used. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =item B<< $log->level($level) >> | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Sets or gets the threshold level for logging. Everything lower than this level will not be logged. By default C<debug>. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =item B<< $log->namespace($namespace) >> | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Sets or gets the namespace of the logger. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =item B<< $log->tee_stderr($bool) >> | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Log to file and STDERR. If no path is set, setting this option has no effect. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =item B<< $log->append($bool) >> | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | If 1, the logger appends the log output to the log file. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =item B<< $log->debug(@lines) >> | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Log debug message. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item B<< $log->error(@lines) >> | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Log error message. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =item B<< $log->warn(@lines) >> | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Log warning message. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =item B<< $log->fatal(@lines) >> | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Log fatal message. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =item B<< $log->info(@lines) >> | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Log info message. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item B<< $log_text = $log->format($level, @lines) >> | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Format C<@lines> and return the formatted text. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =item B<< $log->is_debug >> | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Return true if log level is debug. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =item B<< $log->is_error >> | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Return true if log level is error. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =item B<< $log->is_fatal >> | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Return true if log level is fatal. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =item B<< $log->is_info >> | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Return true if log level is info. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =item B<< $log->is_warn >> | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Return true if log level is warn. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =item B<< $log->is_level($level) >> | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Return true if log level is C<$level> | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =item B<< $log->log($level, @lines) >> | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Logs C<$lines> with C<$level> to log destination. | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =back | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | L<Mojo::Log> | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | jw bargsten, C<< <jwb at cpan dot org> >> | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =cut | ||||||||||||||||||||||||||||