| 1 | package oEdtk::logger; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 41 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | 41 | ▶ | BEGIN { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 41 | ▶ | use Exporter (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 41 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $LOGGERLEVEL $WARN2LOGGER) | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 41 | ; #backward compatibility for Perls under 5.6 | |||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 41 | $VERSION = 2.1131; | |||||||||||||||||
| 11 | 41 | @ISA = qw(Exporter); | |||||||||||||||||
| 12 | 41 | @EXPORT = qw(logger); | |||||||||||||||||
| 13 | 41 | @EXPORT_OK = qw($LOGGERLEVEL $WARN2LOGGER); | |||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | 41 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 16 | 41 | ▶ | use Data::Dumper qw(Dumper); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 17 | 41 | ▶ | use POSIX qw(strftime); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 18 | } | ||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | 41 | $LOGGERLEVEL = 4; # default log level | |||||||||||||||||
| 21 | 41 | $WARN2LOGGER = 0; # by default do not turn warn messsages to logger | |||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 41 | my %hLEVEL = ( U => 8, D => 7, I => 6, P => 5, W => 4, A => 3, C => 0, E => -1 ); | |||||||||||||||||
| 24 | 41 | my %hLABEL = ( | |||||||||||||||||
| 25 | 8 => '8-USER-', | ||||||||||||||||||
| 26 | 7 => '7-DEBUG', | ||||||||||||||||||
| 27 | 6 => '6-INFO-', | ||||||||||||||||||
| 28 | 5 => '5-PARSE', | ||||||||||||||||||
| 29 | 4 => '4-WARN-', | ||||||||||||||||||
| 30 | 3 => '3-ALERT', | ||||||||||||||||||
| 31 | 2 => '2-ALERT', | ||||||||||||||||||
| 32 | 1 => '1-ALERT', | ||||||||||||||||||
| 33 | 0 => '0-CRITC', | ||||||||||||||||||
| 34 | -1 => '-1 ERROR' | ||||||||||||||||||
| 35 | ); | ||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | # Legacy error-number field, interpolated in the diagnostic message below. | ||||||||||||||||||
| 38 | 41 | my $FD; | |||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 410 | ▶ | sub logger { | ||||||||||||||||
|
Subroutine
logger: called CC=10 SCAR=26.2
Pod
documented
| |||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | # CALL: &logger($messLevel, $logMess[, $MODIFY]); | ||||||||||||||||||
| 43 | # CALL: &logger($messLevel, $logMess[, 2]); # increase LOGGERLEVEL | ||||||||||||||||||
| 44 | # CALL: &logger($messLevel, $logMess[, Warn]); # set LOGGERLEVEL to Warn | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | # to log blocking errors while using the die method: | ||||||||||||||||||
| 47 | # open (IN, "<$nomfichier") or die &logger($NOK, "unable to open <$nomfichier>"); | ||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | # LOGGERLEVEL defines the log sensitivity (note: the MODIFY option can change it) | ||||||||||||||||||
| 50 | # 8-> Debug (all), 7-> Information, 6-> Context, 5-> Follow, 4-> Warning, \$NOK-> Error | ||||||||||||||||||
| 51 | # $. is the current line number on the current filehandle | ||||||||||||||||||
| 52 | # $! current errno value | ||||||||||||||||||
| 53 | # $0 perl script name | ||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | 410 | my ( $messLevel, $logMess, $MODIFY ) = @_; | |||||||||||||||||
| 56 | 410 | my $fileHandle = 0; | |||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | #my $FD=$!; | ||||||||||||||||||
| 59 | #$FD =~s/(Bad)\s(file\sdescriptor)/$1 or No $2/io; | ||||||||||||||||||
| 60 | 410 | ▶ | $MODIFY //= 0; # default value, from 5.10 //= replaces ||= | ||||||||||||||||
|
Condition50%1/2
$MODIFY //= 0
MC/DC0%0/1
$MODIFY //= 0
$MODIFY
Truth table50%1/2
A
A= $MODIFY
| |||||||||||||||||||
| 61 | 410 | ▶ | $logMess //= ""; | ||||||||||||||||
|
Condition50%1/2
$logMess //= ""
MC/DC0%0/1
$logMess //= ""
$logMess
Truth table50%1/2
A
A= $logMess
| |||||||||||||||||||
| 62 | 410 | chomp($logMess); | |||||||||||||||||
| 63 | 410 | my $now = strftime "%Y-%m-%d %H:%M:%S", localtime; # %j day of year? | |||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | #warn "LOGGER - MODIFY $MODIFY - @{[%hLEVEL]} => Dump : ". Dumper \%hLEVEL; | ||||||||||||||||||
| 66 | # PID : | ||||||||||||||||||
| 67 | # cat /proc/sys/kernel/pid_max | ||||||||||||||||||
| 68 | # The default value for this file, 32768, results in the same range of PIDs as on earlier kernels. On 32-bit platforms, 32768 is the maximum value for pid_max. On 64-bit systems, pid_max can be set to any value up to 2^22 (PID_MAX_LIMIT, approximately 4 million) | ||||||||||||||||||
| 69 | # alias sys_guid='sudo /sbin/blkid | grep "$(df -h / | sed -n 2p | cut -d" " -f1):" | grep -o "UUID=\"[^\"]*\" " | sed "s/UUID=\"//;s/\"//"' | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | 410 | ▶ | if ( $MODIFY =~ m/^\-{0,1}[1-4]$/ ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 72 | 0 | $LOGGERLEVEL += $MODIFY; | |||||||||||||||||
| 73 | 0 | print STDERR | |||||||||||||||||
| 74 | " \>$now|$hLABEL{6}|PID-$$| LOGGERLEVEL set to $hLABEL{$LOGGERLEVEL}|FH-$fileHandle \n"; | ||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | elsif ( $MODIFY =~ m/^([UDIPW])/ ) { | ||||||||||||||||||
| 78 | 0 | $LOGGERLEVEL = $hLEVEL{$1}; | |||||||||||||||||
| 79 | 0 | print STDERR | |||||||||||||||||
| 80 | " \>$now|$hLABEL{6}|PID-$$| LOGGERLEVEL set to $hLABEL{$LOGGERLEVEL} ($MODIFY)|FH-$fileHandle \n"; | ||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | # } elsif ($MODIFY !~m/\d/ && $MODIFY !~m/^([UDIPW])/) { | ||||||||||||||||||
| 83 | } | ||||||||||||||||||
| 84 | elsif ( $MODIFY !~ m/^([0UDIPW])/ ) { | ||||||||||||||||||
| 85 | 0 | print STDERR | |||||||||||||||||
| 86 | " \>$now|$hLABEL{6}|PID-$$| logger - unknown modificator ($MODIFY)|FH-$fileHandle \n"; | ||||||||||||||||||
| 87 | 0 | print STDERR | |||||||||||||||||
| 88 | " \>$now|$hLABEL{6}|PID-$$| logger - use : 8 => '8-USER-', 7 => '7-DEBUG', 6 => '6-INFO-' , 5 => '5-PARSE' , 4 => '4-WARN-' , 3/1 => '3-ALERT', 0 => '0-CRITC' , -1 => '-1 ERROR' |$FD|FH-$fileHandle \n"; | ||||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | 410 | ▶ | $fileHandle += $. if $.; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 92 | 410 | my $loggerMessage = " \>$now|$hLABEL{$messLevel}|PID-$$| $logMess|FH-$fileHandle"; | |||||||||||||||||
| 93 | 410 | ▶ | $loggerMessage .= "|PRG $0" if ( $messLevel <= 5 ); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 94 | 410 | $loggerMessage =~ s/\s+/ /g; | |||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | 410 | ▶ | if ( $messLevel <= 4 ) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 97 | 26 | warn "$loggerMessage\n"; | |||||||||||||||||
| 98 | } | ||||||||||||||||||
| 99 | elsif ( $messLevel <= $LOGGERLEVEL ) { | ||||||||||||||||||
| 100 | 0 | print STDERR "$loggerMessage\n"; | |||||||||||||||||
| 101 | } | ||||||||||||||||||
| 102 | |||||||||||||||||||
| 103 | 410 | return $loggerMessage, "\n"; | |||||||||||||||||
| 104 | } | ||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | 40 | ▶ | END { } | ||||||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 107 | 41 | return 1; | |||||||||||||||||
| 108 | |||||||||||||||||||
| 109 | =head1 NAME | ||||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | oEdtk::logger - the oEdtk diagnostic logger | ||||||||||||||||||
| 112 | |||||||||||||||||||
| 113 | =head1 SYNOPSIS | ||||||||||||||||||
| 114 | |||||||||||||||||||
| 115 | use oEdtk::logger; | ||||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | logger(2, 'a routine message'); | ||||||||||||||||||
| 118 | logger(-1, 'fatal: cannot open the config file'); # for die() | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | =head1 DESCRIPTION | ||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | The single live logger of the toolkit. It writes a formatted diagnostic line | ||||||||||||||||||
| 123 | (timestamp, level label, PID, message, file handle) to STDERR and returns the | ||||||||||||||||||
| 124 | line so a caller can reuse it in a C<die>. The contract -- which logger is | ||||||||||||||||||
| 125 | live, the event-to-level table, and the fd2 routing -- is recorded in | ||||||||||||||||||
| 126 | F<docs/integration/LOGGER.md> and pinned by F<t/int/67_logger_normalization.t>. | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | Do not change the exports: the public API is frozen against C<snap/M.json> by | ||||||||||||||||||
| 129 | F<t/int/01_api_M.t>. | ||||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | =head1 FUNCTIONS | ||||||||||||||||||
| 132 | |||||||||||||||||||
| 133 | =head2 logger | ||||||||||||||||||
| 134 | |||||||||||||||||||
| 135 | my ($line, $eol) = logger(2, $message, $modify); | ||||||||||||||||||
| 136 | |||||||||||||||||||
| 137 | Emit a diagnostic line at the given level. The first argument is the level, a | ||||||||||||||||||
| 138 | number from C<-1> (fatal) to C<8> (user); levels up to C<4> always emit | ||||||||||||||||||
| 139 | (through C<warn>), while higher levels emit only up to the current | ||||||||||||||||||
| 140 | C<$LOGGERLEVEL>. An optional third argument adjusts C<$LOGGERLEVEL>: an integer | ||||||||||||||||||
| 141 | delta in C<1..4>, or one of the letters C<U> C<D> C<I> C<P> C<W>. The message is | ||||||||||||||||||
| 142 | collapsed to single spaces and its file handle is appended. The formatted line | ||||||||||||||||||
| 143 | and a newline are returned. | ||||||||||||||||||
| 144 | |||||||||||||||||||
| 145 | =head2 $LOGGERLEVEL | ||||||||||||||||||
| 146 | |||||||||||||||||||
| 147 | our $LOGGERLEVEL; | ||||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | The emission ceiling, default C<4>. Messages above it are suppressed unless the | ||||||||||||||||||
| 150 | level is raised. | ||||||||||||||||||
| 151 | |||||||||||||||||||
| 152 | =head2 $WARN2LOGGER | ||||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | our $WARN2LOGGER; | ||||||||||||||||||
| 155 | |||||||||||||||||||
| 156 | Flag reserved to route C<warn> messages through the logger; off by default. | ||||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | =head1 AUTHOR | ||||||||||||||||||
| 159 | |||||||||||||||||||
| 160 | The oEdtk team. | ||||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | =cut | ||||||||||||||||||