3 use constant NONE => 0;
4 use constant INFO => 1;
5 use constant DEBUG1 => 1 << 1;
6 use constant DEBUG2 => 1 << 2;
7 use constant QUERY => 1 << 3;
8 use constant TRACE => 1 << 4;
9 use constant BACKTRACE_ON_ERROR => 1 << 5;
10 use constant REQUEST_TIMER => 1 << 6;
11 use constant WARN => 1 << 7;
12 use constant ALL => (1 << 8) - 1;
13 use constant DEVEL => INFO | QUERY | TRACE | BACKTRACE_ON_ERROR | REQUEST_TIMER;
15 use constant FILE_TARGET => 0;
16 use constant STDERR_TARGET => 1;
18 use POSIX qw(strftime getppid);
19 use Time::HiRes qw(gettimeofday tv_interval);
24 my $data_dumper_available;
31 eval("use Data::Dumper");
32 $data_dumper_available = $@ ? 0 : 1;
42 _init_globals_from_config();
44 $self->{"calldepth"} = 0;
45 $self->{"file"} = $file_name || "/tmp/lx-office-debug.log";
46 $self->{"target"} = FILE_TARGET;
50 $self->{ $_[0] } = $_[1];
58 my $globals_inited_from_config;
59 sub _init_globals_from_config {
60 return if $globals_inited_from_config;
61 $globals_inited_from_config = 1;
63 my $cfg = $::lx_office_conf{debug} || {};
65 $global_level = NONE() if $cfg->{global_level} =~ /NONE/;
66 foreach my $level (grep { $_} split(m/\s+/, $cfg->{global_level})) {
67 $global_level |= eval "${level}()";
70 $watch_form = $cfg->{watch_form};
71 $file_name = $cfg->{file_name} || "/tmp/lx-office-debug.log";
75 my ($self, $target, $file) = @_;
77 if ((FILE_TARGET == $target) && $file) {
78 $self->{"file"} = $file;
79 $self->{"target"} = FILE_TARGET;
81 } elsif (STDERR_TARGET == $target) {
82 $self->{"target"} = STDERR_TARGET;
88 my $level = shift || 0;
90 return 1 unless ($global_level & TRACE); # ignore if traces aren't active
91 return 1 if $level && !($global_level & $level); # ignore if level of trace isn't active
93 my ($package, $filename, $line, $subroutine) = caller(1);
94 my ($dummy1, $self_filename, $self_line) = caller(0);
96 my $indent = " " x $self->{"calldepth"}++;
97 my $time = $self->get_request_time || '';
99 if (!defined($package)) {
100 $self->_write('sub' . $level, $indent . "\\ $time top-level?\n");
102 $self->_write('sub' . $level, $indent
103 . "\\ $time ${subroutine} in "
104 . "${self_filename}:${self_line} called from "
105 . "${filename}:${line}\n");
112 my $level = shift || 0;
114 return 1 unless ($global_level & TRACE); # ignore if traces aren't active
115 return 1 if $level && !($global_level & $level); # ignore if level of trace isn't active
117 my ($package, $filename, $line, $subroutine) = caller(1);
118 my ($dummy1, $self_filename, $self_line) = caller(0);
120 my $indent = " " x --$self->{"calldepth"};
121 my $time = $self->want_request_timer ? $self->get_request_time : '';
123 if (!defined($package)) {
124 $self->_write('sub' . $level, $indent . "/ $time top-level?\n");
126 $self->_write('sub' . $level, $indent . "/ $time ${subroutine} in " . "${self_filename}:${self_line}\n");
132 my ($self, $force) = @_;
134 return 1 unless ($force || ($global_level & BACKTRACE_ON_ERROR));
136 $self->message(BACKTRACE_ON_ERROR, "Starting full caller dump:");
138 while (my ($dummy, $filename, $line, $subroutine) = caller $level) {
139 $self->message(BACKTRACE_ON_ERROR, " ${subroutine} from ${filename}:${line}");
148 my ($self, $level, $message) = @_;
150 $self->_write(level2string($level), $message) if (($self->{"level"} | $global_level) & $level || !$level);
154 my ($self, $message) = @_;
155 $self->message(WARN, $message);
159 my ($self, $level, $name, $variable) = @_;
161 if ($data_dumper_available) {
163 if ($variable && ('Form' eq ref $variable) && defined $variable->{password}) {
164 $password = $variable->{password};
165 $variable->{password} = 'X' x 8;
168 my $dumper = Data::Dumper->new([$variable]);
169 $dumper->Sortkeys(1);
170 $self->message($level, "dumping ${name}:\n" . $dumper->Dump());
172 $variable->{password} = $password if (defined $password);
174 # Data::Dumper does not reset the iterator belonging to this hash
175 # if 'Sortkeys' is true. Therefore clear the iterator manually.
176 # See "perldoc -f each".
177 if ($variable && (('HASH' eq ref $variable) || ('Form' eq ref $variable))) {
182 $self->message($level,
183 "dumping ${name}: Data::Dumper not available; "
184 . "variable cannot be dumped");
189 my ($self, $level, $name, $variable) = @_;
191 $self->message($level, "dumping ${name}:\n" . YAML::Dump($variable));
194 sub dump_sql_result {
195 my ($self, $level, $prefix, $results) = @_;
197 if (!$results || !scalar @{ $results }) {
198 $self->message($level, "Empty result set");
202 my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
204 foreach my $row (@{ $results }) {
205 map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
208 my @sorted_names = sort keys %column_lengths;
209 my $format = join '|', map { '%' . $column_lengths{$_} . 's' } @sorted_names;
211 $prefix .= ' ' if $prefix;
213 $self->message($level, $prefix . sprintf($format, @sorted_names));
214 $self->message($level, $prefix . join('+', map { '-' x $column_lengths{$_} } @sorted_names));
216 foreach my $row (@{ $results }) {
217 $self->message($level, $prefix . sprintf($format, map { $row->{$_} } @sorted_names));
219 $self->message($level, $prefix . sprintf('(%d row%s)', scalar @{ $results }, scalar @{ $results } > 1 ? 's' : ''));
222 sub enable_sub_tracing {
224 $global_level |= TRACE;
227 sub disable_sub_tracing {
229 $global_level &= ~ TRACE;
232 sub is_tracing_enabled {
234 return $global_level & TRACE;
239 my ($self, $prefix, $message) = @_;
240 my $date = strftime("%Y-%m-%d %H:%M:%S $$ [" . getppid() . "] ${prefix}: ", localtime(time()));
245 if ((FILE_TARGET == $self->{"target"})
246 && open(FILE, ">>" . $self->{"file"})) {
247 print(FILE "${date}${message}\n");
250 } elsif (STDERR_TARGET == $self->{"target"}) {
251 print(STDERR "${date}${message}\n");
257 # use $_[0] as a bit mask and return levelstrings separated by /
258 join '/', qw(info debug1 debug2 query trace error_call_trace request_timer WARNING)[ grep { (reverse split //, sprintf "%08b", $_[0])[$_] } 0..7 ]
263 return 1 unless want_request_timer();
264 $self->set_request_timer;
269 return 1 unless want_request_timer();
270 $self->_write("time", $self->get_request_time);
272 $self->{calldepth} = 0;
277 return 1 unless want_request_timer();
278 $self->_write("time", $self->get_request_time);
281 sub get_request_time {
283 return $self->want_request_timer && $self->{request_start} ? tv_interval($self->{request_start}) : undef;
286 sub set_request_timer {
288 $self->{request_start} = [gettimeofday];
291 sub want_request_timer {
292 $global_level & REQUEST_TIMER;