TaskPipe/LoggerManager.pm

filter: Statementstmt 82.6% 95 / 115 Branchbran 50.0% 8 / 16 Conditioncond 42.8% 3 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 86.9% 20 / 23 Podpod 0.0% 0 / 6 totaltotal 74.2% 127 / 171 CC 14 SCAR 12.6
CC
14
Cov
77%
get_log_path
12.0
get_log_methods
7.1
logging_settings
3.2
CRAP 3.5
« TaskPipe/Tool/Command_DeployTables.pm ↑ Summary TaskPipe/Role/MooseType_StrArrayRef.pm »
1 package TaskPipe::LoggerManager;
2  
3 42 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 42 with 'MooseX::ConfigCascade';
5  
6 42 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 42 ▶use TaskPipe::PathSettings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 42 ▶use TaskPipe::LoggerManager::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 42 ▶use Template::Nest;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 42 ▶use TaskPipe::RunInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 42 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 42 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 42 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 42 ▶use File::Path 'make_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 42 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 has path_settings => (is => 'ro', isa => 'TaskPipe::PathSettings', default => sub{
18 1204 ▶ TaskPipe::PathSettings->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
19 42 });
20 has settings => (is => 'ro', isa => 'TaskPipe::LoggerManager::Settings', default => sub{
21 1204 ▶ TaskPipe::LoggerManager::Settings->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
22 42 });
23 42 has foreground => (is => 'rw', isa => 'Bool', default => 1);
24  
25 sub init_logger{
26 907 ▶ my $self = shift;
Subroutine init_logger: called CC=1 SCAR=0.0
Pod undocumented
27  
28 907 Log::Log4perl->init( $self->logging_settings );
29  
30 }
31  
32 has run_info => (is => 'rw', isa => 'TaskPipe::RunInfo', default => sub{
33 1204 ▶ TaskPipe::RunInfo->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
34 42 });
35  
36 #has job_id => (is => 'rw', isa => 'Int');
37 #has run_id => (is => 'rw', isa => 'Int');
38 #has thread_id => (is => 'rw', isa => 'Int',default => 1);
39 #has task_name => (is => 'rw', isa => 'Str');
40  
41 has custom_cspecs => (is => 'ro', isa => 'HashRef', default => sub{{
42 1204 ▶ J => 'job_id',
Subroutine __ANON__: called CC=1 SCAR=0.0
43 #i => 'run_id',
44 h => 'thread_id',
45 N => 'task_name'
46 42 }});
47  
48 #has name => (is => 'rw', isa => 'Maybe[Str]', lazy => 1, default => sub{
49 # my $self = shift;
50 # my $tid = $self->path_settings->project->task_identifier;
51 # my ($name) = ref($self) =~ /$tid(\w+)$/;
52 # return $name;
53 #});
54  
55  
56  
57  
58 sub logging_settings{
59 907 ▶ my $self = shift;
Subroutine logging_settings: called CC=3 SCAR=11.7
Pod undocumented
60  
61 907 my %c;
62  
63 907 my $c = $self->settings;
64  
65 907 my %s;
66 907 $s{rootLogger} = $c->log_level;
67  
68 907 my $log_methods = $self->get_log_methods;
69  
70  
71 907 ▶ if ( $log_methods->{screen} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($log_methods->{"screen"}) 907 0 907
72 907 $s{'rootLogger'}.=', SCREEN';
73 907 $s{'appender.SCREEN'} = 'Log::Log4perl::Appender::Screen';
74 907 $s{'appender.SCREEN.color.DEBUG'} = 'white';
75 907 $s{'appender.SCREEN.color.INFO'} = 'bold white';
76 907 $s{'appender.SCREEN.stderr'} = '0';
77 907 $s{'appender.SCREEN.layout'} = 'Log::Log4perl::Layout::ColoredPatternLayout';
78  
79 907 $s{'appender.SCREEN.layout.ConversionPattern'}=$c->log_screen_pattern;
80 907 $s{'appender.SCREEN.layout.ColorMap'} = $self->build_color_map;
81 907 $s{'appender.SCREEN.layout.ColorMap'} =~ s/\n//g;
82 907 $s{'appender.SCREEN.layout.ColorStyle'} = 'continuous';
83 907 $self->add_custom_cspecs( \%s,'SCREEN' );
84 }
85  
86 907 ▶ if ( $log_methods->{file} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($log_methods->{"file"}) 0 907 907
87  
88 0 $s{'rootLogger'}.=', LOGFILE';
89 0 $s{'appender.LOGFILE'}='Log::Log4perl::Appender::File';
90 0 $s{'appender.LOGFILE.filename'}= $self->get_log_path;
91 0 $s{'appender.LOGFILE.mode'}= $c->log_file_access;
92 0 $s{'appender.LOGFILE.layout'}='PatternLayout';
93 0 $s{'appender.LOGFILE.layout.ConversionPattern'} = $c->log_file_pattern;
94 0 $self->add_custom_cspecs( \%s, 'LOGFILE' );
95 }
96  
97 # print "s: ".Dumper( \%s )."\n";
98 907 return \%s;
99  
100 }
101  
102  
103 sub get_log_methods{
104 907 ▶ my ($self) = @_;
Subroutine get_log_methods: called CC=6 SCAR=19.6
Pod undocumented
105  
106 907 my $log_mode = $self->settings->log_mode;
107 907 my $log_methods = {'screen' => 1 };
108 907 ▶ $log_methods = {'file' => 1 } if $log_mode eq 'file';
Branch: 1 branch
BranchTrueFalseTotal
if $log_mode eq "file" 0 907 907
109 907 ▶ $log_methods = {'file' => 1 } if ( $log_mode eq 'shell' && $self->run_info->shell eq 'background');
Branch: 1 branch
BranchTrueFalseTotal
if $log_mode eq "shell" and $self->run_info->shell eq "background" 0 907 907
Condition33%1/3
$log_mode eq "shell" and $self->run_info->shell eq "background"
!ll&&!rl&&r
09070
MC/DC0%0/2
$log_mode eq "shell" and $self->run_info->shell eq "background"
$log_mode eq "shell" $self->run_info->shell eq "background"
Truth table33%1/3
A and B
A= $log_mode eq "shell"B= $self->run_info->shell eq "background"
ABresult
0X0
100
111
110 907 ▶ $log_methods->{'file'} = 1 if $log_mode eq 'both';
Branch: 1 branch
BranchTrueFalseTotal
if $log_mode eq "both" 0 907 907
111 907 ▶ $log_methods = {} if $log_mode eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $log_mode eq "none" 0 907 907
112  
113 907 return $log_methods;
114 }
115
116  
117  
118 sub add_custom_cspecs{
119 907 ▶ my ($self,$s,$appender) = @_;
Subroutine add_custom_cspecs: called CC=3 SCAR=11.0
Pod undocumented
120  
121 907 foreach my $cspec (keys %{$self->custom_cspecs}){
122 2721 my $method = $self->custom_cspecs->{$cspec};
123 2721 ▶ my $var = $self->run_info->$method || '?';
Condition100%2/2
$self->run_info->$method || '?'
l!l
8541867
MC/DC100%1/1
$self->run_info->$method || '?'
$self->run_info->$method
Truth table100%2/2
A
A= $self->run_info->$method
Aresult
11
00
124 2721 $s->{"appender.$appender.layout.cspec.$cspec"} = "sub{ return '$var'; }";
125 }
126 }
127  
128  
129 sub get_log_path{
130 0 ▶ my ($self) = @_;
Subroutine get_log_path: not called CC=3 SCAR=24.8
Pod undocumented
131  
132 0 my $dt = DateTime->now;
133  
134 0 my $nest = Template::Nest->new( template_hash => {
135 log_dir_format => +$self->settings->log_dir_format,
136 filename_format => +$self->settings->log_filename_format
137 });
138  
139 0 my @cmd;
140  
141 my $dir = $self->path_settings->path(
142 'log', $dt->ymd,
143 +$nest->render({
144 NAME => 'log_dir_format',
145 job_id => $self->run_info->job_id,
146 0 cmd => +join('-',@{$self->run_info->cmd})
147 })
148 );
149  
150 0 ▶ if ( ! -d $dir ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
151  
152 try {
153  
154 0 ▶ make_path( $dir );
Subroutine __ANON__: not called CC=1 SCAR=6.9
155  
156 } catch {
157  
158 0 ▶ confess "Could not create directory $dir: $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
159  
160 0 };
161  
162 }
163  
164 0 ▶ my $filename = $nest->render({
Condition0%0/2
$self->run_info->thread_id || 1
l!l
00
MC/DC0%0/1
$self->run_info->thread_id || 1
$self->run_info->thread_id
Truth table0%0/2
A
A= $self->run_info->thread_id
Aresult
11
00
165 NAME => 'filename_format',
166 thread_id => +$self->run_info->thread_id || 1
167 });
168
169 0 my $log_path = +File::Spec->catdir( $dir, $filename );
170 #print "log path is: ".$log_path."\n";
171 0 return $log_path;
172  
173 }
174
175  
176  
177 sub build_color_map{
178 907 ▶ my ($self) = @_;
Subroutine build_color_map: called CC=3 SCAR=11.0
Pod undocumented
179  
180 907 local $Data::Dumper::Terse = 1;
181  
182 907 my @kv;
183 907 my %sc = %{$self->settings->log_screen_colors};
184 907 foreach my $cspec ( keys %sc ){
185 6349 my $cspec_str;
186 6349 ▶ if ( ref $sc{$cspec} eq ref {} ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sc{$cspec} eq ref {}) { } 907 5442 6349
187 $cspec_str = $cspec
188 .' => sub { my($val) = @_; my $case = '
189 907 .Dumper( $sc{$cspec} )
190 .'; return +$case->{ $val }; }';
191 907 $cspec_str =~ s/\n//gs;
192 } else {
193 5442 $cspec_str = "'".$cspec."' => '".$sc{$cspec}."',";
194 }
195 6349 push @kv,$cspec_str;
196 }
197  
198 907 my $color_map = 'sub { return { '.join( ', ', @kv ).'}}';
199 907 return $color_map;
200 }
201
202  
203 =head1 NAME
204  
205 TaskPipe::LoggerManager - Logging manager for TaskPipe
206  
207 =head1 DESCRIPTION
208  
209 It is not recommended to use this module directly. See the general manpages for TaskPipe
210  
211 =head1 AUTHOR
212  
213 Tom Gracey <tomgracey@gmail.com>
214  
215 =head1 COPYRIGHT AND LICENSE
216  
217 Copyright (c) Tom Gracey 2018
218  
219 TaskPipe is free software, licensed under
220  
221 The GNU Public License Version 3
222  
223 =cut
224  
225 42 __PACKAGE__->meta->make_immutable;
226 42 1;
227
228
« TaskPipe/Tool/Command_DeployTables.pm ↑ Summary TaskPipe/Role/MooseType_StrArrayRef.pm »