line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::Smart; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
73663
|
use warnings; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
95
|
|
4
|
3
|
|
|
3
|
|
73
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
126
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.009'; |
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
86
|
use 5.008; |
|
3
|
|
|
|
|
14
|
|
|
3
|
|
|
|
|
156
|
|
8
|
3
|
|
|
3
|
|
16
|
use Carp; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
331
|
|
9
|
3
|
|
|
3
|
|
2878
|
use IO::File; |
|
3
|
|
|
|
|
44009
|
|
|
3
|
|
|
|
|
438
|
|
10
|
3
|
|
|
3
|
|
27
|
use base qw(Exporter); |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
3651
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT = qw(LOG YAML DUMP CLOSE); |
13
|
|
|
|
|
|
|
my $arg_ref; # options |
14
|
|
|
|
|
|
|
my $fhs_ref; # file handles |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub import { |
18
|
4
|
|
|
4
|
|
253
|
my $package = shift; |
19
|
4
|
|
|
|
|
29
|
my ($caller_package, $caller_name, $line) = caller(0); |
20
|
4
|
50
|
|
|
|
24
|
return 1 if $caller_name =~ m/\(eval\s.*\)/xms; |
21
|
4
|
|
|
|
|
7
|
my $TRUE = 1; |
22
|
4
|
|
|
|
|
7
|
my $FALSE = 0; |
23
|
4
|
|
|
|
|
28
|
my $file = $caller_name; |
24
|
4
|
|
|
|
|
6
|
my $arg; |
25
|
4
|
|
|
|
|
20
|
$file =~ m/(.*)\/.*\z/xms; |
26
|
4
|
|
|
|
|
20
|
$arg->{-path} = $1; |
27
|
4
|
|
|
|
|
23
|
$arg->{-name} = "$caller_package.log"; |
28
|
4
|
|
|
|
|
10
|
$arg->{-timestamp} = $FALSE; |
29
|
|
|
|
|
|
|
|
30
|
4
|
|
|
|
|
8
|
my @symbols = (); |
31
|
4
|
|
|
|
|
13
|
push @_, @EXPORT; |
32
|
4
|
|
|
|
|
15
|
while (@_) { |
33
|
19
|
|
|
|
|
24
|
my $key = shift; |
34
|
19
|
100
|
|
|
|
50
|
if ($key =~ /^[-]/) { |
35
|
3
|
50
|
|
|
|
15
|
if ($key =~ /-path/) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
36
|
0
|
|
|
|
|
0
|
$arg->{$key} = shift; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
elsif ($key =~ /-name/) { |
39
|
2
|
|
|
|
|
7
|
$arg->{$key} = shift; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
elsif ($key =~ /-timestamp/) { |
42
|
1
|
|
|
|
|
4
|
$arg->{$key} = $TRUE; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
elsif ($key =~ /-append/) { |
45
|
0
|
|
|
|
|
0
|
$arg->{$key} = $TRUE; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
else { |
49
|
16
|
|
|
|
|
41
|
push @symbols, $key; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
4
|
|
|
|
|
21
|
$arg_ref->{$caller_package} = $arg; |
54
|
4
|
|
|
|
|
11
|
$fhs_ref->{$caller_package} = _open($arg); |
55
|
4
|
|
|
|
|
597
|
Exporter::export($package, $caller_package, @symbols); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _open { |
60
|
4
|
|
|
4
|
|
7
|
my $arg = shift; |
61
|
4
|
50
|
|
|
|
111
|
croak "[Log::Smart]permission denied. |
62
|
|
|
|
|
|
|
the output directory checks for write permission." |
63
|
|
|
|
|
|
|
unless -w "$arg->{-path}"; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# IO::File has insecure dependency problem. |
66
|
|
|
|
|
|
|
# Now, therefore not 'w' or '+<' but O_* mode. |
67
|
4
|
50
|
|
|
|
17
|
my $mode = $arg->{-append} ? O_APPEND | O_CREAT : O_WRONLY | O_CREAT; |
68
|
4
|
50
|
|
|
|
30
|
my $fh = IO::File->new("$arg->{-path}/$arg->{-name}", $mode) or |
69
|
|
|
|
|
|
|
croak "IO::File can't open the file : " |
70
|
|
|
|
|
|
|
. $arg->{-path} . " name : " . $arg->{-name}; |
71
|
4
|
|
|
|
|
455
|
return $fh; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub LOG { |
76
|
6
|
|
|
6
|
1
|
58
|
my $value = shift; |
77
|
6
|
|
|
|
|
54
|
my $fh = $fhs_ref->{ caller(0) }; |
78
|
6
|
|
|
|
|
11
|
my $arg = $arg_ref->{ caller(0) }; |
79
|
|
|
|
|
|
|
|
80
|
6
|
|
|
|
|
19
|
_log($fh, $arg, $value); |
81
|
6
|
|
|
|
|
183
|
$fh->flush; |
82
|
6
|
|
|
|
|
48
|
return $value; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub _log { |
86
|
7
|
|
|
7
|
|
11
|
my ($fh, $arg, $value) = @_; |
87
|
7
|
100
|
|
|
|
219
|
$value = '[' . localtime(time) . ']' . $value if $arg->{-timestamp}; |
88
|
7
|
50
|
|
|
|
95
|
print $fh "$value\n" or croak "Can't print value."; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub DUMP { |
92
|
1
|
|
|
1
|
1
|
5
|
my $fh = $fhs_ref->{ caller(0) }; |
93
|
1
|
|
|
|
|
3
|
my $arg = $arg_ref->{ caller(0) }; |
94
|
1
|
|
|
|
|
19
|
_dump($fh, $arg, @_); |
95
|
1
|
|
|
|
|
92
|
$fh->flush; |
96
|
1
|
50
|
|
|
|
5
|
return wantarray ? @_ : $_[0]; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub _dump { |
100
|
|
|
|
|
|
|
# Args must shifts because message or later is Dump args. |
101
|
1
|
|
|
1
|
|
1
|
my $fh = shift; |
102
|
1
|
|
|
|
|
2
|
my $arg = shift; |
103
|
1
|
|
|
|
|
2
|
my $message = shift; |
104
|
1
|
|
|
|
|
54
|
eval "require Data::Dumper"; |
105
|
1
|
50
|
|
|
|
5375
|
croak "Data::Dumper is not installed" if $@; |
106
|
1
|
|
|
|
|
2
|
$Data::Dumper::Sortkeys = 1; |
107
|
|
|
|
|
|
|
|
108
|
1
|
|
|
|
|
4
|
_log($fh, $arg, "[$message #DUMP]"); |
109
|
1
|
50
|
|
|
|
4
|
print $fh Data::Dumper::Dumper(@_) or croak "Can't print value."; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub YAML { |
113
|
0
|
|
|
0
|
1
|
0
|
my $message = shift; |
114
|
0
|
|
|
|
|
0
|
eval "require YAML"; |
115
|
0
|
0
|
|
|
|
0
|
croak "YAML is not installed." if $@; |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
0
|
my $fh = $fhs_ref->{ caller(0) }; |
118
|
0
|
|
|
|
|
0
|
my $arg = $arg_ref->{ caller(0) }; |
119
|
0
|
|
|
|
|
0
|
_log($fh, $arg, "[$message #YAML]"); |
120
|
0
|
0
|
|
|
|
0
|
print $fh YAML::Dump(@_) or croak "Can't print value."; |
121
|
0
|
|
|
|
|
0
|
$fh->flush; |
122
|
0
|
0
|
|
|
|
0
|
return wantarray ? @_ : $_[0]; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub CLOSE { |
126
|
1
|
|
|
1
|
|
11019
|
$fhs_ref->{ caller(0) }->close; |
127
|
1
|
|
|
|
|
27
|
delete $$fhs_ref{ caller(0) }; |
128
|
1
|
|
|
|
|
147
|
delete $$arg_ref{ caller(0) }; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 NAME |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Log::Smart - Messages for smart logging to the file |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 VERSION |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
version 0.009 |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=cut |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 SYNOPSIS |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
use Log::Smart -timestamp; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
LOG("write a message"); |
147
|
|
|
|
|
|
|
DUMP("dump the data structures", $arg); |
148
|
|
|
|
|
|
|
YAML("dump the data structures back into yaml", $arg) |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 DESCRIPTION |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
B provides logging methods that is easy to use. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
This module automatically creates and opens the file for logging. |
155
|
|
|
|
|
|
|
It is created to location of the file that used this module. |
156
|
|
|
|
|
|
|
And name of the file is the namespace + I<.log> with using this module. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
It exports a function that you can put just about anywhere |
159
|
|
|
|
|
|
|
in your Perl code to make it logging. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
To change the location or filename, you can use the options. |
162
|
|
|
|
|
|
|
Please refer to B for more information on. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
package Example; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
use Log::Smart; |
168
|
|
|
|
|
|
|
#file name "Example.log" |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
package Example; |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
use Log::Smart -name => 'mydebug.mylog'; |
174
|
|
|
|
|
|
|
#file name "mydebug.mylog" |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
B |
177
|
|
|
|
|
|
|
This module automatically determines the output location(need write permission) and the filename when you don't use some options. |
178
|
|
|
|
|
|
|
You should carefully use it, otherwise the file of same name is overwrited. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 BACKWARD INCOMPATIBILITY |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Current version of Log::Smart was once called Debug::Smart. |
183
|
|
|
|
|
|
|
When I released this module naming it to Debug::Smart was wrong. |
184
|
|
|
|
|
|
|
Debug::Smart was unmatched this module functions. |
185
|
|
|
|
|
|
|
Thanks for nadim khemir of review. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 EXPORT |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=over |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=item LOG |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
To write variable to the file. |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=item DUMP |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
To write the variable structures to the file with Data::Dumper::Dumper. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=item YAML |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
To write the variable structures to the file with YAML::Dump. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=item CLOSE |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
To close file handle if you want expressly. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=back |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head1 OPTIONS |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
use Log::Smart -path => '/path/to/'; |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
I<-path> option specify output location of the log file. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
use Log::Smart -name => 'filename'; |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
I<-filename> option specify the filename. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
use Log::Smart -timestamp; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
I<-timestamp> option add timestamp to the head of logging message. |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
use Log::Smart -append |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
I<-append> option is append mode. Writing at end-of-file. |
226
|
|
|
|
|
|
|
Default is write mode. It will be overwritten. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=head1 AUTHOR |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
Kazuhiro Shibuya, C<< >> |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head1 BUGS |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
235
|
|
|
|
|
|
|
C, or through the web interface at |
236
|
|
|
|
|
|
|
L. I will be notified, and then you'll automatically be |
237
|
|
|
|
|
|
|
notified of progress on your bug as I make changes. |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
Copyright 2007-2008 Kazuhiro Shibuya, All Rights Reserved. |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
244
|
|
|
|
|
|
|
under the same terms as Perl itself. |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=cut |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
1; # End of Log::Smart |