Skip to content

Commit

Permalink
Refactor so it's easier to patch by Progress::Any::Output::Term*
Browse files Browse the repository at this point in the history
  • Loading branch information
perlancar committed Jan 28, 2015
1 parent 432a5c4 commit 487c317
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/Log/Any/Adapter/ScreenColoredLevel.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ sub init {
emergency => 'red',
};
$self->{min_level} //= _default_level();

$self->{_fh} = $self->{stderr} ? \*STDERR : \*STDOUT;
}

sub hook_before_log {
return;
#my ($self, $msg) = @_;
}

sub hook_after_log {
my ($self, $msg) = @_;
print { $self->{_fh} } "\n" unless $msg =~ /\n\z/;
}

for my $method (Log::Any->logging_methods()) {
Expand All @@ -57,7 +69,7 @@ for my $method (Log::Any->logging_methods()) {
return if $logging_levels{$method} <
$logging_levels{$self->{min_level}};

my $nl = $msg =~ /\R\z/ ? "" : "\n";
$self->hook_before_log($msg);

if ($self->{formatter}) {
$msg = $self->{formatter}->($self, $msg);
Expand All @@ -67,11 +79,9 @@ for my $method (Log::Any->logging_methods()) {
$msg = Term::ANSIColor::colored($msg, $self->{colors}{$method});
}

if ($self->{stderr}) {
print STDERR $msg, $nl;
} else {
print $msg, $nl;
}
print { $self->{_fh} } $msg;

$self->hook_after_log($msg);
}
);
}
Expand Down

0 comments on commit 487c317

Please sign in to comment.