-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathREADME
254 lines (180 loc) · 8.62 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
NAME
Debuggit - A fairly simplistic debug statement handler
SYNOPSIS
use Debuggit DEBUG => 1;
# say you have a global hashref for your site configuration
# (not to imply that global vars are good)
our $Config = get_global_config();
# now we can set some config things based on whether we're in debug mode or not
$Config->{'DB'} = DEBUG ? 'dev' : 'prod';
# maybe we need to pull our local Perl modules from our VC working copy
push @INC, $Config->{'vcdir/lib'} if DEBUG;
# basic debugging output
debuggit("only print this if debugging is on");
debuggit(3 => "only print this if debugging is level 3 or higher");
# show off our formatting
my $var1 = 6;
my $var2;
my $var3 = " leading and trailing spaces ";
# assuming debugging is enabled ...
debuggit("var1 is", $var1); # var1 is 6
debuggit("var2 is", $var2); # var2 is <<undef>>
debuggit("var3 is", $var3); # var3 is << leading and trailing spaces >>
# note that spaces between args, as well as final newlines, are provided automatically
# use "functions" in the debugging args list
my $var4 = { complex => 'hash', with => 'lots', of => 'stuff' };
# this will call Data::Dumper::Dumper() for you
# (even if you've never loaded Data::Dumper)
debuggit("var4 is", DUMP => $var4);
# or maybe you prefer Data::Printer instead?
use Debuggit DEBUG => 1, DataPrinter => 1;
debuggit("var4 is", DUMP => $var4);
# make your own function
Debuggit::add_func(CONFIG => 1,
sub { my ($self, $var) = $_; return (lc($self), 'var', $var, 'is', $Config->{$var}) });
# and use it like so
debuggit(CONFIG => 'DB'); # config var DB is dev
DESCRIPTION
You want debugging? No, you want sophisticated, full-featured, on-demand
debugging, and you don't want to take it out when you release the code
because you might need it again later, but you also don't want it to
take up any space or cause any slowdown of your production application.
Sound impossible? Nah. Just use Debuggit.
Quick Start
To start:
use strict;
use warnings;
use Debuggit;
my $var = 6;
debuggit(2 => "var is", $var); # this does not print
debuggit(4 => "var is", $var); # neither does this
Later ...
use strict;
use warnings;
use Debuggit DEBUG => 2;
my $var = 6;
debuggit(2 => "var is", $var); # now this prints
debuggit(4 => "var is", $var); # but this still doesn't
That's it. Really. Everything else is just gravy.
Documentation
This POD explains just the basics of using "Debuggit". For full details,
see Debuggit::Manual.
EXPORTS
DEBUG
DEBUG is a constant integer set to whatever value you choose:
use Debuggit DEBUG => 2;
or to 0 if you don't choose:
use Debuggit;
Actually, failure to specify a value only defaults to 0 the first time
in a program this is seen. Subsequent times (e.g. in modules included by
the main script), DEBUG will be set to the first value passed in. In
this way, you can set DEBUG in the main script and have it "fall
through" to all included modules. See "The DEBUG Constant" in
Debuggit::Manual for full details.
Functions exported
Only "debuggit" is exported.
FUNCTIONS
debuggit
Use this function to conditionally print debugging output. If the first
argument is a positive integer, the output is printed only if DEBUG is
set to that number or higher. The remaining arguments are concatenated
with spaces, a newline is appended, and the results are printed to
STDERR. Some minor formatting is done to help distinguish "undef" values
and values with leading or trailing spaces. To get further details, or
to learn how to override any of those things, see "The debuggit
function" in Debuggit::Manual.
default_formatter
This is what "debuggit" is set to initially. You can call it directly if
you want to "wrap" "debuggit". For examples of this, see "Wrapping the
debugging output" in Debuggit::Cookbook.
add_func
remove_func
Add or remove debugging functions. Please see "Debugging Functions" in
Debuggit::Manual.
DIAGNOSTICS
* Cannot redefine DEBUG; original value of %s is used
It means you did something like this:
use Debuggit DEBUG => 2;
use Debuggit DEBUG => 3;
only probably not nearly so obvious. Debuggit tries to be very
tolerant of multiple imports into the same package, but the "DEBUG"
symbol is a constant function and can't be redefined without
engendering severe wonkiness, so Debuggit won't do it. As long as
you pass the same value for "DEBUG", that's okay. But if the second
(or more) value is different from the first, then you will get the
original value regardless. At least this way you'll be forewarned.
PERFORMANCE
Debuggit is designed to be left in your code, even when running in
production environments. Because of this, it needs to disappear entirely
when debugging is turned off. It can achieve this unlikely goal via the
use of a compile-time constant. Please see "Performance Considerations"
in Debuggit::Manual for full details.
BUGS and CAVEATS
* Once you set "DEBUG", you can't change it. Even if you try, you get
the original value. See "DIAGNOSTICS".
* Doing:
debuggit(0 => "in production mode");
never prints anything, even when "DEBUG" is 0. That's because
"debuggit" is guaranteed to be an empty function when debugging is
turned off.
* Doing:
debuggit($var, "is the value");
is inherently dangerous. If $var is a positive integer, "debuggit"
would interpret it as a debug level, and not print it. So, either do
this:
debuggit(1 => $var, "is the value");
or this:
debuggit("the value is", $var);
Or, to look at it another way, you can pass a value as the first arg
to print, or you can leave off a debugging level altogether, but
don't try to do both at once.
* Doing:
my $var1 = "DUMP";
my $var2 = "stuff";
debuggit(1 => "vars are", $var1, $var2);
is equivalent to:
debuggit(1 => "vars are", DUMP => $var2);
which is probably not going to do what you want, assuming the
default functions are still in place. See "IMPORTANT CAVEAT!" in
Debuggit::Manual for full details.
* Doing:
debuggit(2 => "first thousand elements:", @array[0..999]);
is likely going to have a performance impact even when debugging is
off. Instead, do:
debuggit("first thousand elements:", @array[0..999]) if DEBUG >= 2;
See "Style Considerations" in Debuggit::Manual for another example
and details on the problem.
That's all I know of. However, lacking omniscience, I welcome bug
reports.
SUPPORT
Debuggit is on GitHub at barefootcoder/debuggit. Feel free to fork and
submit patches. Please note that I develop via TDD (Test-Driven
Development), so a patch that includes a failing test is much more
likely to get accepted (or least likely to get accepted more quickly).
If you just want to report a problem or request a feature, that's okay
too. You can create an issue on GitHub, or a bug in CPAN's RT (at
http://rt.cpan.org). Or just send an email to bug-Debuggit@rt.cpan.org.
AUTHOR
Buddy Burden
CPAN ID: BAREFOOT
Barefoot Software
barefootcoder@gmail.com
COPYRIGHT
This program is free software licensed under
The Artistic License
The full text of the license can be found in the LICENSE file included
with this module.
This module is copyright (c) 2008-2015, Barefoot Software. It has many
venerable ancestors (some more direct than others), including but not
limited to:
* "Barefoot::debug", (c) 2000-2006 Barefoot Software, 2004-2006
ThinkGeek
* "Barefoot::base", (c) 2001-2006 Barefoot Software
* "Geek::Dev::Debug", (c) 2004 ThinkGeek
* "VCtools::Base", (c) 2004-2008 Barefoot Software, 2004 ThinkGeek
* "Barefoot", (c) 2006-2009 Barefoot Software
* "Company::Debug", (c) 2008 Rent.com
SEE ALSO
Log::Log4perl, debug, Debug, Debug::Message, Debug::EchoMessage.
Comparison with most of these (and others) can be found in "Comparison
Matrix" in Debuggit::Manual.