blib/lib/Net/XMPP/Debug.pm

filter: Statementstmt 50.9% 56 / 110 Branchbran 44.2% 23 / 52 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 91.6% 11 / 12 Podpod 33.3% 2 / 6 totaltotal 51.3% 95 / 185 SCAR 50.0
CC
30
Cov
49%
Log
90.0
Init
63.1
AUTOLOAD
5.2
CRAP 148.7
« blib/lib/Net/XMPP.pm ↑ Summary blib/lib/Net/XMPP/JID.pm »
1 ##############################################################################
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Library General Public
5 # License as published by the Free Software Foundation; either
6 # version 2 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Library General Public License for more details.
12 #
13 # You should have received a copy of the GNU Library General Public
14 # License along with this library; if not, write to the
15 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 # Boston, MA 02111-1307, USA.
17 #
18 # Copyright (C) 1998-2004 Jabber Software Foundation http://jabber.org/
19 #
20 ##############################################################################
21  
22 package Net::XMPP::Debug;
23  
24 =head1 NAME
25  
26 Net::XMPP::Debug - XMPP Debug Module
27  
28 =head1 SYNOPSIS
29  
30 Net::XMPP::Debug is a module that provides a developer easy access
31 to logging debug information.
32  
33 =head1 DESCRIPTION
34  
35 Debug is a helper module for the Net::XMPP modules. It provides
36 the Net::XMPP modules with an object to control where, how, and
37 what is logged.
38  
39 =head2 Basic Functions
40  
41 $Debug = Net::XMPP::Debug->new();
42  
43 $Debug->Init(
44 level => 2,
45 file => "stdout",
46 header =>"MyScript");
47  
48 $Debug->Log0("Connection established");
49  
50 =head1 METHODS
51  
52 =head2 Basic Functions
53  
54 =over 4
55  
56 =item new
57  
58 new(hash)
59  
60 creates the Debug object. The hash argument is passed
61 to the Init function. See that function description
62 below for the valid settings.
63  
64 =item Init
65  
66 Init(
67 level => integer,
68 file => string,
69 header => string,
70 setdefault => 0|1,
71 usedefault => 0|1,
72 time => 0|1)
73  
74 initializes the debug object.
75  
76 The B<level> determines the maximum level of debug
77 messages to log:
78  
79 0 - Base level Output (default)
80 1 - High level API calls
81 2 - Low level API calls
82 ...
83 N - Whatever you want....
84  
85 The B<file> determines where the debug log
86 goes. You can either specify a path to
87 a file, or "stdout" (the default). "stdout"
88 tells Debug to send all of the debug info
89 sent to this object to go to stdout.
90  
91 B<header> is a string that will preappended
92 to the beginning of all log entries. This
93 makes it easier to see what generated the
94 log entry (default is "Debug").
95  
96 B<setdefault> saves the current filehandle
97 and makes it available for other Debug
98 objects to use. To use the default set
99 B<usedefault> to 1.
100  
101 The B<time> parameter specifies whether or not to add a
102 timestamp to the beginning of each logged line.
103  
104 =item LogN
105  
106 LogN(array)
107  
108 Logs the elements of the array at the corresponding
109 debug level N. If you pass in a reference to an
110 array or hash then they are printed in a readable
111 way. (ie... Log0, Log2, Log100, etc...)
112  
113 =back
114  
115 =head1 EXAMPLE
116  
117 $Debug = Net::XMPP:Debug->new(level=>2,
118 header=>"Example");
119  
120 $Debug->Log0("test");
121  
122 $Debug->Log2("level 2 test");
123  
124 $hash{a} = "atest";
125 $hash{b} = "btest";
126  
127 $Debug->Log1("hashtest", \%hash);
128  
129 You would get the following log:
130  
131 Example: test
132 Example: level 2 test
133 Example: hashtest { a=>"atest" b=>"btest" }
134  
135 If you had set the level to 1 instead of 2 you would get:
136  
137 Example: test
138 Example: hashtest { a=>"atest" b=>"btest" }
139  
140 =head1 AUTHOR
141  
142 Originally authored by Ryan Eatmon.
143  
144 Previously maintained by Eric Hacker.
145  
146 Currently maintained by Darian Anthony Patrick.
147  
148 =head1 COPYRIGHT
149  
150 This module is free software, you can redistribute it and/or modify it
151 under the LGPL 2.1.
152  
153 =cut
154  
155 15 require 5.008;
156 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
158 15 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
159 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
160 15 ▶use vars qw( %HANDLES $DEFAULT $DEFAULTLEVEL $DEFAULTTIME $AUTOLOAD );
Subroutine BEGIN: called CC=1 SCAR=0.0
161  
162 15 $DEFAULTLEVEL = -1;
163  
164 sub new
165 {
166 25 ▶ my $proto = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
167 25 my $self = { };
168 25 bless($self, $proto);
169  
170 25 $self->Init(@_);
171  
172 25 return $self;
173 }
174  
175  
176 ##############################################################################
177 #
178 # Init - opens the fielhandle and initializes the Debug object.
179 #
180 ##############################################################################
181 sub Init
182 {
183 25 ▶ my $self = shift;
Subroutine Init: called CC=18 SCAR=41.4
Pod documented
184  
185 25 my %args;
186 25 ▶ while($#_ >= 0) { $args{ lc pop(@_) } = pop(@_); }
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ >= 0) 74 25 99
187  
188 25 ▶ delete($args{file}) if (defined $args{file} && lc($args{file}) eq "stdout");
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"file"} and lc $args{"file"} eq "stdout" 10 15 25
Condition66%2/3
defined $args{"file"} and lc $args{"file"} eq "stdout"
!ll&&!rl&&r
15010
MC/DC50%1/2
defined $args{"file"} and lc $args{"file"} eq "stdout"
defined $args{"file"} lc $args{"file"} eq "stdout"
Truth table66%2/3
A and B
A= defined $args{"file"}B= lc $args{"file"} eq "stdout"
ABresult
0X0
100
111
189  
190 25 ▶ $args{time} = 0 if !exists($args{time});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"time"} 21 4 25
191 25 ▶ $args{setdefault} = 0 if !exists($args{setdefault});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"setdefault"} 15 10 25
192 25 ▶ $args{usedefault} = 0 if !exists($args{usedefault});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"usedefault"} 10 15 25
193  
194 25 $self->{TIME} = $args{time};
195  
196 25 ▶ if ($args{usedefault} == 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'usedefault'} == 1) { } 15 10 25
197 {
198 15 $args{setdefault} = 0;
199 15 $self->{USEDEFAULT} = 1;
200 }
201 else
202 {
203 10 $self->{LEVEL} = 0;
204 10 ▶ $self->{LEVEL} = $args{level} if exists($args{level});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"level"} 10 0 10
205 10 ▶ if ($self->{LEVEL} >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"LEVEL"} >= 0) 0 10 10
206 {
207  
208 0 $self->{HANDLE} = FileHandle->new(">&STDERR");
209 0 $self->{HANDLE}->autoflush(1);
210 0 ▶ if (exists($args{file}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"file"}) 0 0 0
211 {
212 0 ▶ if (exists($Net::XMPP::Debug::HANDLES{$args{file}}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Net::XMPP::Debug::HANDLES{$args{'file'}}) { } 0 0 0
213 {
214 0 $self->{HANDLE} = $Net::XMPP::Debug::HANDLES{$args{file}};
215 0 $self->{HANDLE}->autoflush(1);
216 }
217 else
218 {
219 0 ▶ if (-e $args{file})
Branch: 1 branch
BranchTrueFalseTotal
if (-e $args{'file'}) { } 0 0 0
220 {
221 0 ▶ if (-w $args{file})
Branch: 1 branch
BranchTrueFalseTotal
if (-w $args{'file'}) { } 0 0 0
222 {
223 0 $self->{HANDLE} = FileHandle->new(">$args{file}");
224 0 ▶ if (defined($self->{HANDLE}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'HANDLE'}) { } 0 0 0
225 {
226 0 $self->{HANDLE}->autoflush(1);
227 0 $Net::XMPP::Debug::HANDLES{$args{file}} = $self->{HANDLE};
228 }
229 else
230 {
231 0 print STDERR "ERROR: Debug filehandle could not be opened.\n";
232 0 print STDERR" Debugging disabled.\n";
233 0 print STDERR " ($!)\n";
234 0 $self->{LEVEL} = -1;
235 }
236 }
237 else
238 {
239 0 print STDERR "ERROR: You do not have permission to write to $args{file}.\n";
240 0 print STDERR" Debugging disabled.\n";
241 0 $self->{LEVEL} = -1;
242 }
243 }
244 else
245 {
246 0 $self->{HANDLE} = FileHandle->new(">$args{file}");
247 0 ▶ if (defined($self->{HANDLE}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'HANDLE'}) { } 0 0 0
248 {
249 0 $self->{HANDLE}->autoflush(1);
250 0 $Net::XMPP::Debug::HANDLES{$args{file}} = $self->{HANDLE};
251 }
252 else
253 {
254 0 print STDERR "ERROR: Debug filehandle could not be opened.\n";
255 0 print STDERR" Debugging disabled.\n";
256 0 print STDERR " ($!)\n";
257 0 $self->{LEVEL} = -1;
258 }
259 }
260 }
261 }
262 }
263 }
264 25 ▶ if ($args{setdefault} == 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"setdefault"} == 1) 10 15 25
265 {
266 10 $Net::XMPP::Debug::DEFAULT = $self->{HANDLE};
267 10 $Net::XMPP::Debug::DEFAULTLEVEL = $self->{LEVEL};
268 10 $Net::XMPP::Debug::DEFAULTTIME = $self->{TIME};
269 }
270  
271 25 $self->{HEADER} = "Debug";
272 25 ▶ $self->{HEADER} = $args{header} if exists($args{header});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"header"} 25 0 25
273 }
274  
275  
276 ##############################################################################
277 #
278 # Log - takes the limit and the array to log and logs them
279 #
280 ##############################################################################
281 sub Log
282 {
283 0 ▶ my $self = shift;
Subroutine Log: not called CC=9 SCAR=45.0
Pod undocumented
284 0 my (@args) = @_;
285  
286 0 my $fh = $self->{HANDLE};
287 0 ▶ $fh = $Net::XMPP::Debug::DEFAULT if exists($self->{USEDEFAULT});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"USEDEFAULT"} 0 0 0
288 0 ▶ return if not $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
289  
290 0 my $string = "";
291  
292 0 my $testTime = $self->{TIME};
293 0 ▶ $testTime = $Net::XMPP::Debug::DEFAULTTIME if exists($self->{USEDEFAULT});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"USEDEFAULT"} 0 0 0
294  
295 0 ▶ $string .= "[".&Net::XMPP::GetTimeStamp("local",time,"short")."] "
Branch: 1 branch
BranchTrueFalseTotal
if $testTime == 1 0 0 0
296 if ($testTime == 1);
297 0 $string .= $self->{HEADER}.": ";
298  
299 0 my $arg;
300  
301 0 foreach $arg (@args)
302 {
303 0 ▶ if (ref($arg) eq "HASH")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'HASH') { } 0 0 0
304 {
305 0 $string .= " {";
306 0 my $key;
307 0 foreach $key (sort {$a cmp $b} keys(%{$arg}))
308 {
309 0 $string .= " ".$key."=>'".$arg->{$key}."'";
310 }
311 0 $string .= " }";
312 }
313 else
314 {
315 0 ▶ if (ref($arg) eq "ARRAY")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 0 0 0
316 {
317 0 $string .= " [ ".join(" ",@{$arg})." ]";
318 } else {
319 0 $string .= $arg;
320 }
321 }
322 }
323 0 print $fh "$string\n";
324 0 return 1;
325 }
326  
327  
328 ##############################################################################
329 #
330 # AUTOLOAD - if a function is called that is not defined then this function
331 # will examine the function name and either give an error or call
332 # the appropriate function.
333 #
334 ##############################################################################
335 sub AUTOLOAD
336 {
337 2060 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=5 SCAR=16.5
338 2060 ▶ return if ($AUTOLOAD =~ /::DESTROY$/);
Branch: 1 branch
BranchTrueFalseTotal
if $Net::XMPP::Debug::AUTOLOAD =~ /::DESTROY$/ 7 2053 2060
339 2053 my ($function) = ($AUTOLOAD =~ /\:\:(.*)$/);
340 2053 ▶ croak("$function not defined") if !($function =~ /Log\d+/);
Branch: 1 branch
BranchTrueFalseTotal
unless $function =~ /Log\d+/ 0 2053 2053
341 2053 my ($level) = ($function =~ /Log(\d+)/);
342 2053 ▶ return 0 if ($level > (exists($self->{USEDEFAULT}) ? $Net::XMPP::Debug::DEFAULTLEVEL : $self->{LEVEL}));
Branch: 2 branches
BranchTrueFalseTotal
if $level > (exists $self->{"USEDEFAULT"} ? $Net::XMPP::Debug::DEFAULTLEVEL : $self->{"LEVEL"}) 2053 0 2053
exists $self->{'USEDEFAULT'} ? : 1994 59 2053
343 0 $self->Log(@_);
344 }
345  
346  
347 ##############################################################################
348 #
349 # GetHandle - returns the filehandle being used by this object.
350 #
351 ##############################################################################
352 sub GetHandle
353 {
354 4 ▶ my $self = shift;
Subroutine GetHandle: called CC=1 SCAR=0.0
Pod undocumented
355 4 return $self->{HANDLE};
356 }
357  
358  
359 ##############################################################################
360 #
361 # GetLevel - returns the debug level used by this object.
362 #
363 ##############################################################################
364 sub GetLevel
365 {
366 4 ▶ my $self = shift;
Subroutine GetLevel: called CC=1 SCAR=0.0
Pod undocumented
367 4 return $self->{LEVEL};
368 }
369  
370  
371 ##############################################################################
372 #
373 # GetTime - returns the debug time used by this object.
374 #
375 ##############################################################################
376 sub GetTime
377 {
378 4 ▶ my $self = shift;
Subroutine GetTime: called CC=1 SCAR=0.0
Pod undocumented
379 4 return $self->{TIME};
380 }
381  
382  
383 15 1;
« blib/lib/Net/XMPP.pm ↑ Summary blib/lib/Net/XMPP/JID.pm »