| 1 | package XS::Log; | ||||||||||||||||||||||||||||||||||||||
| 2 | 2 | our $VERSION = 1.18; | |||||||||||||||||||||||||||||||||||||
| 3 | 2 | our $BUILDDATE = "2026-11-23"; #Build Time: 10:27:15 | |||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | #颜色设置 | ||||||||||||||||||||||||||||||||||||||
| 8 | 2 | LOG_LEVEL_OFF => 0, # 关闭日志 | |||||||||||||||||||||||||||||||||||||
| 9 | LOG_LEVEL_FATAL => 1, # 严重错误 | ||||||||||||||||||||||||||||||||||||||
| 10 | LOG_LEVEL_ERROR => 2, # 错误信息 | ||||||||||||||||||||||||||||||||||||||
| 11 | LOG_LEVEL_WARN => 3, # 警告信息 | ||||||||||||||||||||||||||||||||||||||
| 12 | LOG_LEVEL_INFO => 4, # 一般信息 | ||||||||||||||||||||||||||||||||||||||
| 13 | LOG_LEVEL_TRACE => 5, # 最详细的日志信息 | ||||||||||||||||||||||||||||||||||||||
| 14 | LOG_LEVEL_DEBUG => 6, # 调试信息 | ||||||||||||||||||||||||||||||||||||||
| 15 | LOG_LEVEL_TEXT => 7, # 原文输出 | ||||||||||||||||||||||||||||||||||||||
| 16 | LOG_MODE_CYCLE => 0, | ||||||||||||||||||||||||||||||||||||||
| 17 | LOG_MODE_DAILY => 1, | ||||||||||||||||||||||||||||||||||||||
| 18 | LOG_MODE_HOURLY => 2, | ||||||||||||||||||||||||||||||||||||||
| 19 | LOG_TARGET_CONSOLE => 0x01, | ||||||||||||||||||||||||||||||||||||||
| 20 | LOG_TARGET_FILE => 0x02, | ||||||||||||||||||||||||||||||||||||||
| 21 | LOG_TARGET_SYSLOG => 0x04 | ||||||||||||||||||||||||||||||||||||||
| 22 | 2 | }; | |||||||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use XSLoader; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 24 | 2 | require Exporter; | |||||||||||||||||||||||||||||||||||||
| 25 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||||||||
| 28 | openLog closeLog flushLog setLogOptions setLogColor setLogMode setLogTargets setLogLevel | ||||||||||||||||||||||||||||||||||||||
| 29 | printNote printBug printInf printWarn printErr printFail printLog printRep | ||||||||||||||||||||||||||||||||||||||
| 30 | LOG_LEVEL_OFF LOG_LEVEL_FATAL LOG_LEVEL_ERROR LOG_LEVEL_WARN LOG_LEVEL_INFO LOG_LEVEL_TRACE LOG_LEVEL_DEBUG LOG_LEVEL_TEXT | ||||||||||||||||||||||||||||||||||||||
| 31 | LOG_MODE_CYCLE LOG_MODE_DAILY LOG_MODE_HOURLY | ||||||||||||||||||||||||||||||||||||||
| 32 | LOG_TARGET_CONSOLE LOG_TARGET_FILE LOG_TARGET_SYSLOG | ||||||||||||||||||||||||||||||||||||||
| 33 | Fatal Error Warn Info Debug Trace Print Colored | ||||||||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||||||||
| 35 | # 定义导出标签 :all | ||||||||||||||||||||||||||||||||||||||
| 36 | 2 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||||||||
| 37 | all => \@EXPORT_OK, | ||||||||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||||||||
| 39 | 2 | XSLoader::load('XS::Log', $VERSION); | |||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||
| 41 | #-------------------------------------------- | ||||||||||||||||||||||||||||||||||||||
| 42 | # 兼容Log2的相关函数:Fatal Error Warn Info Debug Trace Print Colored | ||||||||||||||||||||||||||||||||||||||
| 43 | #-------------------------------------------- | ||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub Fatal{my $msg = shift; my ($pkg, $file, $line) = caller;xs_log_write(LOG_LEVEL_ERROR,$file,$line,$msg);} | ||||||||||||||||||||||||||||||||||||
|
Subroutine
Fatal: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub Error{my $msg = shift; my ($pkg, $file, $line) = caller;xs_log_write(LOG_LEVEL_ERROR,$file,$line,$msg);} | ||||||||||||||||||||||||||||||||||||
|
Subroutine
Error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub Warn {my $msg = shift; my ($pkg, $file, $line) = caller;xs_log_write(LOG_LEVEL_WARN,$file,$line,$msg);} | ||||||||||||||||||||||||||||||||||||
|
Subroutine
Warn: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub Info {my $msg = shift; my ($pkg, $file, $line) = caller;xs_log_write(LOG_LEVEL_INFO,$file,$line,$msg);} | ||||||||||||||||||||||||||||||||||||
|
Subroutine
Info: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub Debug{my $msg = shift; my ($pkg, $file, $line) = caller;xs_log_write(LOG_LEVEL_DEBUG,$file,$line,$msg);} | ||||||||||||||||||||||||||||||||||||
|
Subroutine
Debug: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub Trace{my $msg = shift; my ($pkg, $file, $line) = caller;xs_log_write(LOG_LEVEL_TRACE,$file,$line,$msg);} | ||||||||||||||||||||||||||||||||||||
|
Subroutine
Trace: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub Print{my $msg = shift;xs_log_write(LOG_LEVEL_TEXT,undef,undef,$msg);} | ||||||||||||||||||||||||||||||||||||
|
Subroutine
Print: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||
| 52 | sub Colored | ||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
Colored: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 54 | 0 | my $txt = shift; | |||||||||||||||||||||||||||||||||||||
| 55 | 0 | my $color = shift; | |||||||||||||||||||||||||||||||||||||
| 56 | #\033[30m黑色字\033[0m black | ||||||||||||||||||||||||||||||||||||||
| 57 | #\033[31m红色字\033[0m red | ||||||||||||||||||||||||||||||||||||||
| 58 | #\033[32m绿色字\033[0m green | ||||||||||||||||||||||||||||||||||||||
| 59 | #\033[33m黄色字\033[0m yellow | ||||||||||||||||||||||||||||||||||||||
| 60 | #\033[34m蓝色字\033[0m blue | ||||||||||||||||||||||||||||||||||||||
| 61 | #\033[35m紫色字\033[0m magenta | ||||||||||||||||||||||||||||||||||||||
| 62 | #\033[36m天蓝字\033[0m cyan | ||||||||||||||||||||||||||||||||||||||
| 63 | #\033[37m白色字\033[0m white | ||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if(lc($color) eq "black") {$txt = "\033[30m$txt\033[0m";} | ||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||
| 65 | 0 | elsif(lc($color) eq "red") {$txt = "\033[31m$txt\033[0m";} | |||||||||||||||||||||||||||||||||||||
| 66 | 0 | elsif(lc($color) eq "green") {$txt = "\033[32m$txt\033[0m";} | |||||||||||||||||||||||||||||||||||||
| 67 | 0 | elsif(lc($color) eq "yellow") {$txt = "\033[33m$txt\033[0m";} | |||||||||||||||||||||||||||||||||||||
| 68 | 0 | elsif(lc($color) eq "blue") {$txt = "\033[34m$txt\033[0m";} | |||||||||||||||||||||||||||||||||||||
| 69 | 0 | elsif(lc($color) eq "magenta") {$txt = "\033[35m$txt\033[0m";} | |||||||||||||||||||||||||||||||||||||
| 70 | 0 | elsif(lc($color) eq "cyan") {$txt = "\033[36m$txt\033[0m";} | |||||||||||||||||||||||||||||||||||||
| 71 | 0 | elsif(lc($color) eq "white") {$txt = "\033[37m$txt\033[0m";} | |||||||||||||||||||||||||||||||||||||
| 72 | else{} | ||||||||||||||||||||||||||||||||||||||
| 73 | 0 | return $txt; | |||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||
| 75 | # 统一日志处理 | ||||||||||||||||||||||||||||||||||||||
| 76 | 5 | ▶ | sub _printLog { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_printLog: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||||||
| 77 | 5 | my ($level, $fmt, @args) = @_; | |||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | # 格式化消息 | ||||||||||||||||||||||||||||||||||||||
| 80 | 5 | ▶ | my $msg = @args ? sprintf($fmt, @args) : ($fmt // ''); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$fmt // ""
MC/DC0%0/1
$fmt // ""
$fmt
Truth table50%1/2
A
A= $fmt
| |||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||
| 82 | # 获取调用位置 | ||||||||||||||||||||||||||||||||||||||
| 83 | 5 | my ($file, $line) = ('', 0); | |||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||
| 85 | 5 | ▶ | if (get_show_file_info()) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 86 | 0 | (undef, $file, $line) = caller(1); | |||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||
| 89 | 5 | xs_log_write($level, $file, $line, $msg); | |||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||
| 92 | # 日志接口 | ||||||||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub printNote { _printLog(LOG_LEVEL_TRACE, @_); } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
printNote: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | sub printBug { _printLog(LOG_LEVEL_DEBUG, @_); } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
printBug: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | sub printInf { _printLog(LOG_LEVEL_INFO, @_); } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
printInf: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | sub printWarn { _printLog(LOG_LEVEL_WARN, @_); } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
printWarn: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | sub printErr { _printLog(LOG_LEVEL_ERROR, @_); } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
printErr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub printFail { _printLog(LOG_LEVEL_FATAL, @_); } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
printFail: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||
| 100 | # 普通文本日志,不获取调用位置 | ||||||||||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | sub printLog { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
printLog: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 102 | 1 | my ($fmt, @args) = @_; | |||||||||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | my $msg = @args ? sprintf($fmt, @args) : ($fmt // ''); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$fmt // ""
MC/DC0%0/1
$fmt // ""
$fmt
Truth table50%1/2
A
A= $fmt
| |||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | 1 | xs_log_write(LOG_LEVEL_TEXT, '', 0, $msg); | |||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub printRep { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
printRep: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my ($fmt, @args) = @_; | |||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my $msg = @args ? sprintf($fmt, @args) : ($fmt // ''); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$fmt // ""
MC/DC0%0/1
$fmt // ""
$fmt
Truth table0%0/2
A
A= $fmt
| |||||||||||||||||||||||||||||||||||||||
| 111 | 0 | xs_rep_write($msg); | |||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||
| 113 | 2 | 1; | |||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||||||||||||