line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HPC::Runner::Command::Utils::Log; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1584
|
use Log::Log4perl qw(:easy); |
|
1
|
|
|
|
|
40990
|
|
|
1
|
|
|
|
|
8
|
|
4
|
1
|
|
|
1
|
|
1243
|
use Data::Dumper; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
99
|
|
5
|
1
|
|
|
1
|
|
10
|
use IPC::Open3; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
65
|
|
6
|
1
|
|
|
1
|
|
10
|
use IO::Select; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
58
|
|
7
|
1
|
|
|
1
|
|
8
|
use Symbol; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
80
|
|
8
|
1
|
|
|
1
|
|
898
|
use DateTime; |
|
1
|
|
|
|
|
478278
|
|
|
1
|
|
|
|
|
44
|
|
9
|
1
|
|
|
1
|
|
490
|
use DateTime::Format::Duration; |
|
1
|
|
|
|
|
5073
|
|
|
1
|
|
|
|
|
64
|
|
10
|
1
|
|
|
1
|
|
8
|
use Cwd; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
60
|
|
11
|
1
|
|
|
1
|
|
5
|
use File::Path qw(make_path); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
12
|
1
|
|
|
1
|
|
5
|
use File::Spec; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
17
|
|
13
|
1
|
|
|
1
|
|
4
|
use File::Slurp; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
65
|
|
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
6
|
use MooseX::App::Role; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
11
|
|
16
|
1
|
|
|
1
|
|
9249
|
use MooseX::Types::Path::Tiny qw/Path Paths AbsPath AbsFile/; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
12
|
|
17
|
1
|
|
|
1
|
|
3992
|
use Path::Tiny; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1146
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 HPC::Runner::Command::Utils::Log |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Class for all logging attributes |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head2 Command Line Options |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head3 logdir |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Pattern to use to write out logs directory. Defaults to outdir/prunner_current_date_time/log1 .. log2 .. log3. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=cut |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
option 'logdir' => ( |
33
|
|
|
|
|
|
|
is => 'rw', |
34
|
|
|
|
|
|
|
isa => AbsPath, |
35
|
|
|
|
|
|
|
coerce => 1, |
36
|
|
|
|
|
|
|
lazy => 1, |
37
|
|
|
|
|
|
|
required => 1, |
38
|
|
|
|
|
|
|
default => sub { |
39
|
|
|
|
|
|
|
my $self = shift; |
40
|
|
|
|
|
|
|
$self->set_logdir; |
41
|
|
|
|
|
|
|
}, |
42
|
|
|
|
|
|
|
documentation => 'Directory where logfiles are written.' |
43
|
|
|
|
|
|
|
. ' Defaults to current_working_directory/prunner_current_date_time/log1 .. log2 .. log3', |
44
|
|
|
|
|
|
|
trigger => sub { |
45
|
|
|
|
|
|
|
my $self = shift; |
46
|
|
|
|
|
|
|
my $val = shift; |
47
|
|
|
|
|
|
|
$self->_make_the_dirs($val); |
48
|
|
|
|
|
|
|
}, |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head3 show_process_id |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Show process_id in each log file. This is useful for aggregating logs |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
option 'show_processid' => ( |
58
|
|
|
|
|
|
|
is => 'rw', |
59
|
|
|
|
|
|
|
isa => 'Bool', |
60
|
|
|
|
|
|
|
default => 0, |
61
|
|
|
|
|
|
|
documentation => 'Show the process ID per logging message. ' |
62
|
|
|
|
|
|
|
. 'This is useful when aggregating logs.', |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head3 process_table |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
We also want to write all cmds and exit codes to a table |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
#TODO add a json format also |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=cut |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
option 'process_table' => ( |
74
|
|
|
|
|
|
|
isa => Path, |
75
|
|
|
|
|
|
|
is => 'rw', |
76
|
|
|
|
|
|
|
lazy => 1, |
77
|
|
|
|
|
|
|
coerce => 1, |
78
|
|
|
|
|
|
|
default => sub { |
79
|
|
|
|
|
|
|
my $self = shift; |
80
|
|
|
|
|
|
|
make_path( $self->logdir ); |
81
|
|
|
|
|
|
|
my $process_table = |
82
|
|
|
|
|
|
|
File::Spec->catdir( $self->logdir, "001-task_table.md" ); |
83
|
|
|
|
|
|
|
$process_table = path($process_table); |
84
|
|
|
|
|
|
|
$process_table->touchpath; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $header = |
87
|
|
|
|
|
|
|
"|| Version || Scheduler Id || Jobname || Task Tags || ProcessID || ExitCode || Duration ||\n"; |
88
|
|
|
|
|
|
|
write_file( $process_table, { append => 1 }, $header ) |
89
|
|
|
|
|
|
|
or $self->app_log->warn("Couldn't open process file! $!\n"); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
return $process_table; |
92
|
|
|
|
|
|
|
}, |
93
|
|
|
|
|
|
|
trigger => sub { |
94
|
|
|
|
|
|
|
my $self = shift; |
95
|
|
|
|
|
|
|
$self->process_table->touchpath; |
96
|
|
|
|
|
|
|
}, |
97
|
|
|
|
|
|
|
); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head3 metastr |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
JSON string passed from HPC::Runner::App::Scheduler. It describes the total number of jobs, processes, and job batches. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
option 'metastr' => ( |
106
|
|
|
|
|
|
|
is => 'rw', |
107
|
|
|
|
|
|
|
isa => 'Str', |
108
|
|
|
|
|
|
|
documentation => q{Meta str passed from HPC::Runner::Command::Scheduler}, |
109
|
|
|
|
|
|
|
required => 0, |
110
|
|
|
|
|
|
|
); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
option 'logname' => ( |
113
|
|
|
|
|
|
|
isa => 'Str', |
114
|
|
|
|
|
|
|
is => 'rw', |
115
|
|
|
|
|
|
|
default => 'hpcrunner_logs', |
116
|
|
|
|
|
|
|
required => 0, |
117
|
|
|
|
|
|
|
); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head2 Internal Attributes |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
You shouldn't be calling these directly. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=cut |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
has 'dt' => ( |
126
|
|
|
|
|
|
|
is => 'rw', |
127
|
|
|
|
|
|
|
isa => 'DateTime', |
128
|
|
|
|
|
|
|
default => sub { return DateTime->now( time_zone => 'local' ); }, |
129
|
|
|
|
|
|
|
lazy => 1, |
130
|
|
|
|
|
|
|
); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
##TODO Put this in its own class |
133
|
|
|
|
|
|
|
##Application log |
134
|
|
|
|
|
|
|
has 'app_log' => ( |
135
|
|
|
|
|
|
|
is => 'rw', |
136
|
|
|
|
|
|
|
lazy => 1, |
137
|
|
|
|
|
|
|
default => sub { |
138
|
|
|
|
|
|
|
my $self = shift; |
139
|
|
|
|
|
|
|
my $file_name = File::Spec->catdir( $self->logdir, 'main.log' ); |
140
|
|
|
|
|
|
|
$self->_make_the_dirs( $self->logdir ); |
141
|
|
|
|
|
|
|
my $log_conf = q( |
142
|
|
|
|
|
|
|
log4perl.category = INFO, FILELOG, Screen |
143
|
|
|
|
|
|
|
log4perl.appender.Screen = \ |
144
|
|
|
|
|
|
|
Log::Log4perl::Appender::ScreenColoredLevels |
145
|
|
|
|
|
|
|
log4perl.appender.Screen.layout = \ |
146
|
|
|
|
|
|
|
Log::Log4perl::Layout::PatternLayout |
147
|
|
|
|
|
|
|
log4perl.appender.Screen.layout.ConversionPattern = \ |
148
|
|
|
|
|
|
|
[%d] %m %n |
149
|
|
|
|
|
|
|
log4perl.appender.FILELOG = Log::Log4perl::Appender::File |
150
|
|
|
|
|
|
|
log4perl.appender.FILELOG.mode = append |
151
|
|
|
|
|
|
|
log4perl.appender.FILELOG.layout = Log::Log4perl::Layout::PatternLayout |
152
|
|
|
|
|
|
|
log4perl.appender.FILELOG.layout.ConversionPattern = %d %p %m %n |
153
|
|
|
|
|
|
|
); |
154
|
|
|
|
|
|
|
$log_conf .= "log4perl.appender.FILELOG.filename = $file_name"; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Log::Log4perl->init( \$log_conf ); |
157
|
|
|
|
|
|
|
return get_logger(); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
##Submit Log |
162
|
|
|
|
|
|
|
has 'log' => ( |
163
|
|
|
|
|
|
|
is => 'rw', |
164
|
|
|
|
|
|
|
default => sub { my $self = shift; return $self->init_log }, |
165
|
|
|
|
|
|
|
lazy => 1 |
166
|
|
|
|
|
|
|
); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
has 'logfile' => ( |
169
|
|
|
|
|
|
|
traits => ['String'], |
170
|
|
|
|
|
|
|
is => 'rw', |
171
|
|
|
|
|
|
|
default => \&set_logfile, |
172
|
|
|
|
|
|
|
handles => { |
173
|
|
|
|
|
|
|
append_logfile => 'append', |
174
|
|
|
|
|
|
|
prepend_logfile => 'prepend', |
175
|
|
|
|
|
|
|
clear_logfile => 'clear', |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
); |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head2 Subroutines |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head3 set_logdir |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
Set the log directory |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=cut |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub set_logdir { |
188
|
1
|
|
|
1
|
1
|
6
|
my $self = shift; |
189
|
|
|
|
|
|
|
|
190
|
1
|
|
|
|
|
5
|
my $logdir; |
191
|
|
|
|
|
|
|
|
192
|
1
|
50
|
|
|
|
67
|
if ( $self->has_version ) { |
193
|
0
|
0
|
|
|
|
0
|
if ( $self->has_project ) { |
194
|
0
|
|
|
|
|
0
|
$logdir = |
195
|
|
|
|
|
|
|
File::Spec->catdir( 'hpc-runner', $self->version, $self->project, |
196
|
|
|
|
|
|
|
'logs', $self->set_logfile . '-' . $self->logname ); |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
else { |
199
|
0
|
|
|
|
|
0
|
$logdir = |
200
|
|
|
|
|
|
|
File::Spec->catdir( 'hpc-runner', $self->version, |
201
|
|
|
|
|
|
|
'logs', $self->set_logfile . '-' . $self->logname ); |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
else { |
205
|
1
|
50
|
|
|
|
86
|
if ( $self->has_project ) { |
206
|
0
|
|
|
|
|
0
|
$logdir = |
207
|
|
|
|
|
|
|
File::Spec->catdir( 'hpc-runner', $self->project, |
208
|
|
|
|
|
|
|
'logs', $self->set_logfile . '-' . $self->logname ); |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
else { |
211
|
1
|
|
|
|
|
22
|
$logdir = |
212
|
|
|
|
|
|
|
File::Spec->catdir( 'hpc-runner', |
213
|
|
|
|
|
|
|
'logs', $self->set_logfile . '-' . $self->logname ); |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
1
|
|
|
|
|
11
|
$logdir =~ s/\.log$//; |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# my $dt = DateTime->now( time_zone => 'local' ); |
220
|
|
|
|
|
|
|
# $dt = "$dt"; |
221
|
|
|
|
|
|
|
# $dt =~ s/:/-/g; |
222
|
|
|
|
|
|
|
# |
223
|
|
|
|
|
|
|
# $logdir = File::Spec->catdir( $logdir, $dt ); |
224
|
|
|
|
|
|
|
|
225
|
1
|
|
|
|
|
14
|
$self->_make_the_dirs($logdir); |
226
|
|
|
|
|
|
|
|
227
|
1
|
|
|
|
|
59
|
return $logdir; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head3 set_logfile |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Set logfile |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=cut |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
sub set_logfile { |
237
|
3
|
|
|
3
|
1
|
1820
|
my $self = shift; |
238
|
|
|
|
|
|
|
|
239
|
3
|
|
|
|
|
57
|
my $tt = DateTime->now( time_zone => 'local' )->ymd(); |
240
|
3
|
|
|
|
|
8503
|
return "$tt"; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head3 init_log |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
Initialize Log4perl log |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=cut |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
sub init_log { |
250
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
251
|
|
|
|
|
|
|
|
252
|
0
|
|
|
|
|
|
Log::Log4perl->easy_init( |
253
|
|
|
|
|
|
|
{ |
254
|
|
|
|
|
|
|
level => $TRACE, |
255
|
|
|
|
|
|
|
utf8 => 1, |
256
|
|
|
|
|
|
|
mode => 'append', |
257
|
|
|
|
|
|
|
file => ">>" . File::Spec->catdir( $self->logdir, $self->logfile ), |
258
|
|
|
|
|
|
|
layout => '%d: %p %m%n ' |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
); |
261
|
|
|
|
|
|
|
|
262
|
0
|
|
|
|
|
|
my $log = get_logger(); |
263
|
0
|
|
|
|
|
|
return $log; |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
sub log_main_messages { |
267
|
0
|
|
|
0
|
0
|
|
my ( $self, $level, $message ) = @_; |
268
|
|
|
|
|
|
|
|
269
|
0
|
0
|
|
|
|
|
return unless $message; |
270
|
0
|
0
|
|
|
|
|
$level = 'info' unless $level; |
271
|
0
|
|
|
|
|
|
$self->log->$level($message); |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
1; |