blib/lib/App/CELL.pm

filter: Statementstmt 89.5% 77 / 86 Branchbran 65.0% 13 / 20 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 91.6% 22 / 24 Podpod 100% 9 / 9 totaltotal 84.5% 131 / 155 CC 16 SCAR 10.1
CC
16
Cov
84%
appname
6.0
load
5.1
loaded
5.0
CRAP 2.7
« blib/lib/App/CELL/Test.pm ↑ Summary blib/lib/App/CELL/Test/LogToFile.pm »
1 # *************************************************************************
2 # Copyright (c) 2014-2020, SUSE LLC
3 #
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 #
16 # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 # *************************************************************************
32  
33 package App::CELL;
34  
35 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 10 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 10 ▶use App::CELL::Config qw( $meta $core $site );
Subroutine BEGIN: called CC=1 SCAR=0.0
41 10 ▶use App::CELL::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 10 ▶use App::CELL::Log qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
43 10 ▶use App::CELL::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 10 ▶use App::CELL::Util qw( stringify_args utc_timestamp );
Subroutine BEGIN: called CC=1 SCAR=0.0
45 10 ▶use Params::Validate qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
46 10 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48  
49 =head1 NAME
50  
51 App::CELL - Configuration, Error-handling, Localization, and Logging
52  
53  
54  
55 =head1 VERSION
56  
57 Version 0.231
58  
59 =cut
60  
61 10 our $VERSION = '0.231';
62  
63  
64  
65 =head1 SYNOPSIS
66  
67 # imagine you have a script/app called 'foo' . . .
68  
69 use Log::Any::Adapter ( 'File', "/var/tmp/foo.log" );
70 use App::CELL qw( $CELL $log $meta $site );
71  
72 # load config params and messages from sitedir
73 my $status = $CELL->load( sitedir => '/etc/foo' );
74 return $status unless $status->ok;
75  
76 # set appname to FOO_APPNAME (a config param just loaded from sitedir)
77 $CELL->appname( $CELL->FOO_APPNAME || "foo" );
78  
79 # write to the log
80 $log->notice("Configuration loaded from /etc/foo");
81  
82 # get value of site configuration parameter FOO_PARAM
83 my $val = $site->FOO_PARAM;
84  
85 # get a list of all supported languages
86 my @supp_lang = $CELL->supported_languages;
87  
88 # determine if a language is supported
89 print "sk supported" if $CELL->language_supported('sk');
90  
91 # get message object and text in default language
92 $status = $CELL->msg('FOO_INFO_MSG');
93 my $fmsg = $status->payload if $status->ok;
94 my $text = $fmsg->text;
95  
96 # get message object and text in default language
97 # (message that takes arguments)
98 $fmsg = $CELL->msg('BAR_ARGS_MSG', "arg1", "arg2");
99 print $fmsg->text, "\n";
100  
101 # get text of message in a different language
102 my $sk_text = $fmsg->lang('sk')->text;
103  
104  
105  
106  
107 =head1 DESCRIPTION
108  
109 This is the top-level module of App::CELL, the Configuration,
110 Error-handling, Localization, and Logging framework for applications (or
111 scripts) written in Perl.
112  
113 For details, read the POD in the L<App::CELL> distro. For an introduction,
114 read L<App::CELL::Guide>.
115  
116  
117  
118 =head1 EXPORTS
119  
120 This module provides the following exports:
121  
122 =over
123  
124 =item C<$CELL> - App::CELL singleton object
125  
126 =item C<$log> - App::CELL::Log singleton object
127  
128 =item C<$meta> - App::CELL::Config singleton object
129  
130 =item C<$core> - App::CELL::Config singleton object
131  
132 =item C<$site> - App::CELL::Config singleton object
133  
134 =back
135  
136 =cut
137  
138 10 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
139 10 our @EXPORT_OK = qw( $CELL $log $meta $core $site );
140  
141 10 our $CELL = bless {
142 appname => __PACKAGE__,
143 enviro => '',
144 }, __PACKAGE__;
145  
146 # ($log is imported from App::CELL::Log)
147 # ($meta, $core, and $site are imported from App::CELL::Config)
148  
149  
150  
151 =head1 METHODS
152  
153  
154 =head2 appname
155  
156 If no argument is given, returns the C<appname> -- i.e. the name of the
157 application or script that is using L<App::CELL> for its configuration,
158 error handling, etc.
159  
160 If an argument is given, assumes that it denotes the desired C<appname> and sets
161 it. Also initializes the logger.
162  
163 =cut
164  
165 0 ▶sub appname {
Subroutine appname: not called CC=2 SCAR=17.9
Pod documented
166 0 my @ARGS = @_;
167 0 ▶ return $CELL->{appname} if not @ARGS;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGS 0 0 0
168 0 $CELL->{appname} = $ARGS[0];
169 0 $log->ident( $CELL->{'appname'} );
170 }
171  
172  
173 =head2 enviro
174  
175 Get the C<enviro> attribute, i.e. the name of the environment variable
176 containing the sitedir
177  
178 =cut
179  
180 0 ▶sub enviro { return $CELL->{enviro}; }
Subroutine enviro: not called CC=1 SCAR=6.9
Pod documented
181  
182  
183 =head2 loaded
184  
185 Get the current load status, which can be any of the following:
186 0 nothing loaded yet
187 'SHARE' sharedir loaded
188 'BOTH' sharedir _and_ sitedir loaded
189  
190 =cut
191  
192 7 ▶sub loaded {
Subroutine loaded: called CC=5 SCAR=16.1
Pod documented
193 7 ▶ return 'SHARE' if $App::CELL::Load::sharedir_loaded and not
Branch: 1 branch
BranchTrueFalseTotal
if $App::CELL::Load::sharedir_loaded and not @App::CELL::Load::sitedir 3 4 7
Condition100%3/3
$App::CELL::Load::sharedir_loaded and not @App::CELL::Load::sitedir
!ll&&!rl&&r
313
MC/DC100%2/2
$App::CELL::Load::sharedir_loaded and not @App::CELL::Load::sitedir
$App::CELL::Load::sharedir_loaded not @App::CELL::Load::sitedir
Truth table100%3/3
A and not(B)
A= $App::CELL::Load::sharedir_loadedB= not @App::CELL::Load::sitedir
ABresult
0X0
100
111
194 @App::CELL::Load::sitedir;
195 4 ▶ return 'BOTH' if $App::CELL::Load::sharedir_loaded and
Branch: 1 branch
BranchTrueFalseTotal
if $App::CELL::Load::sharedir_loaded and @App::CELL::Load::sitedir 1 3 4
Condition66%2/3
$App::CELL::Load::sharedir_loaded and @App::CELL::Load::sitedir
!ll&&!rl&&r
301
MC/DC50%1/2
$App::CELL::Load::sharedir_loaded and @App::CELL::Load::sitedir
$App::CELL::Load::sharedir_loaded @App::CELL::Load::sitedir
Truth table66%2/3
A and B
A= $App::CELL::Load::sharedir_loadedB= @App::CELL::Load::sitedir
ABresult
0X0
100
111
196 @App::CELL::Load::sitedir;
197 3 return 0;
198 }
199  
200  
201 =head2 sharedir
202  
203 Get the C<sharedir> attribute, i.e. the full path of the site configuration
204 directory (available only after sharedir has been successfully loaded)
205  
206 =cut
207  
208 2 ▶sub sharedir {
Subroutine sharedir: called CC=2 SCAR=6.9
Pod documented
209 2 ▶ return '' if not $App::CELL::Load::sharedir_loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $App::CELL::Load::sharedir_loaded 1 1 2
210 1 return $App::CELL::Load::sharedir;
211 }
212  
213  
214 =head2 sitedir
215  
216 Get the C<sitedir> attribute, i.e. the full path of the site configuration
217 directory (available only after sitedir has been successfully loaded)
218  
219 =cut
220  
221 1 ▶sub sitedir {
Subroutine sitedir: called CC=2 SCAR=9.2
Pod documented
222 1 ▶ return '' if not $App::CELL::Load::sitedir;
Branch: 1 branch
BranchTrueFalseTotal
unless $App::CELL::Load::sitedir 1 0 1
223 0 return $App::CELL::Load::sitedir;
224 }
225  
226  
227 =head2 supported_languages
228  
229 Get list of supported languages. Equivalent to:
230  
231 $site->CELL_SUPP_LANG || [ 'en ]
232  
233 =cut
234  
235 3 ▶sub supported_languages {
Subroutine supported_languages: called CC=1 SCAR=0.0
Pod documented
236 3 return App::CELL::Message::supported_languages();
237 }
238  
239  
240 =head2 language_supported
241  
242 Determine if a given language is supported.
243  
244 =cut
245  
246 5 ▶sub language_supported {
Subroutine language_supported: called CC=1 SCAR=0.0
Pod documented
247 5 return App::CELL::Message::language_supported( $_[1] );
248 }
249  
250  
251 =head2 C<load>
252  
253 Attempt to load messages and configuration parameters from the sharedir
254 and, possibly, the sitedir as well.
255  
256 Takes: a PARAMHASH that should include at least one of C<enviro> or
257 C<sitedir> (if both are given, C<enviro> takes precedence with C<sitedir>
258 as a fallback). The PARAMHASH can also include a C<verbose> parameter
259 which, when set to a true value, causes the load routine to log more
260 verbosely.
261  
262 Returns: an C<App::CELL::Status> object, which could be any of the
263 following:
264 OK success
265 WARN previous call already succeeded, nothing to do
266 ERR failure
267  
268 On success, it also sets the C<CELL_META_START_DATETIME> meta parameter.
269  
270 =cut
271  
272 10 ▶sub load {
Subroutine load: called CC=5 SCAR=16.2
Pod documented
273 10 my $class = shift;
274 10 my ( %ARGS ) = validate( @_, {
275 enviro => { type => SCALAR, optional => 1 },
276 sitedir => { type => SCALAR, optional => 1 },
277 verbose => { type => SCALAR, default => 0 },
278 } );
279 10 my $status;
280  
281 10 $log->info( "CELL version $VERSION called from " . (caller)[0] .
282 " with arguments " . stringify_args( \%ARGS ),
283 cell => 1, suppress_caller => 1 );
284  
285 # we only get past this next call if at least the sharedir loads
286 # successfully (sitedir is optional)
287 10 $status = App::CELL::Load::init( %ARGS );
288 10 ▶ return $status unless $status->ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $status->ok 8 2 10
289 2 ▶ $log->info( "App::CELL has finished loading messages and site conf params",
Branch: 1 branch
BranchTrueFalseTotal
if $App::CELL::meta->CELL_META_LOAD_VERBOSE 0 2 2
290 cell => 1 ) if $meta->CELL_META_LOAD_VERBOSE;
291  
292 2 $log->show_caller( $site->CELL_LOG_SHOW_CALLER );
293 2 $log->debug_mode ( $site->CELL_DEBUG_MODE );
294  
295 2 ▶ $App::CELL::Message::supp_lang = $site->CELL_SUPP_LANG || [ 'en' ];
Condition50%1/2
$App::CELL::site->CELL_SUPP_LANG || ['en']
l!l
20
MC/DC0%0/1
$App::CELL::site->CELL_SUPP_LANG || ['en']
$App::CELL::site->CELL_SUPP_LANG
Truth table50%1/2
A
A= $App::CELL::site->CELL_SUPP_LANG
Aresult
11
00
296 2 ▶ $App::CELL::Message::def_lang = $site->CELL_DEF_LANG || 'en';
Condition50%1/2
$App::CELL::site->CELL_DEF_LANG || 'en'
l!l
20
MC/DC0%0/1
$App::CELL::site->CELL_DEF_LANG || 'en'
$App::CELL::site->CELL_DEF_LANG
Truth table50%1/2
A
A= $App::CELL::site->CELL_DEF_LANG
Aresult
11
00
297  
298 2 $meta->set( 'CELL_META_START_DATETIME', utc_timestamp() );
299 2 $log->info( "**************** App::CELL $VERSION loaded and ready ****************",
300 cell => 1, suppress_caller => 1 );
301  
302 2 return App::CELL::Status->ok;
303 }
304  
305  
306  
307 =head2 Status constructors
308  
309 The following "factory" makes a bunch of status constructor methods
310 (wrappers for App::CELL::Status->new )
311  
312 =cut
313  
314 10 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
315 10 foreach (@App::CELL::Log::permitted_levels) {
316 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
317 150 my $level_uc = $_;
318 150 my $level_lc = lc $_;
319 150 ▶ *{"status_$level_lc"} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
320 22 my ( $self, $code, @ARGS ) = @_;
321 22 ▶ if ( @ARGS % 2 ) { # odd number of arguments
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGS % 2) 0 22 22
322 0 $log->warn( "status_$level_lc called with odd number (" .
323 scalar @ARGS .
324 ") of arguments; discarding the arguments!" );
325 0 @ARGS = ();
326 }
327 22 my %ARGS = @ARGS;
328 22 return App::CELL::Status->new(
329 level => $level_uc,
330 code => $code,
331 caller => [ CORE::caller() ],
332 %ARGS,
333 );
334 }
335 150 }
336 }
337  
338 =head3 status_crit
339  
340 Constructor for 'CRIT' status objects
341  
342 =head3 status_critical
343  
344 Constructor for 'CRIT' status objects
345  
346 =head3 status_debug
347  
348 Constructor for 'DEBUG' status objects
349  
350 =head3 status_emergency
351  
352 Constructor for 'DEBUG' status objects
353  
354 =head3 status_err
355  
356 Constructor for 'ERR' status objects
357  
358 =head3 status_error
359  
360 Constructor for 'ERR' status objects
361  
362 =head3 status_fatal
363  
364 Constructor for 'FATAL' status objects
365  
366 =head3 status_info
367  
368 Constructor for 'INFO' status objects
369  
370 =head3 status_inform
371  
372 Constructor for 'INFORM' status objects
373  
374 =head3 status_not_ok
375  
376 Constructor for 'NOT_OK' status objects
377  
378 =head3 status_notice
379  
380 Constructor for 'NOTICE' status objects
381  
382 =head3 status_ok
383  
384 Constructor for 'OK' status objects
385  
386 =head3 status_trace
387  
388 Constructor for 'TRACE' status objects
389  
390 =head3 status_warn
391  
392 Constructor for 'WARN' status objects
393  
394 =head3 status_warning
395  
396 Constructor for 'WARNING' status objects
397  
398  
399 =head2 msg
400  
401 Construct a message object (wrapper for App::CELL::Message::new)
402  
403 =cut
404  
405 4 ▶sub msg {
Subroutine msg: called CC=3 SCAR=12.0
Pod documented
406 4 my ( $self, $code, @ARGS ) = @_;
407 4 my $status = App::CELL::Message->new( code => $code, args => [ @ARGS ] );
408 4 ▶ return if $status->not_ok; # will return undef in scalar mode
Branch: 1 branch
BranchTrueFalseTotal
if $status->not_ok 0 4 4
409 4 ▶ return $status->payload if blessed $status->payload;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($status->payload) 4 0 4
410 0 return;
411 }
412  
413  
414  
415  
416 =head1 LICENSE AND COPYRIGHT
417  
418 Copyright (c) 2014-2020, SUSE LLC
419  
420 All rights reserved.
421  
422 Redistribution and use in source and binary forms, with or without
423 modification, are permitted provided that the following conditions are met:
424  
425 1. Redistributions of source code must retain the above copyright notice,
426 this list of conditions and the following disclaimer.
427  
428 2. Redistributions in binary form must reproduce the above copyright
429 notice, this list of conditions and the following disclaimer in the
430 documentation and/or other materials provided with the distribution.
431  
432 3. Neither the name of SUSE LLC nor the names of its contributors may be
433 used to endorse or promote products derived from this software without
434 specific prior written permission.
435  
436 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
437 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
438 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
439 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
440 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
441 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
442 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
443 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
444 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
445 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
446 POSSIBILITY OF SUCH DAMAGE.
447  
448 =cut
449  
450 # END OF CELL MODULE
451 10 1;
« blib/lib/App/CELL/Test.pm ↑ Summary blib/lib/App/CELL/Test/LogToFile.pm »