line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
################################################################################ |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# $Id: Logs.pm 211 2009-05-25 06:05:50Z aijaz $ |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
################################################################################ |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
TaskForest::Logs - Functions related to logging |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 SYNOPSIS |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use TaskForest::Logs qw/$log/; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
&TaskForest::Logs::init($banner); # print $banner and initialize the logger |
16
|
|
|
|
|
|
|
# this will also tie stdout to $log->info() |
17
|
|
|
|
|
|
|
# and stderr to $log->error() |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# $log is a logger. See Log::Log4perl |
20
|
|
|
|
|
|
|
$log->debug("Debug message"); |
21
|
|
|
|
|
|
|
$log->info("Info message"); |
22
|
|
|
|
|
|
|
$log->warn("Warn message"); |
23
|
|
|
|
|
|
|
$log->error("Error message"); |
24
|
|
|
|
|
|
|
$log->fatal("Fatal message"); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
&TaskForest::Logs::resetLogs(); |
27
|
|
|
|
|
|
|
# This will delete the error file if it is empty, and also untie |
28
|
|
|
|
|
|
|
# STDOUT and STDERR |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=cut |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
package TaskForest::Logs; |
34
|
|
|
|
|
|
|
|
35
|
87
|
|
|
87
|
|
584
|
use strict; |
|
87
|
|
|
|
|
199
|
|
|
87
|
|
|
|
|
4513
|
|
36
|
87
|
|
|
87
|
|
506
|
use warnings; |
|
87
|
|
|
|
|
3265
|
|
|
87
|
|
|
|
|
4229
|
|
37
|
87
|
|
|
87
|
|
824
|
use Exporter; |
|
87
|
|
|
|
|
161
|
|
|
87
|
|
|
|
|
4629
|
|
38
|
87
|
|
|
87
|
|
508
|
use Data::Dumper; |
|
87
|
|
|
|
|
155
|
|
|
87
|
|
|
|
|
9162
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
41
|
|
|
|
|
|
|
our @EXPORT_OK = qw/$log/; |
42
|
|
|
|
|
|
|
|
43
|
87
|
|
|
87
|
|
557
|
use Log::Log4perl qw(get_logger :levels :nowarn); |
|
87
|
|
|
|
|
129
|
|
|
87
|
|
|
|
|
1153
|
|
44
|
87
|
|
|
87
|
|
11456
|
use Log::Log4perl::Layout; |
|
87
|
|
|
|
|
215
|
|
|
87
|
|
|
|
|
1893
|
|
45
|
87
|
|
|
87
|
|
455
|
use Log::Log4perl::Level; |
|
87
|
|
|
|
|
154
|
|
|
87
|
|
|
|
|
945
|
|
46
|
|
|
|
|
|
|
|
47
|
87
|
|
|
87
|
|
56779
|
use TaskForest::OutLogger; |
|
87
|
|
|
|
|
186
|
|
|
87
|
|
|
|
|
2448
|
|
48
|
87
|
|
|
87
|
|
47077
|
use TaskForest::ErrLogger; |
|
87
|
|
|
|
|
203
|
|
|
87
|
|
|
|
|
4724
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
our $log; |
51
|
|
|
|
|
|
|
my $iappender; |
52
|
|
|
|
|
|
|
my $eappender; |
53
|
|
|
|
|
|
|
my $iobj; |
54
|
|
|
|
|
|
|
my $eobj; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
BEGIN { |
57
|
87
|
|
|
87
|
|
487
|
use vars qw($VERSION); |
|
87
|
|
|
|
|
181
|
|
|
87
|
|
|
|
|
3583
|
|
58
|
87
|
|
|
87
|
|
63276
|
$VERSION = '1.30'; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
END { |
63
|
36
|
|
|
36
|
|
1448
|
cleanup(); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub init { |
67
|
88
|
|
|
88
|
0
|
312
|
my $banner = shift; |
68
|
|
|
|
|
|
|
|
69
|
88
|
|
|
|
|
407
|
my $options = &TaskForest::Options::getOptions(); |
70
|
88
|
|
|
|
|
1123
|
my $log_dir = &TaskForest::LogDir::getLogDir($options->{log_dir}); |
71
|
|
|
|
|
|
|
|
72
|
88
|
|
|
|
|
1112
|
my $log_file = "$log_dir/$options->{log_file}"; |
73
|
88
|
|
|
|
|
331
|
my $err_file = "$log_dir/$options->{err_file}"; |
74
|
|
|
|
|
|
|
|
75
|
88
|
|
|
|
|
273
|
my $files = "$log_file\n$err_file"; |
76
|
|
|
|
|
|
|
|
77
|
88
|
|
|
|
|
1052
|
my $levels = { debug => $DEBUG, info => $INFO, warn => $WARN, error => $ERROR, fatal => $FATAL }; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Define a category logger |
80
|
88
|
|
|
|
|
653
|
$log = get_logger("ENoor"); |
81
|
88
|
|
|
|
|
25627
|
$log->level($levels->{$options->{log_threshold}}); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Define a layout |
84
|
88
|
|
|
|
|
78681
|
my $layout = Log::Log4perl::Layout::PatternLayout::Multiline->new("%d %6p %4L:%-32F{1} - %m%n"); |
85
|
|
|
|
|
|
|
|
86
|
88
|
100
|
|
|
|
65607
|
if (!$options->{log}) { |
87
|
18
|
|
|
|
|
1866
|
return; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Define 2 file appenders |
91
|
70
|
100
|
66
|
|
|
723
|
if ($iappender and $eappender) { |
92
|
26
|
|
|
|
|
861
|
$iappender->file_switch($log_file); |
93
|
26
|
|
|
|
|
14119
|
$eappender->file_switch($err_file); |
94
|
26
|
|
|
|
|
4482
|
$iappender->threshold($levels->{$options->{log_threshold}}); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
else { |
97
|
44
|
|
|
|
|
15655
|
print "$files\n"; |
98
|
44
|
|
|
|
|
680
|
$iappender = Log::Log4perl::Appender->new( "Log::Log4perl::Appender::File", |
99
|
|
|
|
|
|
|
filename => $log_file, |
100
|
|
|
|
|
|
|
mode => 'append'); |
101
|
|
|
|
|
|
|
|
102
|
44
|
|
|
|
|
267350
|
$eappender = Log::Log4perl::Appender->new( "Log::Log4perl::Appender::File", |
103
|
|
|
|
|
|
|
filename => $err_file, |
104
|
|
|
|
|
|
|
mode => 'append'); |
105
|
|
|
|
|
|
|
|
106
|
44
|
|
|
|
|
9597
|
$iappender->threshold($levels->{$options->{log_threshold}}); |
107
|
44
|
|
|
|
|
920
|
$eappender->threshold($WARN); |
108
|
|
|
|
|
|
|
|
109
|
44
|
|
|
|
|
615
|
$iappender->layout($layout); |
110
|
44
|
|
|
|
|
442
|
$eappender->layout($layout); |
111
|
|
|
|
|
|
|
|
112
|
44
|
|
|
|
|
525
|
$log->add_appender($iappender); |
113
|
44
|
|
|
|
|
31805
|
$log->add_appender($eappender); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
70
|
|
|
|
|
133000
|
$iobj = tie (*STDOUT, 'TaskForest::OutLogger'); |
118
|
70
|
|
|
|
|
1006
|
$eobj = tie (*STDERR, 'TaskForest::ErrLogger'); |
119
|
|
|
|
|
|
|
|
120
|
70
|
50
|
|
|
|
588
|
if ($banner) { |
121
|
70
|
|
|
|
|
541
|
print "********************************************************************************\n$banner\n"; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub resetLogs { |
127
|
36
|
|
|
36
|
0
|
399
|
my $options = &TaskForest::Options::getOptions(); |
128
|
|
|
|
|
|
|
|
129
|
36
|
|
|
|
|
261
|
my $err_file = "$options->{log_dir}/$options->{err_file}"; |
130
|
|
|
|
|
|
|
|
131
|
36
|
50
|
|
|
|
1371
|
unless (-s $err_file) { |
132
|
36
|
100
|
|
|
|
490
|
if ($log) { |
133
|
11
|
|
|
|
|
184
|
$log->info("Deleting error log because it is empty\n"); |
134
|
|
|
|
|
|
|
} |
135
|
36
|
|
|
|
|
7088
|
unlink $err_file; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
else { |
138
|
|
|
|
|
|
|
# need to send email |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
36
|
100
|
|
|
|
391
|
if ($iobj) { |
142
|
8
|
|
|
|
|
41
|
undef $iobj; |
143
|
8
|
|
|
|
|
271
|
untie(*STDOUT); |
144
|
|
|
|
|
|
|
} |
145
|
36
|
100
|
|
|
|
476
|
if ($eobj) { |
146
|
8
|
|
|
|
|
34
|
undef $eobj; |
147
|
8
|
|
|
|
|
328
|
untie(*STDERR); |
148
|
|
|
|
|
|
|
} |
149
|
36
|
|
|
|
|
427
|
undef $log; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub cleanup { |
153
|
36
|
|
|
36
|
0
|
440
|
my $exit_code = $?; |
154
|
|
|
|
|
|
|
|
155
|
36
|
|
|
|
|
178
|
my $message = "Exiting $exit_code"; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
#print "$message\n"; |
158
|
|
|
|
|
|
|
|
159
|
36
|
|
|
|
|
498
|
resetLogs(); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
1; |