| 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::XMPP3::Debug; | ||||||||||||||
| 23 | |||||||||||||||
| 24 | =head1 NAME | ||||||||||||||
| 25 | |||||||||||||||
| 26 | Net::XMPP3::Debug - XMPP Debug Module | ||||||||||||||
| 27 | |||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||
| 29 | |||||||||||||||
| 30 | Net::XMPP3::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::XMPP3 modules. It provides | ||||||||||||||
| 36 | the Net::XMPP3 modules with an object to control where, how, and | ||||||||||||||
| 37 | what is logged. | ||||||||||||||
| 38 | |||||||||||||||
| 39 | =head2 Basic Functions | ||||||||||||||
| 40 | |||||||||||||||
| 41 | $Debug = new Net::XMPP3::Debug(); | ||||||||||||||
| 42 | |||||||||||||||
| 43 | $Debug->Init(level=>2, | ||||||||||||||
| 44 | file=>"stdout", | ||||||||||||||
| 45 | header=>"MyScript"); | ||||||||||||||
| 46 | |||||||||||||||
| 47 | $Debug->Log0("Connection established"); | ||||||||||||||
| 48 | |||||||||||||||
| 49 | =head1 METHODS | ||||||||||||||
| 50 | |||||||||||||||
| 51 | =head2 Basic Functions | ||||||||||||||
| 52 | |||||||||||||||
| 53 | new(hash) - creates the Debug object. The hash argument is passed | ||||||||||||||
| 54 | to the Init function. See that function description | ||||||||||||||
| 55 | below for the valid settings. | ||||||||||||||
| 56 | |||||||||||||||
| 57 | Init(level=>integer, - initializes the debug object. The level | ||||||||||||||
| 58 | file=>string, determines the maximum level of debug | ||||||||||||||
| 59 | header=>string, messages to log: | ||||||||||||||
| 60 | setdefault=>0|1, 0 - Base level Output (default) | ||||||||||||||
| 61 | usedefault=>0|1, 1 - High level API calls | ||||||||||||||
| 62 | time=>0|1) 2 - Low level API calls | ||||||||||||||
| 63 | ... | ||||||||||||||
| 64 | N - Whatever you want.... | ||||||||||||||
| 65 | The file determines where the debug log | ||||||||||||||
| 66 | goes. You can either specify a path to | ||||||||||||||
| 67 | a file, or "stdout" (the default). "stdout" | ||||||||||||||
| 68 | tells Debug to send all of the debug info | ||||||||||||||
| 69 | sent to this object to go to stdout. | ||||||||||||||
| 70 | header is a string that will preappended | ||||||||||||||
| 71 | to the beginning of all log entries. This | ||||||||||||||
| 72 | makes it easier to see what generated the | ||||||||||||||
| 73 | log entry (default is "Debug"). | ||||||||||||||
| 74 | setdefault saves the current filehandle | ||||||||||||||
| 75 | and makes it available for other Debug | ||||||||||||||
| 76 | objects to use. To use the default set | ||||||||||||||
| 77 | usedefault to 1. The time parameter | ||||||||||||||
| 78 | specifies whether or not to add a timestamp | ||||||||||||||
| 79 | to the beginning of each logged line. | ||||||||||||||
| 80 | |||||||||||||||
| 81 | LogN(array) - Logs the elements of the array at the corresponding | ||||||||||||||
| 82 | debug level N. If you pass in a reference to an | ||||||||||||||
| 83 | array or hash then they are printed in a readable | ||||||||||||||
| 84 | way. (ie... Log0, Log2, Log100, etc...) | ||||||||||||||
| 85 | |||||||||||||||
| 86 | =head1 EXAMPLE | ||||||||||||||
| 87 | |||||||||||||||
| 88 | $Debug = new Net::XMPP3:Debug(level=>2, | ||||||||||||||
| 89 | header=>"Example"); | ||||||||||||||
| 90 | |||||||||||||||
| 91 | $Debug->Log0("test"); | ||||||||||||||
| 92 | |||||||||||||||
| 93 | $Debug->Log2("level 2 test"); | ||||||||||||||
| 94 | |||||||||||||||
| 95 | $hash{a} = "atest"; | ||||||||||||||
| 96 | $hash{b} = "btest"; | ||||||||||||||
| 97 | |||||||||||||||
| 98 | $Debug->Log1("hashtest",\%hash); | ||||||||||||||
| 99 | |||||||||||||||
| 100 | You would get the following log: | ||||||||||||||
| 101 | |||||||||||||||
| 102 | Example: test | ||||||||||||||
| 103 | Example: level 2 test | ||||||||||||||
| 104 | Example: hashtest { a=>"atest" b=>"btest" } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | If you had set the level to 1 instead of 2 you would get: | ||||||||||||||
| 107 | |||||||||||||||
| 108 | Example: test | ||||||||||||||
| 109 | Example: hashtest { a=>"atest" b=>"btest" } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | =head1 AUTHOR | ||||||||||||||
| 112 | |||||||||||||||
| 113 | Ryan Eatmon | ||||||||||||||
| 114 | |||||||||||||||
| 115 | =head1 COPYRIGHT | ||||||||||||||
| 116 | |||||||||||||||
| 117 | This module is free software, you can redistribute it and/or modify it | ||||||||||||||
| 118 | under the LGPL. | ||||||||||||||
| 119 | |||||||||||||||
| 120 | =cut | ||||||||||||||
| 121 | |||||||||||||||
| 122 | 11 | require 5.003; | |||||||||||||
| 123 | 11 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 124 | 11 | ▶ | use FileHandle; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 125 | 11 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 126 | 11 | ▶ | use vars qw( %HANDLES $DEFAULT $DEFAULTLEVEL $DEFAULTTIME $AUTOLOAD ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 127 | |||||||||||||||
| 128 | 11 | $DEFAULTLEVEL = -1; | |||||||||||||
| 129 | |||||||||||||||
| 130 | sub new | ||||||||||||||
| 131 | 18 | ▶ | { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 132 | 18 | my $proto = shift; | |||||||||||||
| 133 | 18 | my $self = { }; | |||||||||||||
| 134 | 18 | bless($self, $proto); | |||||||||||||
| 135 | |||||||||||||||
| 136 | 18 | $self->Init(@_); | |||||||||||||
| 137 | |||||||||||||||
| 138 | 18 | return $self; | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | |||||||||||||||
| 142 | ############################################################################## | ||||||||||||||
| 143 | # | ||||||||||||||
| 144 | # Init - opens the fielhandle and initializes the Debug object. | ||||||||||||||
| 145 | # | ||||||||||||||
| 146 | ############################################################################## | ||||||||||||||
| 147 | sub Init | ||||||||||||||
| 148 | 18 | ▶ | { | ||||||||||||
|
Subroutine
Init: called CC=16 SCAR=37.9
Pod
undocumented
| |||||||||||||||
| 149 | 18 | my $self = shift; | |||||||||||||
| 150 | |||||||||||||||
| 151 | 18 | my %args; | |||||||||||||
| 152 | 18 | ▶ | while($#_ >= 0) { $args{ lc pop(@_) } = pop(@_); } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | |||||||||||||||
| 154 | 18 | ▶ | delete($args{file}) if (lc($args{file}) eq "stdout"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 155 | |||||||||||||||
| 156 | 18 | ▶ | $args{time} = 0 if !exists($args{time}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 157 | 18 | ▶ | $args{setdefault} = 0 if !exists($args{setdefault}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 158 | 18 | ▶ | $args{usedefault} = 0 if !exists($args{usedefault}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | |||||||||||||||
| 160 | 18 | $self->{TIME} = $args{time}; | |||||||||||||
| 161 | |||||||||||||||
| 162 | 18 | ▶ | if ($args{usedefault} == 1) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 163 | { | ||||||||||||||
| 164 | 11 | $args{setdefault} = 0; | |||||||||||||
| 165 | 11 | $self->{USEDEFAULT} = 1; | |||||||||||||
| 166 | } | ||||||||||||||
| 167 | else | ||||||||||||||
| 168 | { | ||||||||||||||
| 169 | 7 | $self->{LEVEL} = 0; | |||||||||||||
| 170 | 7 | ▶ | $self->{LEVEL} = $args{level} if exists($args{level}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 171 | |||||||||||||||
| 172 | 7 | $self->{HANDLE} = new FileHandle(">&STDERR"); | |||||||||||||
| 173 | 7 | $self->{HANDLE}->autoflush(1); | |||||||||||||
| 174 | 7 | ▶ | if (exists($args{file})) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 175 | { | ||||||||||||||
| 176 | 0 | ▶ | if (exists($Net::XMPP3::Debug::HANDLES{$args{file}})) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 177 | { | ||||||||||||||
| 178 | 0 | $self->{HANDLE} = $Net::XMPP3::Debug::HANDLES{$args{file}}; | |||||||||||||
| 179 | 0 | $self->{HANDLE}->autoflush(1); | |||||||||||||
| 180 | } | ||||||||||||||
| 181 | else | ||||||||||||||
| 182 | { | ||||||||||||||
| 183 | 0 | ▶ | if (-e $args{file}) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 184 | { | ||||||||||||||
| 185 | 0 | ▶ | if (-w $args{file}) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 186 | { | ||||||||||||||
| 187 | 0 | $self->{HANDLE} = new FileHandle(">$args{file}"); | |||||||||||||
| 188 | 0 | ▶ | if (defined($self->{HANDLE})) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 189 | { | ||||||||||||||
| 190 | 0 | $self->{HANDLE}->autoflush(1); | |||||||||||||
| 191 | 0 | $Net::XMPP3::Debug::HANDLES{$args{file}} = $self->{HANDLE}; | |||||||||||||
| 192 | } | ||||||||||||||
| 193 | else | ||||||||||||||
| 194 | { | ||||||||||||||
| 195 | 0 | print STDERR "ERROR: Debug filehandle could not be opened.\n"; | |||||||||||||
| 196 | 0 | print STDERR" Debugging disabled.\n"; | |||||||||||||
| 197 | 0 | print STDERR " ($!)\n"; | |||||||||||||
| 198 | 0 | $self->{LEVEL} = -1; | |||||||||||||
| 199 | } | ||||||||||||||
| 200 | } | ||||||||||||||
| 201 | else | ||||||||||||||
| 202 | { | ||||||||||||||
| 203 | 0 | print STDERR "ERROR: You do not have permission to write to $args{file}.\n"; | |||||||||||||
| 204 | 0 | print STDERR" Debugging disabled.\n"; | |||||||||||||
| 205 | 0 | $self->{LEVEL} = -1; | |||||||||||||
| 206 | } | ||||||||||||||
| 207 | } | ||||||||||||||
| 208 | else | ||||||||||||||
| 209 | { | ||||||||||||||
| 210 | 0 | $self->{HANDLE} = new FileHandle(">$args{file}"); | |||||||||||||
| 211 | 0 | ▶ | if (defined($self->{HANDLE})) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 212 | { | ||||||||||||||
| 213 | 0 | $self->{HANDLE}->autoflush(1); | |||||||||||||
| 214 | 0 | $Net::XMPP3::Debug::HANDLES{$args{file}} = $self->{HANDLE}; | |||||||||||||
| 215 | } | ||||||||||||||
| 216 | else | ||||||||||||||
| 217 | { | ||||||||||||||
| 218 | 0 | print STDERR "ERROR: Debug filehandle could not be opened.\n"; | |||||||||||||
| 219 | 0 | print STDERR" Debugging disabled.\n"; | |||||||||||||
| 220 | 0 | print STDERR " ($!)\n"; | |||||||||||||
| 221 | 0 | $self->{LEVEL} = -1; | |||||||||||||
| 222 | } | ||||||||||||||
| 223 | } | ||||||||||||||
| 224 | } | ||||||||||||||
| 225 | } | ||||||||||||||
| 226 | } | ||||||||||||||
| 227 | 18 | ▶ | if ($args{setdefault} == 1) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 228 | { | ||||||||||||||
| 229 | 7 | $Net::XMPP3::Debug::DEFAULT = $self->{HANDLE}; | |||||||||||||
| 230 | 7 | $Net::XMPP3::Debug::DEFAULTLEVEL = $self->{LEVEL}; | |||||||||||||
| 231 | 7 | $Net::XMPP3::Debug::DEFAULTTIME = $self->{TIME}; | |||||||||||||
| 232 | } | ||||||||||||||
| 233 | |||||||||||||||
| 234 | 18 | $self->{HEADER} = "Debug"; | |||||||||||||
| 235 | 18 | ▶ | $self->{HEADER} = $args{header} if exists($args{header}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 236 | } | ||||||||||||||
| 237 | |||||||||||||||
| 238 | |||||||||||||||
| 239 | ############################################################################## | ||||||||||||||
| 240 | # | ||||||||||||||
| 241 | # Log - takes the limit and the array to log and logs them | ||||||||||||||
| 242 | # | ||||||||||||||
| 243 | ############################################################################## | ||||||||||||||
| 244 | sub Log | ||||||||||||||
| 245 | 0 | ▶ | { | ||||||||||||
|
Subroutine
Log: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||
| 246 | 0 | my $self = shift; | |||||||||||||
| 247 | 0 | my (@args) = @_; | |||||||||||||
| 248 | |||||||||||||||
| 249 | 0 | my $fh = $self->{HANDLE}; | |||||||||||||
| 250 | 0 | ▶ | $fh = $Net::XMPP3::Debug::DEFAULT if exists($self->{USEDEFAULT}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 251 | |||||||||||||||
| 252 | 0 | my $string = ""; | |||||||||||||
| 253 | |||||||||||||||
| 254 | 0 | my $testTime = $self->{TIME}; | |||||||||||||
| 255 | 0 | ▶ | $testTime = $Net::XMPP3::Debug::DEFAULTTIME if exists($self->{USEDEFAULT}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 256 | |||||||||||||||
| 257 | 0 | ▶ | $string .= "[".&Net::XMPP3::GetTimeStamp("local",time,"short")."] " | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 258 | if ($testTime == 1); | ||||||||||||||
| 259 | 0 | $string .= $self->{HEADER}.": "; | |||||||||||||
| 260 | |||||||||||||||
| 261 | 0 | my $arg; | |||||||||||||
| 262 | |||||||||||||||
| 263 | 0 | foreach $arg (@args) | |||||||||||||
| 264 | { | ||||||||||||||
| 265 | 0 | ▶ | if (ref($arg) eq "HASH") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 266 | { | ||||||||||||||
| 267 | 0 | $string .= " {"; | |||||||||||||
| 268 | 0 | my $key; | |||||||||||||
| 269 | 0 | foreach $key (sort {$a cmp $b} keys(%{$arg})) | |||||||||||||
| 270 | { | ||||||||||||||
| 271 | 0 | $string .= " ".$key."=>'".$arg->{$key}."'"; | |||||||||||||
| 272 | } | ||||||||||||||
| 273 | 0 | $string .= " }"; | |||||||||||||
| 274 | } | ||||||||||||||
| 275 | else | ||||||||||||||
| 276 | { | ||||||||||||||
| 277 | 0 | ▶ | if (ref($arg) eq "ARRAY") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 278 | { | ||||||||||||||
| 279 | 0 | $string .= " [ ".join(" ",@{$arg})." ]"; | |||||||||||||
| 280 | } else { | ||||||||||||||
| 281 | 0 | $string .= $arg; | |||||||||||||
| 282 | } | ||||||||||||||
| 283 | } | ||||||||||||||
| 284 | } | ||||||||||||||
| 285 | 0 | print $fh "$string\n"; | |||||||||||||
| 286 | 0 | return 1; | |||||||||||||
| 287 | } | ||||||||||||||
| 288 | |||||||||||||||
| 289 | |||||||||||||||
| 290 | ############################################################################## | ||||||||||||||
| 291 | # | ||||||||||||||
| 292 | # AUTOLOAD - if a function is called that is not defined then this function | ||||||||||||||
| 293 | # will examine the function name and either give an error or call | ||||||||||||||
| 294 | # the appropriate function. | ||||||||||||||
| 295 | # | ||||||||||||||
| 296 | ############################################################################## | ||||||||||||||
| 297 | sub AUTOLOAD | ||||||||||||||
| 298 | 2021 | ▶ | { | ||||||||||||
|
Subroutine
AUTOLOAD: called CC=5 SCAR=16.5
| |||||||||||||||
| 299 | 2021 | my $self = shift; | |||||||||||||
| 300 | 2021 | ▶ | return if ($AUTOLOAD =~ /::DESTROY$/); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 301 | 2015 | my ($function) = ($AUTOLOAD =~ /\:\:(.*)$/); | |||||||||||||
| 302 | 2015 | ▶ | croak("$function not defined") if !($function =~ /Log\d+/); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 303 | 2015 | my ($level) = ($function =~ /Log(\d+)/); | |||||||||||||
| 304 | 2015 | ▶ | return 0 if ($level > (exists($self->{USEDEFAULT}) ? $Net::XMPP3::Debug::DEFAULTLEVEL : $self->{LEVEL})); | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 305 | 0 | $self->Log(@_); | |||||||||||||
| 306 | } | ||||||||||||||
| 307 | |||||||||||||||
| 308 | |||||||||||||||
| 309 | ############################################################################## | ||||||||||||||
| 310 | # | ||||||||||||||
| 311 | # GetHandle - returns the filehandle being used by this object. | ||||||||||||||
| 312 | # | ||||||||||||||
| 313 | ############################################################################## | ||||||||||||||
| 314 | sub GetHandle | ||||||||||||||
| 315 | 1 | ▶ | { | ||||||||||||
|
Subroutine
GetHandle: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 316 | 1 | my $self = shift; | |||||||||||||
| 317 | 1 | return $self->{HANDLE}; | |||||||||||||
| 318 | } | ||||||||||||||
| 319 | |||||||||||||||
| 320 | |||||||||||||||
| 321 | ############################################################################## | ||||||||||||||
| 322 | # | ||||||||||||||
| 323 | # GetLevel - returns the debug level used by this object. | ||||||||||||||
| 324 | # | ||||||||||||||
| 325 | ############################################################################## | ||||||||||||||
| 326 | sub GetLevel | ||||||||||||||
| 327 | 1 | ▶ | { | ||||||||||||
|
Subroutine
GetLevel: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 328 | 1 | my $self = shift; | |||||||||||||
| 329 | 1 | return $self->{LEVEL}; | |||||||||||||
| 330 | } | ||||||||||||||
| 331 | |||||||||||||||
| 332 | |||||||||||||||
| 333 | ############################################################################## | ||||||||||||||
| 334 | # | ||||||||||||||
| 335 | # GetTime - returns the debug time used by this object. | ||||||||||||||
| 336 | # | ||||||||||||||
| 337 | ############################################################################## | ||||||||||||||
| 338 | sub GetTime | ||||||||||||||
| 339 | 1 | ▶ | { | ||||||||||||
|
Subroutine
GetTime: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 340 | 1 | my $self = shift; | |||||||||||||
| 341 | 1 | return $self->{TIME}; | |||||||||||||
| 342 | } | ||||||||||||||
| 343 | |||||||||||||||
| 344 | |||||||||||||||
| 345 | 11 | 1; | |||||||||||||