line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::LogLite; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1994
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
57
|
|
4
|
1
|
|
|
1
|
|
6
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
84
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
$VERSION = 0.82; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
814
|
|
9
|
1
|
|
|
1
|
|
2082
|
use IO::LockedFile 0.21; |
|
1
|
|
|
|
|
32122
|
|
|
1
|
|
|
|
|
6
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $TEMPLATE = '[] <> |
12
|
|
|
|
|
|
|
'; |
13
|
|
|
|
|
|
|
my $LOG_LINE_NUMBERS = 0; # by default we do not log the line numbers |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
########################################## |
16
|
|
|
|
|
|
|
# new($filepath) |
17
|
|
|
|
|
|
|
# new($filepath,$level) |
18
|
|
|
|
|
|
|
# new($filepath,$level,$default_message) |
19
|
|
|
|
|
|
|
########################################## |
20
|
|
|
|
|
|
|
# the constructor |
21
|
|
|
|
|
|
|
sub new { |
22
|
4
|
|
|
4
|
1
|
734
|
my $proto = shift; # get the class name |
23
|
4
|
|
33
|
|
|
24
|
my $class = ref($proto) || $proto; |
24
|
4
|
|
|
|
|
7
|
my $self = {}; |
25
|
|
|
|
|
|
|
# private data |
26
|
4
|
|
|
|
|
12
|
$self->{FILE_PATH} = shift; # get the file path of the config file |
27
|
4
|
|
100
|
|
|
19
|
$self->{LEVEL} = shift || 5; # the default level is 5 |
28
|
|
|
|
|
|
|
# report when: |
29
|
|
|
|
|
|
|
# 0 the application is unusable |
30
|
|
|
|
|
|
|
# 1 the application is going to be unusable |
31
|
|
|
|
|
|
|
# 2 critical conditions |
32
|
|
|
|
|
|
|
# 3 error conditions |
33
|
|
|
|
|
|
|
# 4 warning conditions |
34
|
|
|
|
|
|
|
# 5 normal but significant condition |
35
|
|
|
|
|
|
|
# 6 informational |
36
|
|
|
|
|
|
|
# 7+ debug-level messages |
37
|
4
|
|
50
|
|
|
24
|
$self->{DEFAULT_MESSAGE} = shift || ""; # the default message |
38
|
4
|
|
33
|
|
|
28
|
$self->{TEMPLATE} = shift || $TEMPLATE; # the template |
39
|
4
|
|
|
|
|
7
|
$self->{LOG_LINE_NUMBERS} = $LOG_LINE_NUMBERS; |
40
|
|
|
|
|
|
|
# we create IO::LockedFile object that can be locked later |
41
|
4
|
|
|
|
|
33
|
$self->{FH} = new IO::LockedFile({ lock => 0 }, ">>".$self->{FILE_PATH}); |
42
|
4
|
50
|
|
|
|
811
|
unless ($self->{FH}->opened) { |
43
|
0
|
|
|
|
|
0
|
croak("Log::LogLite: Cannot open the log file $self->{FILE_PATH}"); |
44
|
|
|
|
|
|
|
} |
45
|
4
|
|
|
|
|
28
|
bless ($self, $class); |
46
|
4
|
|
|
|
|
12
|
return $self; |
47
|
|
|
|
|
|
|
} # of new |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
########################## |
50
|
|
|
|
|
|
|
# write($message, $level) |
51
|
|
|
|
|
|
|
########################## |
52
|
|
|
|
|
|
|
# will log the message in the log file only if $level>=LEVEL |
53
|
|
|
|
|
|
|
sub write { |
54
|
8
|
|
|
8
|
1
|
342
|
my $self = shift; |
55
|
8
|
|
|
|
|
10
|
my $message = shift; # get the message are informational |
56
|
8
|
|
100
|
|
|
23
|
my $level = shift || "-"; |
57
|
8
|
100
|
100
|
|
|
49
|
if ($level ne "-" && $level > $self->{LEVEL}) { |
58
|
|
|
|
|
|
|
# if the level of this message is higher |
59
|
|
|
|
|
|
|
# then the deafult level - do nothing |
60
|
2
|
|
|
|
|
5
|
return; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# lock the log file before we append |
64
|
6
|
|
|
|
|
25
|
$self->{FH}->lock(); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# parse the template |
67
|
6
|
|
|
|
|
349
|
my $line = $self->{TEMPLATE}; |
68
|
6
|
|
|
|
|
28
|
$line =~ s!!date_string()!igoe; |
|
6
|
|
|
|
|
11
|
|
69
|
6
|
|
|
|
|
30
|
$line =~ s!!$level!igo; |
70
|
6
|
|
|
|
|
20
|
$line =~ s!!$self->called_by()!igoe; |
|
5
|
|
|
|
|
14
|
|
71
|
6
|
|
|
|
|
29
|
$line =~ s!!$self->{DEFAULT_MESSAGE}!igo; |
72
|
6
|
|
|
|
|
23
|
$line =~ s!!$message!igo; |
73
|
6
|
|
|
|
|
29
|
print {$self->{FH}} $line; |
|
6
|
|
|
|
|
42
|
|
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# unlock the file |
76
|
6
|
|
|
|
|
27
|
$self->{FH}->unlock(); |
77
|
|
|
|
|
|
|
} # of write |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
########################## |
80
|
|
|
|
|
|
|
# template() |
81
|
|
|
|
|
|
|
# template($template) |
82
|
|
|
|
|
|
|
########################## |
83
|
|
|
|
|
|
|
sub template { |
84
|
1
|
|
|
1
|
1
|
5
|
my $self = shift; |
85
|
1
|
50
|
|
|
|
4
|
if (@_) { $self->{TEMPLATE} = shift } |
|
1
|
|
|
|
|
3
|
|
86
|
1
|
|
|
|
|
3
|
return $self->{TEMPLATE}; |
87
|
|
|
|
|
|
|
} # of template |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
########################## |
90
|
|
|
|
|
|
|
# level() |
91
|
|
|
|
|
|
|
# level($level) |
92
|
|
|
|
|
|
|
########################## |
93
|
|
|
|
|
|
|
# an interface to LEVEL |
94
|
|
|
|
|
|
|
sub level { |
95
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
96
|
0
|
0
|
|
|
|
0
|
if (@_) { $self->{LEVEL} = shift } |
|
0
|
|
|
|
|
0
|
|
97
|
0
|
|
|
|
|
0
|
return $self->{LEVEL}; |
98
|
|
|
|
|
|
|
} # of level |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
########################### |
101
|
|
|
|
|
|
|
# default_message() |
102
|
|
|
|
|
|
|
# default_message($message) |
103
|
|
|
|
|
|
|
########################### |
104
|
|
|
|
|
|
|
# an interface to DEFAULT_MESSAGE |
105
|
|
|
|
|
|
|
sub default_message { |
106
|
1
|
|
|
1
|
1
|
6
|
my $self = shift; |
107
|
1
|
50
|
|
|
|
4
|
if (@_) { $self->{DEFAULT_MESSAGE} = shift } |
|
1
|
|
|
|
|
3
|
|
108
|
1
|
|
|
|
|
4
|
return $self->{DEFAULT_MESSAGE}; |
109
|
|
|
|
|
|
|
} # of default_message |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
########################## |
112
|
|
|
|
|
|
|
# log_line_numbers() |
113
|
|
|
|
|
|
|
# log_line_numbers($log_line_numbers) |
114
|
|
|
|
|
|
|
########################## |
115
|
|
|
|
|
|
|
# an interface to LOG_LINE_NUMBERS |
116
|
|
|
|
|
|
|
sub log_line_numbers { |
117
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
118
|
0
|
0
|
|
|
|
0
|
if (@_) { $self->{LOG_LINE_NUMBERS} = shift } |
|
0
|
|
|
|
|
0
|
|
119
|
0
|
|
|
|
|
0
|
return $self->{LOG_LINE_NUMBERS}; |
120
|
|
|
|
|
|
|
} # of log_line_numbers |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
####################### |
123
|
|
|
|
|
|
|
# date_string() |
124
|
|
|
|
|
|
|
####################### |
125
|
|
|
|
|
|
|
sub date_string { |
126
|
6
|
|
|
6
|
0
|
276
|
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); |
127
|
|
|
|
|
|
|
# note that there is no Y2K bug here. see localtime in perlfunc. |
128
|
6
|
|
|
|
|
63
|
return sprintf("%02d/%02d/%04d %02d:%02d:%02d", |
129
|
|
|
|
|
|
|
$mday, $mon + 1, $year + 1900, $hour, $min, $sec); |
130
|
|
|
|
|
|
|
} # of date_string |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
####################### |
133
|
|
|
|
|
|
|
# called_by |
134
|
|
|
|
|
|
|
####################### |
135
|
|
|
|
|
|
|
sub called_by { |
136
|
5
|
|
|
5
|
0
|
8
|
my $self = shift; |
137
|
5
|
|
|
|
|
7
|
my $depth = 2; |
138
|
5
|
|
|
|
|
6
|
my $args; |
139
|
|
|
|
|
|
|
my $pack; |
140
|
0
|
|
|
|
|
0
|
my $file; |
141
|
0
|
|
|
|
|
0
|
my $line; |
142
|
0
|
|
|
|
|
0
|
my $subr; |
143
|
0
|
|
|
|
|
0
|
my $has_args; |
144
|
0
|
|
|
|
|
0
|
my $wantarray; |
145
|
0
|
|
|
|
|
0
|
my $evaltext; |
146
|
0
|
|
|
|
|
0
|
my $is_require; |
147
|
0
|
|
|
|
|
0
|
my $hints; |
148
|
0
|
|
|
|
|
0
|
my $bitmask; |
149
|
0
|
|
|
|
|
0
|
my @subr; |
150
|
5
|
|
|
|
|
7
|
my $str = ""; |
151
|
5
|
|
|
|
|
5
|
while (1) { |
152
|
5
|
|
|
|
|
11
|
($pack, $file, $line, $subr, $has_args, $wantarray, $evaltext, |
153
|
|
|
|
|
|
|
$is_require, $hints, $bitmask) = caller($depth); |
154
|
5
|
50
|
|
|
|
13
|
unless (defined($subr)) { |
155
|
5
|
|
|
|
|
7
|
last; |
156
|
|
|
|
|
|
|
} |
157
|
0
|
|
|
|
|
0
|
$depth++; |
158
|
0
|
0
|
|
|
|
0
|
$line = ($self->{LOG_LINE_NUMBERS}) ? "$file:".$line."-->" : ""; |
159
|
0
|
|
|
|
|
0
|
push(@subr, $line.$subr); |
160
|
|
|
|
|
|
|
} |
161
|
5
|
|
|
|
|
6
|
@subr = reverse(@subr); |
162
|
5
|
|
|
|
|
11
|
foreach $subr (@subr) { |
163
|
0
|
|
|
|
|
0
|
$str .= $subr; |
164
|
0
|
|
|
|
|
0
|
$str .= " > "; |
165
|
|
|
|
|
|
|
} |
166
|
5
|
|
|
|
|
7
|
$str =~ s/ > $/: /; |
167
|
5
|
|
|
|
|
22
|
return $str; |
168
|
|
|
|
|
|
|
} # of called_by |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
1; |
171
|
|
|
|
|
|
|
__END__ |