| 1 | package oEdtk::logger; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | BEGIN { | ||
| 6 | use Exporter (); | ||
| 7 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $LOGGERLEVEL $WARN2LOGGER) | ||
| 8 | ; #backward compatibility for Perls under 5.6 | ||
| 9 | |||
| 10 | $VERSION = 2.1126; | ||
| 11 | @ISA = qw(Exporter); | ||
| 12 | @EXPORT = qw(logger); | ||
| 13 | @EXPORT_OK = qw($LOGGERLEVEL $WARN2LOGGER); | ||
| 14 | |||
| 15 | use warnings; | ||
| 16 | use Data::Dumper qw(Dumper); | ||
| 17 | use POSIX qw(strftime); | ||
| 18 | } | ||
| 19 | |||
| 20 | $LOGGERLEVEL = 4; # default log level | ||
| 21 | $WARN2LOGGER = 0; # by default do not turn warn messsages to logger | ||
| 22 | |||
| 23 | my %hLEVEL = ( U => 8, D => 7, I => 6, P => 5, W => 4, A => 3, C => 0, E => -1 ); | ||
| 24 | 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 | my $FD; | ||
| 39 | |||
| 40 | sub logger { | ||
| 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 | my ( $messLevel, $logMess, $MODIFY ) = @_; | ||
| 56 | my $fileHandle = 0; | ||
| 57 | |||
| 58 | #my $FD=$!; | ||
| 59 | #$FD =~s/(Bad)\s(file\sdescriptor)/$1 or No $2/io; | ||
| 60 | $MODIFY //= 0; # default value, from 5.10 //= replaces ||= | ||
| 61 | $logMess //= ""; | ||
| 62 | chomp($logMess); | ||
| 63 | 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 | if ( $MODIFY =~ m/^\-{0,1}[1-4]$/ ) { | ||
| 72 | $LOGGERLEVEL += $MODIFY; | ||
| 73 | print STDERR | ||
| 74 | " \>$now|$hLABEL{6}|PID-$$| LOGGERLEVEL set to $hLABEL{$LOGGERLEVEL}|FH-$fileHandle \n"; | ||
| 75 | |||
| 76 | } | ||
| 77 | elsif ( $MODIFY =~ m/^([UDIPW])/ ) { | ||
| 78 | $LOGGERLEVEL = $hLEVEL{$1}; | ||
| 79 | 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 | print STDERR | ||
| 86 | " \>$now|$hLABEL{6}|PID-$$| logger - unknown modificator ($MODIFY)|FH-$fileHandle \n"; | ||
| 87 | 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 | $fileHandle += $. if $.; | ||
| 92 | my $loggerMessage = " \>$now|$hLABEL{$messLevel}|PID-$$| $logMess|FH-$fileHandle"; | ||
| 93 | $loggerMessage .= "|PRG $0" if ( $messLevel <= 5 ); | ||
| 94 | $loggerMessage =~ s/\s+/ /g; | ||
| 95 | |||
| 96 | if ( $messLevel <= 4 ) { | ||
| 97 | warn "$loggerMessage\n"; | ||
| 98 | } | ||
| 99 | elsif ( $messLevel <= $LOGGERLEVEL ) { | ||
| 100 | print STDERR "$loggerMessage\n"; | ||
| 101 | } | ||
| 102 | |||
| 103 | return $loggerMessage, "\n"; | ||
| 104 | } | ||
| 105 | |||
| 106 | END { } | ||
| 107 | 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 |