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 TRACE2             =>  1 << 8;
 
  13 use constant ALL                => (1 << 9) - 1;
 
  14 use constant DEVEL              => INFO | DEBUG1 | QUERY | TRACE | BACKTRACE_ON_ERROR | REQUEST_TIMER;
 
  16 use constant FILE_TARGET   => 0;
 
  17 use constant STDERR_TARGET => 1;
 
  20 use POSIX qw(strftime getppid);
 
  21 use Time::HiRes qw(gettimeofday tv_interval);
 
  26 my ($text_diff_available);
 
  28 our $global_level = NONE();
 
  36   _init_globals_from_config();
 
  38   $self->{"calldepth"}  = 0;
 
  39   $self->{"file"}       = $file_name || "/tmp/lx-office-debug.log";
 
  40   $self->{"target"}     = FILE_TARGET;
 
  44     $self->{ $_[0] } = $_[1];
 
  52 my $globals_inited_from_config;
 
  53 sub _init_globals_from_config {
 
  54   return if $globals_inited_from_config;
 
  55   $globals_inited_from_config = 1;
 
  57   my $cfg = $::lx_office_conf{debug} || {};
 
  59   $global_level = NONE() if $cfg->{global_level} =~ /NONE/;
 
  60   foreach my $level (grep { $_} split(m/\s+/, $cfg->{global_level})) {
 
  61     $global_level |= eval "${level}()";
 
  64   $watch_form = $cfg->{watch_form};
 
  65   $file_name  = $cfg->{file_name} || "/tmp/lx-office-debug.log";
 
  69   my ($self, $target, $file) = @_;
 
  71   if ((FILE_TARGET == $target) && $file) {
 
  72     $self->{"file"}   = $file;
 
  73     $self->{"target"} = FILE_TARGET;
 
  75   } elsif (STDERR_TARGET == $target) {
 
  76     $self->{"target"} = STDERR_TARGET;
 
  82   my $level = shift || 0;
 
  84   return 1 unless ($global_level & TRACE);          # ignore if traces aren't active
 
  85   return 1 if $level && !($global_level & TRACE2);  # ignore if level of trace isn't active
 
  87   my ($package, $filename, $line, $subroutine) = caller(1);
 
  88   my ($dummy1, $self_filename, $self_line) = caller(0);
 
  90   my $indent = " " x $self->{"calldepth"}++;
 
  91   my $time = $self->get_request_time || '';
 
  93   if (!defined($package)) {
 
  94     $self->_write('sub' . $level, $indent . "\\ $time top-level?\n");
 
  96     $self->_write('sub' . $level, $indent
 
  97                     . "\\ $time ${subroutine} in "
 
  98                     . "${self_filename}:${self_line} called from "
 
  99                     . "${filename}:${line}\n");
 
 106   my $level = shift || 0;
 
 108   return 1 unless ($global_level & TRACE);           # ignore if traces aren't active
 
 109   return 1 if $level && !($global_level & TRACE2);   # ignore if level of trace isn't active
 
 111   my ($package, $filename, $line, $subroutine) = caller(1);
 
 112   my ($dummy1, $self_filename, $self_line) = caller(0);
 
 114   my $indent = " " x --$self->{"calldepth"};
 
 115   my $time = $self->want_request_timer ? $self->get_request_time || '' : '';
 
 117   if (!defined($package)) {
 
 118     $self->_write('sub' . $level, $indent . "/ $time top-level?\n");
 
 120     $self->_write('sub' . $level, $indent . "/ $time ${subroutine} in " . "${self_filename}:${self_line}\n");
 
 126   my ($self, $force) = @_;
 
 128   return 1 unless ($force || ($global_level & BACKTRACE_ON_ERROR));
 
 130   $self->message(BACKTRACE_ON_ERROR, "Starting full caller dump:");
 
 132   while (my ($dummy, $filename, $line, $subroutine) = caller $level) {
 
 133     $self->message(BACKTRACE_ON_ERROR, "  ${subroutine} from ${filename}:${line}");
 
 142   my ($self, $level, $message) = @_;
 
 144   $self->_write(level2string($level), $message) if (($self->{"level"} | $global_level) & $level || !$level);
 
 148   my ($self, $message) = @_;
 
 149   $self->message(WARN, $message);
 
 153   my ($self, $level, $name, $variable, %options) = @_;
 
 156   if ($variable && ('Form' eq ref $variable) && defined $variable->{password}) {
 
 157     $password             = $variable->{password};
 
 158     $variable->{password} = 'X' x 8;
 
 161   my $dumper = Data::Dumper->new([$variable]);
 
 162   $dumper->Sortkeys(1);
 
 164   $dumper->$_($options{$_}) for keys %options;
 
 165   my $output = $dumper->Dump();
 
 166   $self->message($level, "dumping ${name}:\n" . $output);
 
 168   $variable->{password} = $password if (defined $password);
 
 170   # Data::Dumper does not reset the iterator belonging to this hash
 
 171   # if 'Sortkeys' is true. Therefore clear the iterator manually.
 
 172   # See "perldoc -f each".
 
 173   if ($variable && (('HASH' eq ref $variable) || ('Form' eq ref $variable))) {
 
 181   my ($self, $level, $name, $variable) = @_;
 
 183   $self->message($level, "dumping ${name}:\n" . YAML::Dump($variable));
 
 186 sub dump_sql_result {
 
 187   my ($self, $level, $prefix, $results) = @_;
 
 189   if (!$results || !scalar @{ $results }) {
 
 190     $self->message($level, "Empty result set");
 
 194   my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
 
 196   foreach my $row (@{ $results }) {
 
 197     map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
 
 200   my @sorted_names = sort keys %column_lengths;
 
 201   my $format       = join '|', map { '%' . $column_lengths{$_} . 's' } @sorted_names;
 
 203   $prefix .= ' ' if $prefix;
 
 205   $self->message($level, $prefix . sprintf($format, @sorted_names));
 
 206   $self->message($level, $prefix . join('+', map { '-' x $column_lengths{$_} } @sorted_names));
 
 208   foreach my $row (@{ $results }) {
 
 209     $self->message($level, $prefix . sprintf($format, map { $row->{$_} } @sorted_names));
 
 211   $self->message($level, $prefix . sprintf('(%d row%s)', scalar @{ $results }, scalar @{ $results } > 1 ? 's' : ''));
 
 215   my ($self, $level, $item1, $item2, %params) = @_;
 
 217   if (!$self->_load_text_diff) {
 
 218     $self->warn("Perl module Text::Diff is not available");
 
 222   my @texts = map { ref $_ ? YAML::Dump($_) : $_ } ($item1, $item2);
 
 224   $self->message($level, Text::Diff::diff(\$texts[0], \$texts[1], \%params));
 
 227 sub _load_text_diff {
 
 228   $text_diff_available = eval("use Text::Diff (); 1;") ? 1 : 0 unless defined $text_diff_available;
 
 229   return $text_diff_available;
 
 232 sub enable_sub_tracing {
 
 234   $global_level |= TRACE;
 
 237 sub disable_sub_tracing {
 
 239   $global_level &= ~ TRACE;
 
 242 sub is_tracing_enabled {
 
 244   return $global_level & TRACE;
 
 249   my ($self, $prefix, $message) = @_;
 
 250   my $date = strftime("%Y-%m-%d %H:%M:%S $$ [" . getppid() . "] ${prefix}: ", localtime(time()));
 
 255   if ((FILE_TARGET == $self->{"target"})
 
 256       && open(FILE, ">>", $self->{"file"})) {
 
 257     print(FILE "${date}${message}\n");
 
 260   } elsif (STDERR_TARGET == $self->{"target"}) {
 
 261     print(STDERR "${date}${message}\n");
 
 267   # use $_[0] as a bit mask and return levelstrings separated by /
 
 268   join '/', qw(info debug1 debug2 query trace error_call_trace request_timer WARNING)[ grep { (reverse split //, sprintf "%08b", $_[0])[$_] } 0..7 ]
 
 273   return 1 unless want_request_timer();
 
 274   $self->set_request_timer;
 
 279   return 1 unless want_request_timer();
 
 280   $self->_write("time", $self->get_request_time);
 
 282   $self->{calldepth} = 0;
 
 286   my ($self, @slurp) = @_;
 
 287   return 1 unless want_request_timer();
 
 288   $self->_write("time", $self->get_request_time() . (@slurp ? " (@slurp)" : ''));
 
 291 sub get_request_time {
 
 293   return $self->want_request_timer && $self->{request_start} ? tv_interval($self->{request_start}) : undef;
 
 296 sub set_request_timer {
 
 298   $self->{request_start} = [gettimeofday];
 
 301 sub want_request_timer {
 
 302   $global_level & REQUEST_TIMER;
 
 306   @_ == 2 ? $_[0]->{file} = $_[1] : $_[0]->{file};
 
 310   my ($self, $level) = @_;
 
 311   my $meth = $self->can(uc $level);
 
 312   die 'unknown level' unless $meth;
 
 317   my ($self, $level, $val) = @_;
 
 319     $global_level |=  $self->_by_name($level) if  $val;
 
 320     $global_level &= ~$self->_by_name($level) if !$val;
 
 322   return $global_level & $self->_by_name($level);
 
 334 LXDebug - kivitendo debugging facilities
 
 338 This module provides functions for debugging kivitendo. An instance is
 
 339 always created as the global variable C<$::lxdebug> at the earliest
 
 342 Debugging is mostly logging of information. Each log function has a
 
 343 I<level> and an I<object> to be logged. The configuration file as well
 
 344 as this module's functions determine which levels get logged, and
 
 345 which file they're logged to.
 
 349 The available log levels are:
 
 355 Always output the message regardless of the active levels. Only use
 
 360 Informational, not an error, more important than C<DEBUG1>.
 
 364 Important debugging information.
 
 368 Less important debugging information that occurs often and spams the
 
 373 Log all queries executed by the L<SL::DBUtils> utility methods.
 
 377 Log sub calls and exits via the L<enter_sub>/L<leave_sub> functions,
 
 378 but only if they're called with a log level that is falsish
 
 379 (e.g. none, C<undef>, 0).
 
 383 Log sub calls and exits via the L<enter_sub>/L<leave_sub> functions
 
 384 even if they're called with a log level of 2. Will only have an effect
 
 385 if C<TRACE> is set as well.
 
 387 =item C<BACKTRACE_ON_ERROR>
 
 389 Log a stack trace when an error is output.
 
 391 =item C<REQUEST_TIMER>
 
 393 Log each request's total execution time when it finishes.
 
 405 Shortcut for C<INFO | QUERY | TRACE | BACKTRACE_ON_ERROR | REQUEST_TIMER>.
 
 411 C<SL::LXDebug> gets its configuration from the C<[debug]> section of
 
 412 the C<config/kivitendo.conf> configuration file. The available options
 
 417 =item C<global_level>
 
 419 A string of log level names that should be activated by
 
 420 default. Multiple log levels are separated by C<|>.
 
 424 A boolean (C<1> or C<0>). Turns on the C<$::form> watch facility. If
 
 425 this is enabled then any key inside C<$::form> can be monitored for
 
 426 changes. For example:
 
 428   # Start watching 'action'
 
 429   $::form->{"Watchdog::action"} = 1;
 
 430   # Stop watching 'invtotal'
 
 431   $::form->{"Watchdog::invtotal"} = 0;
 
 433 A log message is written when the watchdog is enabled for a variable
 
 434 and for each subsequent change. The log message includes the place
 
 435 (file name and line number) of the instruction changing the key.
 
 437 Note that this entails a performance penalty. Also only the keys
 
 438 themselves are monitored -- not the references they point to. E.g. the
 
 439 following would not trigger a change:
 
 441   $::form->{"Watchdog::some_hash"} = 1;
 
 443   $::form->{some_hash}->{some_value} = 42;
 
 445   $::form->{some_hash} = { something => 'else' };
 
 447 =item C<keep_temp_files>
 
 449 A boolean (C<1> or C<0>). If turned on then certain temporary files
 
 450 are not removed but kept in the C<users> directory. These include the
 
 451 temporary files used during printing, e.g. LaTeX files.
 
 455 The path and file name of the debug log file. Must be a location
 
 456 writeable by the web server process.
 
 464 =item C<enter_sub [$level]>
 
 466 =item C<leave_sub [$level]>
 
 468 Pairs of these can be put near the beginning/end of a sub. They'll
 
 469 cause a trace to be written to the log file if the C<TRACE> level is
 
 472 If C<$level> is given then the log messages will only be logged if the
 
 473 global log level C<TRACE2> is active as well.
 
 475 =item C<enable_sub_tracing>
 
 477 =item C<disable_sub_tracing>
 
 479 Enables/disables sub tracing with L<enter_sub>/L<leave_sub> temporarily.
 
 481 =item C<is_tracing_enabled>
 
 483 Returns whether or not the C<TRACE> debug level is active.
 
 485 =item C<show_backtrace [$force]>
 
 487 Logs a stack backtrace if C<$force> is trueish or if the log level
 
 488 C<BACKTRACE_ON_ERROR> is active.
 
 490 =item C<message $level, $message>
 
 492 Logs the message C<$message> if the log level C<$level> is active. The
 
 493 message will be prefixed with a word describing the log level.
 
 495 =item C<warn $message>
 
 497 Equivalent to C<message WARN(), $message>.
 
 499 =item C<dump $level, $name, $variable>
 
 501 Logs a message that the variable named C<$name> is dumped along with a
 
 502 dump of the variable C<$variable> created by the L<Data::Dumper>
 
 503 module. Will log a warning if said module is not available. Will only
 
 504 log if the log level C<$level> is active.
 
 506 =item C<dump_yaml $level, $name, $variable>
 
 508 Logs a message that the variable named C<$name> is dumped along with a
 
 509 dump of the variable C<$variable> created by the C<YAML> module. Will
 
 510 only log if the log level C<$level> is active.
 
 512 =item C<dump_sql $level, $prefix, $results>
 
 514 Dumps the result of an SQL query in tabular form. Will only log if the
 
 515 log level C<$level> is active.
 
 517 =item C<show_diff $level, $item1, $item2, %params>
 
 519 Logs a unified diff of the textual representations of C<$item1> and
 
 520 C<$item2>. Requires the module L<Text::Diff> and logs a warning if
 
 521 said module is not available.
 
 523 C<$item1> and C<$item2> are dumped via L<YAML::Dumper> before diffing
 
 524 if they're non-scalars.
 
 526 Will only log if the log level C<$level> is active.
 
 528 =item C<begin_request>
 
 534 =item C<set_request_timer>
 
 536 =item C<want_request_timer>
 
 538 Internal functions used to log the current request's exeuction time
 
 539 (log level C<REQUEST_TIMER>).
 
 541 =item C<get_request_time>
 
 543 Returns the current request's elapsed execution time in seconds.
 
 545 =item C<file [$file_name]>
 
 547 Sets and/or returns the file name this instance logs to.
 
 549 =item C<level_by_name $level[, $val]>
 
 551 Returns if a log level C<$level> is active. C<$level> is a string
 
 552 representation, not one of the level constants from above.
 
 554 If C<$val> is given then said level will be turned on (if C<$val> is
 
 555 trueish) or off (if C<$val> is falsish).
 
 565 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>,
 
 566 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>