3 use constant NONE => 0;
4 use constant INFO => 1;
5 use constant DEBUG1 => 2;
6 use constant DEBUG2 => 4;
7 use constant QUERY => 8;
8 use constant TRACE => 16;
9 use constant ALL => 31;
11 use constant FILE_TARGET => 0;
12 use constant STDERR_TARGET => 1;
14 use POSIX qw(strftime);
16 my $data_dumper_available;
22 eval("use Data::Dumper");
23 $data_dumper_available = $@ ? 0 : 1;
33 $self->{"calldepth"} = 0;
34 $self->{"file"} = "/tmp/lx-office-debug.log";
35 $self->{"target"} = FILE_TARGET;
39 $self->{ $_[0] } = $_[1];
48 my ($self, $target, $file) = @_;
50 if ((FILE_TARGET == $target) && $file) {
51 $self->{"file"} = $file;
52 $self->{"target"} = FILE_TARGET;
54 } elsif (STDERR_TARGET == $target) {
55 $self->{"target"} = STDERR_TARGET;
60 my ($self, $level) = @_;
63 return 1 unless ($global_level & TRACE); # ignore if traces aren't active
64 return 1 if $level && !($global_level & $level); # ignore if level of trace isn't active
66 my ($package, $filename, $line, $subroutine) = caller(1);
67 my ($dummy1, $self_filename, $self_line) = caller(0);
69 my $indent = " " x $self->{"calldepth"}++;
71 if (!defined($package)) {
72 $self->_write('sub' . $level, $indent . "\\ top-level?\n");
74 $self->_write('sub' . $level, $indent
75 . "\\ ${subroutine} in "
76 . "${self_filename}:${self_line} called from "
77 . "${filename}:${line}\n");
83 my ($self, $level) = @_;
86 return 1 unless ($global_level & TRACE); # ignore if traces aren't active
87 return 1 if $level && !($global_level & $level); # ignore if level of trace isn't active
89 my ($package, $filename, $line, $subroutine) = caller(1);
90 my ($dummy1, $self_filename, $self_line) = caller(0);
92 my $indent = " " x --$self->{"calldepth"};
94 if (!defined($package)) {
95 $self->_write('sub' . $level, $indent . "/ top-level?\n");
97 $self->_write('sub' . $level, $indent . "/ ${subroutine} in " . "${self_filename}:${self_line}\n");
103 my ($self, $level, $message) = @_;
105 $self->_write(level2string($level), $message) if (($self->{"level"} | $global_level) & $level || !$level);
109 my ($self, $level, $name, $variable) = @_;
111 if ($data_dumper_available) {
112 $self->message($level, "dumping ${name}:\n" . Dumper($variable));
114 $self->message($level,
115 "dumping ${name}: Data::Dumper not available; "
116 . "variable cannot be dumped");
120 sub enable_sub_tracing {
122 $self->{level} | TRACE;
125 sub disable_sub_tracing {
127 $self->{level} & ~ TRACE;
131 my ($self, $prefix, $message) = @_;
132 my $date = strftime("%Y-%m-%d %H:%M:%S $$ ${prefix}: ", localtime(time()));
137 if ((FILE_TARGET == $self->{"target"})
138 && open(FILE, ">>" . $self->{"file"})) {
139 print(FILE "${date}${message}\n");
142 } elsif (STDERR_TARGET == $self->{"target"}) {
143 print(STDERR "${date}${message}\n");
148 # use $_[0] as a bit mask and return levelstrings separated by /
149 join '/', qw(info debug1 debug2 query trace)[ grep { (reverse split //, sprintf "%05b", $_[0])[$_] } 0..4 ]