Skip to content

Commit

Permalink
Item14237: Added support for indenting message texts
Browse files Browse the repository at this point in the history
Useful for debugging/tracing when deep recursion takes place and it is
very helpful seeing what level a message corresponds two; or how two
messages are related to each other.
  • Loading branch information
vrurg committed Oct 29, 2017
1 parent 116e7c2 commit 304a9eb
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
20 changes: 19 additions & 1 deletion core/lib/Foswiki.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ use Exporter qw(import);
our @EXPORT_OK = qw(
%regex urlEncode urlDecode make_params load_package load_class
expandStandardEscapes findCaller findCallerByPrefix isTrue
fetchGlobal getNS
indentInc indentMsg
fetchGlobal getNS inject_code
$TRUE $FALSE
);

Expand Down Expand Up @@ -983,6 +984,23 @@ sub guessHomeDir {
return File::Spec->catpath( $v, File::Spec->catdir(@d) );
}

# --- Indentet output

sub indentObj {
state $indentObj;
require Foswiki::Util::IndentMsg;
$indentObj = Foswiki::Util::IndentMsg->new unless defined $indentObj;
return $indentObj;
}

sub indentInc {
return indentObj->incLevel;
}

sub indentMsg {
return indentObj->indent(@_);
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Expand Down
89 changes: 89 additions & 0 deletions core/lib/Foswiki/Util/IndentMsg.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# See bottom of file for license and copyright information

package Foswiki::Util::IndentMsg;

=begin TML
---+!! Class Foswiki::Util::IndentMsg
---++ SYNOPSIS
---++ DESCRIPTION
=cut

use Foswiki::Class;
extends qw<Foswiki::Object>;
with qw<Foswiki::Util::Localize>;

=begin TML
---++ ATTRIBUTES
=cut

has indentLevel => (
is => 'rw',
default => 0,
);

has indentStr => (
is => 'rw',
default => " ",
);

around setLocalizeFlags => sub {
return clearAttributes => 0;
};

=begin TML
---++ METHODS
=cut

sub incLevel {
my $this = shift;
my $holder = $this->localize;
$this->indentLevel( $this->indentLevel + 1 );
return $holder;
}

sub indent {
my $this = shift;
my $prefix = $this->indentStr x $this->indentLevel;
return join( "", map { $prefix . $_ } split /\n/, join( "", @_ ) );
}

around restore => sub {
my $orig = shift;
my $this = shift;

return $orig->( $this, @_ );
};

sub setLocalizableAttributes {
return qw<indentLevel>;
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2017 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.

0 comments on commit 304a9eb

Please sign in to comment.