line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::Lite; |
2
|
1
|
|
|
1
|
|
35097
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
37
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
953
|
use POSIX qw(strftime); |
|
1
|
|
|
|
|
7704
|
|
|
1
|
|
|
|
|
7
|
|
5
|
1
|
|
|
1
|
|
1180
|
use Fcntl qw(:flock); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
174
|
|
6
|
1
|
|
|
1
|
|
5
|
use File::Path qw(make_path); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1013
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter; |
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
our @EXPORT_OK = qw(logrotate logmode logpath logsregex log); |
11
|
|
|
|
|
|
|
our $VERSION = '0.13'; |
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
|
43
|
my $path = shift; |
50
|
1
|
50
|
|
|
|
5
|
if (substr($path, 0, 1) ne '/') |
51
|
|
|
|
|
|
|
{ |
52
|
0
|
|
|
|
|
0
|
$path = $ENV{'PWD'} . "/" . $path; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
3
|
$LOGPATH = $path; |
56
|
1
|
|
|
|
|
3
|
return 1; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub log |
60
|
|
|
|
|
|
|
{ |
61
|
1
|
50
|
|
1
|
1
|
7
|
return 0 unless $_[0]; |
62
|
1
|
|
|
|
|
1
|
my $logtype = shift; |
63
|
1
|
|
|
|
|
217
|
my $log = strftime "%Y-%m-%d %H:%M:%S", localtime; |
64
|
1
|
|
|
|
|
4
|
foreach (@_) |
65
|
|
|
|
|
|
|
{ |
66
|
2
|
|
|
|
|
3
|
my $str = $_; |
67
|
2
|
50
|
|
|
|
21
|
$str =~ s/$LOGSREGEX//g if defined $str; |
68
|
2
|
50
|
|
|
|
9
|
$log .= "\t" . $str if defined $str; |
69
|
|
|
|
|
|
|
} |
70
|
1
|
|
|
|
|
2
|
$log .= "\n"; |
71
|
|
|
|
|
|
|
|
72
|
1
|
50
|
|
|
|
10
|
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
|
|
|
|
5
|
my $logpath = $LOGPATH ? $LOGPATH : 'log'; |
84
|
1
|
|
|
|
|
2
|
my $date_str = ''; |
85
|
1
|
50
|
|
|
|
3
|
if ($LOGROTATE ne 'no') |
86
|
|
|
|
|
|
|
{ |
87
|
1
|
|
|
|
|
2
|
$date_str .= '_'; |
88
|
1
|
50
|
|
|
|
4
|
if ($LOGROTATE eq 'day') |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
89
|
|
|
|
|
|
|
{ |
90
|
1
|
|
|
|
|
45
|
$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
|
|
|
|
|
4
|
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
|
|
|
|
|
137
|
open my $fh, ">>", $logfile; |
105
|
1
|
|
|
|
|
8
|
flock $fh, LOCK_EX; |
106
|
1
|
|
|
|
|
14
|
print $fh $log; |
107
|
1
|
|
|
|
|
35
|
flock $fh, LOCK_UN; |
108
|
1
|
|
|
|
|
12
|
close $fh; |
109
|
1
|
|
|
|
|
5
|
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__ |