blob: 3989293f97a6a8a9ff5c36e69379b7df277fdd33 (
plain) (
blame)
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
|
package Net::HTTP::Spore::Role::Debug;
use IO::File;
use Moose::Role;
has trace => (
is => 'rw',
isa => 'Str',
predicate => 'has_trace',
);
has _trace_fh => (
is => 'rw',
isa => 'GLOB',
);
sub BUILD {
my ($self, $args) = @_;
my $trace = $ENV{SPORE_TRACE} || $args->{trace};
return unless defined $trace;
my ($level, $fh);
if ( $trace =~ /(\d)=(.+)$/ ) {
$level = $1;
my $file = $2;
$fh = IO::File->new( $file, 'w' )
or die "Cannot open trace file $file";
}
else {
$level = $trace;
$fh = IO::File->new('>&STDERR')
or die('Duplication of STDERR for debug output failed (perhaps your STDERR is closed?)');
}
$fh->autoflush;
$self->_trace_fh($fh);
$self->trace($level);
}
sub _trace_msg {
my $self = shift;
my $template = shift;
return unless $self->has_trace;
my $fh = $self->_trace_fh();
print $fh (sprintf( $template, @_ )."\n");
}
sub _trace_verbose {
my $self = shift;
return unless $self->trace && $self->trace > 1;
$self->_trace_msg(@_);
}
1;
|