Crop/Debug.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 88 CC 8 SCAR 27.4
CC
8
Cov
0%
debug
42.0
_verbose
12.0
BEGIN
2.0
CRAP 15.5
1 package Crop::Debug;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Crop::Debug - Debugging utilities for the Crop framework
8  
9 =head1 SYNOPSIS
10  
11 use Crop::Debug;
12 # ...usage...
13  
14 =head1 DESCRIPTION
15  
16 Crop::Debug provides debugging utilities for the Crop framework.
17  
18 =head1 AUTHORS
19  
20 Euvgenio (Core Developer)
21  
22 Alex (Contributor)
23  
24 =head1 COPYRIGHT AND LICENSE
25  
26 Apache 2.0
27  
28 =cut
29  
30 use base qw/ Crop Exporter /;
31  
32 =begin nd
33 Class: Crop::Debug
34 Debug output.
35  
36 Each message has a specific Layer. Layers are independent one with other.
37
38 There're Layers:
39
40 DL_APP - "APPlication" layer. This is a default layer for general purpose. Don't leave such messages in the production code to avoid huge number of dirty stuffs.
41 DL_SRV - "SeRVer" layer. This describes the main server logic (<Crop::Server>).
42 DL_SQL - serves database logic.
43  
44 To switch On output layer you have to include an element <layer name="SRV"/> to the main config file.
45 <output="off"> disables output at all.
46  
47 Example:
48 (start code)
49 # print dump
50 debug $hash_ref;
51  
52 # layer specified
53 debug DL_SRV, "\$var=$var; object=", $object;
54 (end)
55 =cut
56  
57 use v5.14;
58 use warnings;
59 no warnings 'experimental::smartmatch';
60  
61 use Encode qw/ encode /;
62 use Data::Dumper;
63 use Time::Stamp -stamps => {dt_sep => ' ', ms => 1};
64  
65 use vars qw/ @EXPORT /;
66 @EXPORT = qw/ &debug DL_APP DL_SRV DL_SQL /;
67  
68 =begin nd
69 Constant: Prefix
70 common prefix for all exported layers constants
71 =cut
72 use constant {
73 Prefix => 'DL_',
74 };
75  
76 =begin nd
77 Constants: Layers to export:
78  
79 Constant: DL_APP
80 application layer (default)
81  
82 Constant: DL_SRV
83 Server logic
84  
85 Constant: DL_SQL
86 database
87 =cut
88 use constant {
89 DL_APP => Prefix . 'APP',
90 DL_SRV => Prefix . 'SRV',
91 DL_SQL => Prefix . 'SQL',
92 };
93  
94 =begin nd
95 Constant: DefaultLayer
96 <debug (@messages)> without the layer spicified <debug (@messages) will use this layer.
97 =cut
98 use constant {
99 DefaultLayer => DL_APP,
100 };
101  
102 =begin nd
103 Variable: my @Layer_const
104 All the debugging Layers with.
105 =cut
106 my @Layer_const = qw/ DL_APP DL_SRV DL_SQL /;
107  
108 =begin nd
109 Function: debug ($layer, @messages)
110 Print debug message according to the settings in the config.
111  
112 Parameters:
113 $layer - output Layer; optional
114 @messages - output items; if item is a reference, will print dump
115 =cut
116 sub debug {
117 my $layer = $_[0] && $_[0] ~~ @Layer_const ? shift : DefaultLayer;
118  
119 # drop the layer prefix to print in a config-fasion manner
120 my $prefix = Prefix;
121 (my $short) = $layer =~ /^$prefix(\w+)$/;
122
123 return unless _verbose($short);
124
125 my $script = $0 =~ /public_html(\S+)/ ? $1 : ''; # script name
126 my $output = localstamp() . " $script (pid=$$) Debug[$short]: ";
127 for my $arg (@_) {
128 $arg = '' unless defined $arg;
129 $output .= ref $arg ? Dumper $arg : $arg;
130 }
131 $output .= "\n";
132  
133 print STDERR encode 'utf8', $output;
134 flush STDERR;
135 }
136  
137 =begin nd
138 Function: _verbose ($layer)
139 Is the layer has to be printed?
140
141 Parameters:
142 $layer - a layer name in form of a global.xml config, for example, 'SRV'.
143  
144 Returns:
145 true - output ON
146 false - output OFF
147 =cut
148 sub _verbose {
149 my $layer = shift;
150  
151 my $conf = Crop->C->{debug};
152 return 1 unless $conf->{output} eq 'On';
153
154 return unless exists $conf->{layer};
155
156 $layer ~~ @{$conf->{layer}};
157 }
158  
159 1;