line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::Usul::TraitFor::OutputLogging; |
2
|
|
|
|
|
|
|
|
3
|
18
|
|
|
18
|
|
9940
|
use namespace::autoclean; |
|
18
|
|
|
|
|
39
|
|
|
18
|
|
|
|
|
108
|
|
4
|
|
|
|
|
|
|
|
5
|
18
|
|
|
18
|
|
1393
|
use Class::Usul::Constants qw( BRK FAILED FALSE NUL TRUE WIDTH ); |
|
18
|
|
|
|
|
51
|
|
|
18
|
|
|
|
|
151
|
|
6
|
18
|
|
|
18
|
|
15149
|
use Class::Usul::Functions qw( abs_path emit emit_err throw ); |
|
18
|
|
|
|
|
51
|
|
|
18
|
|
|
|
|
97
|
|
7
|
18
|
|
|
18
|
|
21254
|
use Class::Usul::Types qw( Bool SimpleStr ); |
|
18
|
|
|
|
|
52
|
|
|
18
|
|
|
|
|
107
|
|
8
|
18
|
|
|
18
|
|
24505
|
use Text::Autoformat qw( autoformat ); |
|
18
|
|
|
|
|
556777
|
|
|
18
|
|
|
|
|
1378
|
|
9
|
18
|
|
|
18
|
|
144
|
use Moo::Role; |
|
18
|
|
|
|
|
36
|
|
|
18
|
|
|
|
|
177
|
|
10
|
18
|
|
|
18
|
|
7532
|
use Class::Usul::Options; |
|
18
|
|
|
|
|
54
|
|
|
18
|
|
|
|
|
144
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
requires qw( config log ); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Public attributes |
15
|
|
|
|
|
|
|
option 'locale' => is => 'lazy', isa => SimpleStr, format => 's', |
16
|
|
|
|
|
|
|
documentation => 'Loads the specified language message catalogue', |
17
|
4
|
|
|
4
|
|
141
|
builder => sub { $_[ 0 ]->config->locale }, short => 'L'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
option 'quiet' => is => 'ro', isa => Bool, default => FALSE, |
20
|
|
|
|
|
|
|
documentation => 'Quiet the display of information messages', |
21
|
|
|
|
|
|
|
reader => 'quiet_flag', short => 'q'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Private attributes |
24
|
|
|
|
|
|
|
has '_quiet_flag' => is => 'rw', isa => Bool, |
25
|
4
|
|
|
4
|
|
100
|
builder => sub { $_[ 0 ]->quiet_flag }, |
26
|
|
|
|
|
|
|
lazy => TRUE, writer => '_set__quiet_flag'; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Private methods |
29
|
|
|
|
|
|
|
my $_loc = sub { |
30
|
|
|
|
|
|
|
my ($self, $text, $opts, $quote) = @_; $opts //= {}; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
return $self->localize( $text // '[no message]', { |
33
|
|
|
|
|
|
|
locale => $self->locale, |
34
|
|
|
|
|
|
|
no_quote_bind_values => $quote // $opts->{no_quote_bind_values} // FALSE, |
35
|
|
|
|
|
|
|
params => $opts->{args} // [] } ); |
36
|
|
|
|
|
|
|
}; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Public methods |
39
|
|
|
|
|
|
|
sub add_leader { |
40
|
7
|
100
|
100
|
7
|
1
|
581
|
my ($self, $text, $opts) = @_; $text or return NUL; $opts //= {}; |
|
7
|
|
|
|
|
29
|
|
|
5
|
|
|
|
|
26
|
|
41
|
|
|
|
|
|
|
|
42
|
5
|
50
|
|
|
|
104
|
my $leader = $opts->{no_lead} ? NUL : (ucfirst $self->config->name).BRK; |
43
|
|
|
|
|
|
|
|
44
|
5
|
50
|
|
|
|
175
|
if ($opts->{fill}) { |
45
|
0
|
|
0
|
|
|
0
|
my $width = $opts->{width} // WIDTH; |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
0
|
$text = autoformat $text, { right => $width - 1 - length $leader }; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
5
|
50
|
|
|
|
26
|
return join "\n", map { (m{ \A $leader }mx ? NUL : $leader).$_ } |
|
5
|
|
|
|
|
78
|
|
51
|
|
|
|
|
|
|
split m{ \n }mx, $text; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub error { |
55
|
2
|
|
|
2
|
1
|
3263
|
my ($self, $text, $opts) = @_; $text = $self->$_loc( $text, $opts ); |
|
2
|
|
|
|
|
9
|
|
56
|
|
|
|
|
|
|
|
57
|
2
|
|
|
|
|
140
|
$self->log->error( $_ ) for (split m{ \n }mx, "${text}"); |
58
|
|
|
|
|
|
|
|
59
|
2
|
|
|
|
|
17
|
emit_err $self->add_leader( $text, $opts ); |
60
|
|
|
|
|
|
|
|
61
|
2
|
|
|
|
|
10
|
return TRUE; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub fatal { |
65
|
0
|
|
|
0
|
1
|
0
|
my ($self, $text, $opts) = @_; my (undef, $file, $line) = caller 0; |
|
0
|
|
|
|
|
0
|
|
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
0
|
my $posn = ' at '.abs_path( $file )." line ${line}"; |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
0
|
$text = $self->$_loc( $text, $opts ).$posn; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
0
|
$self->log->alert( $_ ) for (split m{ \n }mx, $text); |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
0
|
emit_err $self->add_leader( $text, $opts ); |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
0
|
exit FAILED; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub info { |
79
|
1
|
|
|
1
|
1
|
14743
|
my ($self, $text, $opts) = @_; |
80
|
|
|
|
|
|
|
|
81
|
1
|
|
50
|
|
|
8
|
$opts //= {}; $text = $self->$_loc( $text, $opts, TRUE ); |
|
1
|
|
|
|
|
6
|
|
82
|
|
|
|
|
|
|
|
83
|
1
|
|
|
|
|
27
|
$self->log->info( $_ ) for (split m{ [\n] }mx, $text); |
84
|
|
|
|
|
|
|
|
85
|
1
|
0
|
33
|
|
|
8
|
$self->quiet or $opts->{quiet} or emit $self->add_leader( $text, $opts ); |
86
|
|
|
|
|
|
|
|
87
|
1
|
|
|
|
|
34
|
return TRUE; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub loc { |
91
|
4
|
|
|
4
|
1
|
10
|
my $self = shift; return $self->l10n->localizer( $self->locale, @_ ); |
|
4
|
|
|
|
|
92
|
|
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub output { |
95
|
2
|
|
|
2
|
1
|
5
|
my ($self, $text, $opts) = @_; |
96
|
|
|
|
|
|
|
|
97
|
2
|
|
50
|
|
|
7
|
$opts //= {}; $text = $self->$_loc( $text, $opts, TRUE ); |
|
2
|
|
|
|
|
7
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
my $code = sub { |
100
|
4
|
50
|
33
|
4
|
|
21
|
$opts->{to} && $opts->{to} eq 'err' ? emit_err( @_ ) : emit( @_ ); |
101
|
2
|
|
|
|
|
11
|
}; |
102
|
|
|
|
|
|
|
|
103
|
2
|
100
|
|
|
|
10
|
$code->() if $opts->{cl}; |
104
|
2
|
|
|
|
|
10
|
$code->( $self->add_leader( $text, $opts ) ); |
105
|
2
|
100
|
|
|
|
11
|
$code->() if $opts->{nl}; |
106
|
2
|
|
|
|
|
12
|
return TRUE; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub quiet { |
110
|
8
|
50
|
|
8
|
1
|
24
|
my ($self, $v) = @_; defined $v or return $self->_quiet_flag; $v = !!$v; |
|
8
|
|
|
|
|
175
|
|
|
0
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
0
|
0
|
|
|
|
|
$v != TRUE and throw 'Cannot turn quiet mode off'; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
return $self->_set__quiet_flag( $v ); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub warning { |
118
|
0
|
|
|
0
|
1
|
|
my ($self, $text, $opts) = @_; |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
0
|
|
|
|
$opts //= {}; $text = $self->$_loc( $text, $opts ); |
|
0
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
$self->log->warn( $_ ) for (split m{ \n }mx, $text); |
123
|
|
|
|
|
|
|
|
124
|
0
|
0
|
0
|
|
|
|
$self->quiet or $opts->{quiet} or emit $self->add_leader( $text, $opts ); |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
return TRUE; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
__END__ |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=pod |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=encoding utf-8 |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 Name |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Class::Usul::TraitFor::OutputLogging - Localised logging and command line output methods |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 Synopsis |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
use Moo; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
extends 'Class::Usul'; |
146
|
|
|
|
|
|
|
with 'Class::Usul::TraitFor::OutputLogging'; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 Description |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Localised logging and command line output methods |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 Configuration and Environment |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Requires the following; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=over 3 |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item C<config> |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item C<log> |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=back |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Defines the following command line options; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=over 3 |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item C<L locale> |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Print text and error messages in the selected language. If no language |
171
|
|
|
|
|
|
|
catalogue is supplied prints text and errors in terse English. Defaults |
172
|
|
|
|
|
|
|
to C<en> |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=item C<q quiet_flag> |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Quietens the usual started/finished information messages |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=back |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 Subroutines/Methods |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 add_leader |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
$leader = $self->add_leader( $text, $opts ); |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Prepend C<< $self->config->name >> to each line of C<$text>. If |
187
|
|
|
|
|
|
|
C<< $opts->{no_lead} >> exists then do nothing. Return C<$text> with |
188
|
|
|
|
|
|
|
leader prepended |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head2 error |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
$self->error( $text, $opts ); |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Calls L<Class::Usul::localize|Class::Usul/localize> with |
195
|
|
|
|
|
|
|
the passed options. Logs the result at the error level, then adds the |
196
|
|
|
|
|
|
|
program leader and prints the result to I<STDERR> |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head2 fatal |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
$self->fatal( $text, $opts ); |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Calls L<Class::Usul::localize|Class::Usul/localize> with |
203
|
|
|
|
|
|
|
the passed options. Logs the result at the alert level, then adds the |
204
|
|
|
|
|
|
|
program leader and prints the result to I<STDERR>. Exits with a return |
205
|
|
|
|
|
|
|
code of one |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head2 info |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
$self->info( $text, $opts ); |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Calls L<Class::Usul::localize|Class::Usul/localize> with |
212
|
|
|
|
|
|
|
the passed options. Logs the result at the info level, then adds the |
213
|
|
|
|
|
|
|
program leader and prints the result to I<STDOUT> |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head2 loc |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
$localized_text = $self->loc( $message, @options ); |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
Localises the message. Calls L<localizer|Class::Usul::L10N/localizer>. The |
220
|
|
|
|
|
|
|
domains to search are in the C<l10n_domains> configuration attribute. Adds |
221
|
|
|
|
|
|
|
C<< $self->locale >> to the arguments passed to C<localizer> |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head2 output |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
$self->output( $text, $opts ); |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Calls L<Class::Usul::localize|Class::Usul/localize> with |
228
|
|
|
|
|
|
|
the passed options. Adds the program leader and prints the result to |
229
|
|
|
|
|
|
|
I<STDOUT> |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head2 quiet |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
$bool = $self->quiet( $bool ); |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Custom accessor/mutator for the C<quiet_flag> attribute. Will throw if you try |
236
|
|
|
|
|
|
|
to turn quiet mode off |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head2 warning |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
$self->warning( $text, $opts ); |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Calls L<Class::Usul::localize|Class::Usul/localize> with |
243
|
|
|
|
|
|
|
the passed options. Logs the result at the warning level, then adds the |
244
|
|
|
|
|
|
|
program leader and prints the result to I<STDOUT> |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=head1 Diagnostics |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
None |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=head1 Dependencies |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=over 3 |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=item L<Class::Usul::Options> |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=item L<Text::Autoformat> |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=item L<Moo::Role> |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=back |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head1 Incompatibilities |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
There are no known incompatibilities in this module |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=head1 Bugs and Limitations |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
There are no known bugs in this module. Please report problems to |
269
|
|
|
|
|
|
|
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-Usul. |
270
|
|
|
|
|
|
|
Patches are welcome |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=head1 Acknowledgements |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
Larry Wall - For the Perl programming language |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=head1 Author |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
Peter Flanigan, C<< <pjfl@cpan.org> >> |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head1 License and Copyright |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
Copyright (c) 2017 Peter Flanigan. All rights reserved |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
285
|
|
|
|
|
|
|
under the same terms as Perl itself. See L<perlartistic> |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, |
288
|
|
|
|
|
|
|
but WITHOUT WARRANTY; without even the implied warranty of |
289
|
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=cut |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
# Local Variables: |
294
|
|
|
|
|
|
|
# mode: perl |
295
|
|
|
|
|
|
|
# tab-width: 3 |
296
|
|
|
|
|
|
|
# End: |
297
|
|
|
|
|
|
|
# vim: expandtab shiftwidth=3: |