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 REQUEST            =>  1 << 7;
 
  12 use constant WARN               =>  1 << 8;
 
  13 use constant TRACE2             =>  1 << 9;
 
  14 use constant ALL                => (1 << 10) - 1;
 
  15 use constant DEVEL              => INFO | DEBUG1 | QUERY | TRACE | BACKTRACE_ON_ERROR | REQUEST_TIMER;
 
  17 use constant FILE_TARGET   => 0;
 
  18 use constant STDERR_TARGET => 1;
 
  21 use POSIX qw(strftime getpid);
 
  22 use Time::HiRes qw(gettimeofday tv_interval);
 
  29 my ($text_diff_available);
 
  31 our $global_level = NONE();
 
  39   _init_globals_from_config();
 
  41   $self->{"calldepth"}  = 0;
 
  42   $self->{"file"}       = $file_name || "/tmp/lx-office-debug.log";
 
  43   $self->{"target"}     = FILE_TARGET;
 
  47     $self->{ $_[0] } = $_[1];
 
  55 my $globals_inited_from_config;
 
  56 sub _init_globals_from_config {
 
  57   return if $globals_inited_from_config;
 
  58   $globals_inited_from_config = 1;
 
  60   my $cfg = $::lx_office_conf{debug} || {};
 
  62   $global_level = NONE() if $cfg->{global_level} =~ /NONE/;
 
  63   foreach my $level (grep { $_} split(m/\s+/, $cfg->{global_level})) {
 
  64     $global_level |= eval "${level}()";
 
  67   $watch_form = $cfg->{watch_form};
 
  68   $file_name  = $cfg->{file_name} || "/tmp/lx-office-debug.log";
 
  72   my ($self, $target, $file) = @_;
 
  74   if ((FILE_TARGET == $target) && $file) {
 
  75     $self->{"file"}   = $file;
 
  76     $self->{"target"} = FILE_TARGET;
 
  78   } elsif (STDERR_TARGET == $target) {
 
  79     $self->{"target"} = STDERR_TARGET;
 
  85   my $level = shift || 0;
 
  87   return 1 unless ($global_level & TRACE);          # ignore if traces aren't active
 
  88   return 1 if $level && !($global_level & TRACE2);  # ignore if level of trace isn't active
 
  90   my ($package, $filename, $line, $subroutine) = caller(1);
 
  91   my ($dummy1, $self_filename, $self_line) = caller(0);
 
  93   my $indent = " " x $self->{"calldepth"}++;
 
  94   my $time = $self->get_request_time || '';
 
  96   if (!defined($package)) {
 
  97     $self->_write('sub' . $level, $indent . "\\ $time top-level?\n");
 
  99     $self->_write('sub' . $level, $indent
 
 100                     . "\\ $time ${subroutine} in "
 
 101                     . "${self_filename}:${self_line} called from "
 
 102                     . "${filename}:${line}\n");
 
 109   my $level = shift || 0;
 
 111   return 1 unless ($global_level & TRACE);           # ignore if traces aren't active
 
 112   return 1 if $level && !($global_level & TRACE2);   # ignore if level of trace isn't active
 
 114   my ($package, $filename, $line, $subroutine) = caller(1);
 
 115   my ($dummy1, $self_filename, $self_line) = caller(0);
 
 117   my $indent = " " x --$self->{"calldepth"};
 
 118   my $time = $self->want_request_timer ? $self->get_request_time || '' : '';
 
 120   if (!defined($package)) {
 
 121     $self->_write('sub' . $level, $indent . "/ $time top-level?\n");
 
 123     $self->_write('sub' . $level, $indent . "/ $time ${subroutine} in " . "${self_filename}:${self_line}\n");
 
 129   my ($self, $force) = @_;
 
 131   return 1 unless ($force || ($global_level & BACKTRACE_ON_ERROR));
 
 133   $self->message(0, "Starting full caller dump:");
 
 135   while (my ($dummy, $filename, $line, $subroutine) = caller $level) {
 
 136     $self->message(0, "  ${subroutine} from ${filename}:${line}");
 
 145   my ($self, $level, $message) = @_;
 
 147   $self->_write(level2string($level), $message) if (($self->{"level"} | $global_level) & $level || !$level);
 
 151   my ($self, $message) = @_;
 
 152   $self->message(WARN, $message);
 
 156   my ($self, $level, $name, $variable, %options) = @_;
 
 159   if ($variable && ('Form' eq ref $variable) && defined $variable->{password}) {
 
 160     $password             = $variable->{password};
 
 161     $variable->{password} = 'X' x 8;
 
 164   my $dumper = Data::Dumper->new([$variable]);
 
 165   $dumper->Sortkeys(1);
 
 167   $dumper->$_($options{$_}) for keys %options;
 
 168   my $output = $dumper->Dump();
 
 169   $self->message($level, "dumping ${name}:\n" . $output);
 
 171   $variable->{password} = $password if (defined $password);
 
 173   # Data::Dumper does not reset the iterator belonging to this hash
 
 174   # if 'Sortkeys' is true. Therefore clear the iterator manually.
 
 175   # See "perldoc -f each".
 
 176   if ($variable && (('HASH' eq ref $variable) || ('Form' eq ref $variable))) {
 
 184   my ($self, $level, $name, $variable) = @_;
 
 186   $self->message($level, "dumping ${name}:\n" . YAML::Dump($variable));
 
 189 sub dump_sql_result {
 
 190   my ($self, $level, $prefix, $results) = @_;
 
 192   if (!$results || !scalar @{ $results }) {
 
 193     $self->message($level, "Empty result set");
 
 197   my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
 
 199   foreach my $row (@{ $results }) {
 
 200     map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
 
 203   my @sorted_names = sort keys %column_lengths;
 
 204   my $format       = join '|', map { '%' . $column_lengths{$_} . 's' } @sorted_names;
 
 206   $prefix .= ' ' if $prefix;
 
 208   $self->message($level, $prefix . sprintf($format, @sorted_names));
 
 209   $self->message($level, $prefix . join('+', map { '-' x $column_lengths{$_} } @sorted_names));
 
 211   foreach my $row (@{ $results }) {
 
 212     $self->message($level, $prefix . sprintf($format, map { $row->{$_} } @sorted_names));
 
 214   $self->message($level, $prefix . sprintf('(%d row%s)', scalar @{ $results }, scalar @{ $results } > 1 ? 's' : ''));
 
 218   my ($self, $level, $text, $object) = @_;
 
 222     $copy->{$_} = $object->$_ for $object->meta->columns;
 
 225   $self->dump($level, $text, $copy);
 
 229   my ($self, $level, $item1, $item2, %params) = @_;
 
 231   if (!$self->_load_text_diff) {
 
 232     $self->warn("Perl module Text::Diff is not available");
 
 236   my @texts = map { ref $_ ? YAML::Dump($_) : $_ } ($item1, $item2);
 
 238   $self->message($level, Text::Diff::diff(\$texts[0], \$texts[1], \%params));
 
 241 sub _load_text_diff {
 
 242   $text_diff_available = eval("use Text::Diff (); 1;") ? 1 : 0 unless defined $text_diff_available;
 
 243   return $text_diff_available;
 
 246 sub enable_sub_tracing {
 
 248   $global_level |= TRACE;
 
 251 sub disable_sub_tracing {
 
 253   $global_level &= ~ TRACE;
 
 256 sub is_tracing_enabled {
 
 258   return $global_level & TRACE;
 
 263   my ($self, $prefix, $message) = @_;
 
 264   my @now  = gettimeofday();
 
 265   my $date = strftime("%Y-%m-%d %H:%M:%S." . sprintf('%03d', int($now[1] / 1000)) . " $$ [" . getpid() . "] ${prefix}: ", localtime($now[0]));
 
 269   $self->_write_raw("${date}${message}\n");
 
 273   my ($self, $message) = @_;
 
 275   if ((FILE_TARGET == $self->{"target"})
 
 276       && open(FILE, ">>", $self->{"file"})) {
 
 277     binmode FILE, ":utf8";
 
 281   } elsif (STDERR_TARGET == $self->{"target"}) {
 
 282     print STDERR $message;
 
 288   # use $_[0] as a bit mask and return levelstrings separated by /
 
 289   join '/', qw(info debug1 debug2 query trace error_call_trace request_timer WARNING)[ grep { (reverse split //, sprintf "%08b", $_[0])[$_] } 0..7 ]
 
 294   return 1 unless want_request_timer();
 
 295   $self->set_request_timer;
 
 299   my ($self, %params) = @_;
 
 300   return 1 unless want_request_timer();
 
 302   $self->_write("time", sprintf('%f (%s/%s)', $self->get_request_time, $params{script_name}, $params{action}));
 
 304   $self->{calldepth} = 0;
 
 308   my ($self, @slurp) = @_;
 
 309   return 1 unless want_request_timer();
 
 311   my $now                    = $self->get_request_time;
 
 312   my $diff                   = int((($now - ($self->{previous_log_time} // 0)) * 10_000 + 5) / 10);
 
 313   $self->{previous_log_time} = $now;
 
 315   $self->_write("time", "${now}s Δ ${diff}ms" . (@slurp ? " (@slurp)" : ''));
 
 318 sub get_request_time {
 
 320   return $self->want_request_timer && $self->{request_start} ? tv_interval($self->{request_start}) : undef;
 
 323 sub set_request_timer {
 
 325   $self->{request_start} = [gettimeofday];
 
 328 sub want_request_timer {
 
 329   $global_level & REQUEST_TIMER;
 
 333   @_ == 2 ? $_[0]->{file} = $_[1] : $_[0]->{file};
 
 337   my ($self, $level) = @_;
 
 338   my $meth = $self->can(uc $level);
 
 339   die 'unknown level' unless $meth;
 
 344   my ($self, $level, $val) = @_;
 
 346     $global_level |=  $self->_by_name($level) if  $val;
 
 347     $global_level &= ~$self->_by_name($level) if !$val;
 
 349   return $global_level & $self->_by_name($level);
 
 352 sub is_request_logging_enabled {
 
 354   return $global_level & REQUEST;
 
 357 sub add_request_params {
 
 358   my ($self, $key, $value) = @_;
 
 359   return unless $self->is_request_logging_enabled;
 
 360   return if $key =~ /password/;
 
 362   push @{ $::request->{debug}{PARAMS} ||= [] }, [ $key => $value ];
 
 366   my ($self, $type, $controller, $action) = @_;
 
 367   return unless $self->is_request_logging_enabled;
 
 369   my $session_id = $::auth->create_or_refresh_session;
 
 371   my $template = <<EOL;
 
 372 *************************************
 
 373  $ENV{REQUEST_METHOD} $ENV{SCRIPT_NAME}    $session_id ($::myconfig{login})
 
 374    routing: $type, controller: $controller, action: $action
 
 377   $self->_write('Request', $template);
 
 379   my $params = join "\n   ", map {
 
 381   } @{ $::request->{debug}{PARAMS} || [] };
 
 383   $self->_write_raw(<<EOL);
 
 399 LXDebug - kivitendo debugging facilities
 
 403 This module provides functions for debugging kivitendo. An instance is
 
 404 always created as the global variable C<$::lxdebug> at the earliest
 
 407 Debugging is mostly logging of information. Each log function has a
 
 408 I<level> and an I<object> to be logged. The configuration file as well
 
 409 as this module's functions determine which levels get logged, and
 
 410 which file they're logged to.
 
 414 The available log levels are:
 
 420 Always output the message regardless of the active levels. Only use
 
 425 Informational, not an error, more important than C<DEBUG1>.
 
 429 Important debugging information.
 
 433 Less important debugging information that occurs often and spams the
 
 438 Log all queries executed by the L<SL::DBUtils> utility methods.
 
 442 Log sub calls and exits via the L<enter_sub>/L<leave_sub> functions,
 
 443 but only if they're called with a log level that is falsish
 
 444 (e.g. none, C<undef>, 0).
 
 448 Log sub calls and exits via the L<enter_sub>/L<leave_sub> functions
 
 449 even if they're called with a log level of 2. Will only have an effect
 
 450 if C<TRACE> is set as well.
 
 452 =item C<BACKTRACE_ON_ERROR>
 
 454 Log a stack trace when an error is output.
 
 456 =item C<REQUEST_TIMER>
 
 458 Log each request's total execution time when it finishes.
 
 470 Shortcut for C<INFO | QUERY | TRACE | BACKTRACE_ON_ERROR | REQUEST_TIMER>.
 
 476 C<SL::LXDebug> gets its configuration from the C<[debug]> section of
 
 477 the C<config/kivitendo.conf> configuration file. The available options
 
 482 =item C<global_level>
 
 484 A string of log level names that should be activated by
 
 485 default. Multiple log levels are separated by C<|>.
 
 489 A boolean (C<1> or C<0>). Turns on the C<$::form> watch facility. If
 
 490 this is enabled then any key inside C<$::form> can be monitored for
 
 491 changes. For example:
 
 493   # Start watching 'action'
 
 494   $::form->{"Watchdog::action"} = 1;
 
 495   # Stop watching 'invtotal'
 
 496   $::form->{"Watchdog::invtotal"} = 0;
 
 498 A log message is written when the watchdog is enabled for a variable
 
 499 and for each subsequent change. The log message includes the place
 
 500 (file name and line number) of the instruction changing the key.
 
 502 Note that this entails a performance penalty. Also only the keys
 
 503 themselves are monitored -- not the references they point to. E.g. the
 
 504 following would not trigger a change:
 
 506   $::form->{"Watchdog::some_hash"} = 1;
 
 508   $::form->{some_hash}->{some_value} = 42;
 
 510   $::form->{some_hash} = { something => 'else' };
 
 512 =item C<keep_temp_files>
 
 514 A boolean (C<1> or C<0>). If turned on then certain temporary files
 
 515 are not removed but kept in the C<users> directory. These include the
 
 516 temporary files used during printing, e.g. LaTeX files.
 
 520 The path and file name of the debug log file. Must be a location
 
 521 writeable by the web server process.
 
 529 =item C<enter_sub [$level]>
 
 531 =item C<leave_sub [$level]>
 
 533 Pairs of these can be put near the beginning/end of a sub. They'll
 
 534 cause a trace to be written to the log file if the C<TRACE> level is
 
 537 If C<$level> is given then the log messages will only be logged if the
 
 538 global log level C<TRACE2> is active as well.
 
 540 =item C<enable_sub_tracing>
 
 542 =item C<disable_sub_tracing>
 
 544 Enables/disables sub tracing with L<enter_sub>/L<leave_sub> temporarily.
 
 546 =item C<is_tracing_enabled>
 
 548 Returns whether or not the C<TRACE> debug level is active.
 
 550 =item C<show_backtrace [$force]>
 
 552 Logs a stack backtrace if C<$force> is trueish or if the log level
 
 553 C<BACKTRACE_ON_ERROR> is active.
 
 555 =item C<message $level, $message>
 
 557 Logs the message C<$message> if the log level C<$level> is active. The
 
 558 message will be prefixed with a word describing the log level.
 
 560 =item C<warn $message>
 
 562 Equivalent to C<message WARN(), $message>.
 
 564 =item C<dump $level, $name, $variable>
 
 566 Logs a message that the variable named C<$name> is dumped along with a
 
 567 dump of the variable C<$variable> created by the L<Data::Dumper>
 
 568 module. Will log a warning if said module is not available. Will only
 
 569 log if the log level C<$level> is active.
 
 571 =item C<dump_yaml $level, $name, $variable>
 
 573 Logs a message that the variable named C<$name> is dumped along with a
 
 574 dump of the variable C<$variable> created by the C<YAML> module. Will
 
 575 only log if the log level C<$level> is active.
 
 577 =item C<dump_sql $level, $prefix, $results>
 
 579 Dumps the result of an SQL query in tabular form. Will only log if the
 
 580 log level C<$level> is active.
 
 582 =item C<show_diff $level, $item1, $item2, %params>
 
 584 Logs a unified diff of the textual representations of C<$item1> and
 
 585 C<$item2>. Requires the module L<Text::Diff> and logs a warning if
 
 586 said module is not available.
 
 588 C<$item1> and C<$item2> are dumped via L<YAML::Dumper> before diffing
 
 589 if they're non-scalars.
 
 591 Will only log if the log level C<$level> is active.
 
 593 =item C<begin_request>
 
 599 =item C<set_request_timer>
 
 601 =item C<want_request_timer>
 
 603 Internal functions used to log the current request's exeuction time
 
 604 (log level C<REQUEST_TIMER>).
 
 606 =item C<get_request_time>
 
 608 Returns the current request's elapsed execution time in seconds.
 
 610 =item C<file [$file_name]>
 
 612 Sets and/or returns the file name this instance logs to.
 
 614 =item C<level_by_name $level[, $val]>
 
 616 Returns if a log level C<$level> is active. C<$level> is a string
 
 617 representation, not one of the level constants from above.
 
 619 If C<$val> is given then said level will be turned on (if C<$val> is
 
 620 trueish) or off (if C<$val> is falsish).
 
 630 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>,
 
 631 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>