line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::Lite; |
2
|
1
|
|
|
1
|
|
28790
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
49
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
61
|
|
4
|
1
|
|
|
1
|
|
893
|
use POSIX qw(strftime); |
|
1
|
|
|
|
|
11885
|
|
|
1
|
|
|
|
|
10
|
|
5
|
1
|
|
|
1
|
|
2016
|
use Fcntl qw(:flock); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
239
|
|
6
|
1
|
|
|
1
|
|
9
|
use File::Path qw(make_path); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1168
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter; |
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
our @EXPORT_OK = qw(logrotate logmode logpath logsregex log); |
11
|
|
|
|
|
|
|
our $VERSION = '0.14'; |
12
|
|
|
|
|
|
|
our $LOGPATH; |
13
|
|
|
|
|
|
|
our $LOGMODE = 'log'; # or debug|slient |
14
|
|
|
|
|
|
|
our $LOGROTATE = 'day'; # or month|year|no |
15
|
|
|
|
|
|
|
our $LOGSREGEX = "[\t\r\n]"; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub logsregex |
18
|
|
|
|
|
|
|
{ |
19
|
0
|
|
|
0
|
1
|
0
|
my $regex = shift; |
20
|
0
|
|
|
|
|
0
|
$LOGSREGEX = $regex; |
21
|
0
|
|
|
|
|
0
|
return 1; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub logrotate |
25
|
|
|
|
|
|
|
{ |
26
|
0
|
|
|
0
|
1
|
0
|
my $rotate = shift; |
27
|
0
|
0
|
0
|
|
|
0
|
if ( $rotate eq 'day' |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
28
|
|
|
|
|
|
|
or $rotate eq 'month' |
29
|
|
|
|
|
|
|
or $rotate eq 'year' |
30
|
|
|
|
|
|
|
or $rotate eq 'no') |
31
|
|
|
|
|
|
|
{ |
32
|
0
|
|
|
|
|
0
|
$LOGROTATE = $rotate; |
33
|
|
|
|
|
|
|
} |
34
|
0
|
|
|
|
|
0
|
return 1; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub logmode |
38
|
|
|
|
|
|
|
{ |
39
|
0
|
|
|
0
|
1
|
0
|
my $mode = shift; |
40
|
0
|
0
|
0
|
|
|
0
|
if ($mode eq 'debug' or $mode eq 'log') |
41
|
|
|
|
|
|
|
{ |
42
|
0
|
|
|
|
|
0
|
$LOGMODE = $mode; |
43
|
|
|
|
|
|
|
} |
44
|
0
|
|
|
|
|
0
|
return 1; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub logpath |
48
|
|
|
|
|
|
|
{ |
49
|
1
|
|
|
1
|
1
|
188
|
my $path = shift; |
50
|
1
|
0
|
33
|
|
|
12
|
if (substr($path, 0, 1) ne '/' and $path !~ /^[a-zA-Z]\:\\/ and $ENV{'PWD'}) |
|
|
|
33
|
|
|
|
|
51
|
|
|
|
|
|
|
{ |
52
|
0
|
|
|
|
|
0
|
$path = $ENV{'PWD'} . "/" . $path; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
3
|
$LOGPATH = $path; |
56
|
1
|
|
|
|
|
4
|
return 1; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub log |
60
|
|
|
|
|
|
|
{ |
61
|
1
|
50
|
|
1
|
1
|
10
|
return 0 unless $_[0]; |
62
|
1
|
|
|
|
|
3
|
my $logtype = shift; |
63
|
1
|
|
|
|
|
319
|
my $log = strftime "%Y-%m-%d %H:%M:%S", localtime; |
64
|
1
|
|
|
|
|
23
|
foreach (@_) |
65
|
|
|
|
|
|
|
{ |
66
|
2
|
|
|
|
|
4
|
my $str = $_; |
67
|
2
|
50
|
|
|
|
49
|
$str =~ s/$LOGSREGEX//g if defined $str; |
68
|
2
|
50
|
|
|
|
10
|
$log .= "\t" . $str if defined $str; |
69
|
|
|
|
|
|
|
} |
70
|
1
|
|
|
|
|
4
|
$log .= "\n"; |
71
|
|
|
|
|
|
|
|
72
|
1
|
50
|
|
|
|
6
|
if ($LOGMODE eq 'slient') |
73
|
|
|
|
|
|
|
{ |
74
|
0
|
|
|
|
|
0
|
return 1; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
1
|
50
|
|
|
|
5
|
if ($LOGMODE eq 'debug') |
78
|
|
|
|
|
|
|
{ |
79
|
0
|
|
|
|
|
0
|
print STDERR "[Log::Lite]$log"; |
80
|
0
|
|
|
|
|
0
|
return 1; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
1
|
50
|
|
|
|
7
|
my $logpath = $LOGPATH ? $LOGPATH : 'log'; |
84
|
1
|
|
|
|
|
3
|
my $date_str = ''; |
85
|
1
|
50
|
|
|
|
7
|
if ($LOGROTATE ne 'no') |
86
|
|
|
|
|
|
|
{ |
87
|
1
|
|
|
|
|
2
|
$date_str .= '_'; |
88
|
1
|
50
|
|
|
|
4
|
if ($LOGROTATE eq 'day') |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
89
|
|
|
|
|
|
|
{ |
90
|
1
|
|
|
|
|
71
|
$date_str .= strftime "%Y%m%d", localtime; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
elsif ($LOGROTATE eq 'month') |
93
|
|
|
|
|
|
|
{ |
94
|
0
|
|
|
|
|
0
|
$date_str .= strftime "%Y%m", localtime; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
elsif ($LOGROTATE eq 'year') |
97
|
|
|
|
|
|
|
{ |
98
|
0
|
|
|
|
|
0
|
$date_str .= strftime "%Y", localtime; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
1
|
|
|
|
|
7
|
my $logfile = $logpath . "/" . $logtype . $date_str . ".log"; |
102
|
1
|
50
|
33
|
|
|
20
|
if (-d $logpath or make_path($logpath, {verbose => 0, mode => 0755})) |
103
|
|
|
|
|
|
|
{ |
104
|
1
|
|
|
|
|
467
|
open my $fh, ">>", $logfile; |
105
|
1
|
|
|
|
|
12
|
flock $fh, LOCK_EX; |
106
|
1
|
|
|
|
|
21
|
print $fh $log; |
107
|
1
|
|
|
|
|
60
|
flock $fh, LOCK_UN; |
108
|
1
|
|
|
|
|
13
|
close $fh; |
109
|
1
|
|
|
|
|
12
|
return 1; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
else |
112
|
|
|
|
|
|
|
{ |
113
|
0
|
|
|
|
|
|
print STDERR "[Log::Lite]error mkdir $logpath"; |
114
|
0
|
|
|
|
|
|
return 0; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |
119
|
|
|
|
|
|
|
__END__ |