Skip to content

Commit

Permalink
Log uses Readonly during DESTROY, which might be already cleaned up t…
Browse files Browse the repository at this point in the history
…hemself during global cleanup.
  • Loading branch information
stdweird committed Oct 5, 2015
1 parent 8464203 commit 6422487
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
25 changes: 17 additions & 8 deletions src/main/perl/Log.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ use strict;
use warnings;

use CAF::Reporter qw($SYSLOG);
use parent qw(CAF::Object);
use parent qw(CAF::Object Exporter);

use LC::Exception qw (SUCCESS throw_error);
use FileHandle;
use Readonly;

Readonly my $FH => 'FH';
Readonly our $FH => 'FH';
Readonly our $FILENAME => 'FILENAME';
Readonly my $TSTAMP => 'TSTAMP';
Readonly my $FILENAME => 'FILENAME';
Readonly my $OPTS => 'OPTS';

our @EXPORT_OK = qw($FILENAME $FH);

# $FH is used during DESTROY (and close), but might be
# destroyed itself e.g. during global cleanup
my $_FH = $FH;

my $ec = LC::Exception::Context->new->will_store_all;

# TODO: the pod used to say: INHERITANCE: CAF::Reporter
Expand Down Expand Up @@ -56,14 +62,15 @@ closes the log file, returns SUCCESS on success, undef otherwise
=cut

# Called during DESTROY, use $_XYZ flavour
sub close ($)
{
my $self = shift;

return unless (defined $self->{$FH});
return unless (defined $self->{$_FH});

$self->{$FH}->close();
$self->{$FH} = undef;
$self->{$_FH}->close();
$self->{$_FH} = undef;

return SUCCESS;
}
Expand Down Expand Up @@ -191,10 +198,12 @@ Called during garbage collection. Invokes close().
=cut


# All Readonly here (and in methods called) might be
# cleaned up during global cleanup, so use the $_XYZ
# flavours here (and methods called here).
sub DESTROY {
my $self = shift;
$self->close() if (defined $self->{$FH});
$self->close() if (defined $self->{$_FH});
}

=pod
Expand Down
5 changes: 4 additions & 1 deletion src/test/perl/log.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use warnings;
use Test::More;
use Test::MockModule;
use LC::Exception qw (SUCCESS);
use CAF::Log;
use CAF::Log qw($FH $FILENAME);

mkdir('target/test');

Expand All @@ -28,6 +28,9 @@ Test all methods for C<CAF::Log>
=cut

is($FILENAME, 'FILENAME', 'Exported FILENAME');
is($FH, 'FH', 'Exported FH');

# test failures
my $ec = LC::Exception::Context->new()->will_store_errors();

Expand Down

0 comments on commit 6422487

Please sign in to comment.