# 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 3 of the License, or
# (at your option) any later version.
#
# 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. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
package CrashTest::StackFilter;
use Mojo::Base -base;
use Mojo::Loader qw/load_class find_modules/;
has [ qw/config app filters/ ];
sub new {
my $self = shift->SUPER::new(@_);
if(defined($self->config->{StackFilters})) {
$self->load_plugins($self->config->{StackFilters});
} else {
$self->find_stackfilters_plugins();
}
return $self;
}
sub apply {
my ($self, $thread) = @_;
foreach my $filter(@{$self->filters}) {
#say "apply filter $filter";
$thread = $filter->apply($thread);
}
return $thread;
}
sub load_plugins {
my ($self, $modules) = @_;
my @filters = ();
for my $module (@$modules) {
my $e = load_class($module);
die qq{Loading "$module" failed: $e} if ref $e;
#say "loading $module";
push @filters, $module->new(config => $self->config, app => $self->app);
}
$self->filters(\@filters);
}
sub find_stackfilters_plugins {
my ($self) = @_;
my @modules = ();
# Find modules in a namespace
for my $module (find_modules('CrashTest::StackFilters')) {
my $e = load_class($module);
warn qq{Loading "$module" failed: $e} and next if ref $e;
push @modules, [ $module, $module->priority ];
}
# sort by prio
@modules = sort { $b->[1] <=> $a->[1] } @modules;
# instanciate
my @filters = map { $_->[0]->new(config => $self->config, app => $self->app) } @modules;
$self->filters(\@filters);
}
1;