POE-Declare-Log-File

 view release on metacpan or  search on metacpan

lib/POE/Declare/Log/File.pm  view on Meta::CPAN

	}

	# Connect to the handle
	$_[SELF]->post('connect');
}

sub connect : Event {
	# Create the read/write wheel on the filehandle
	$_[SELF]->{wheel} = POE::Wheel::ReadWrite->new(
		Handle       => $_[SELF]->Handle,
		Filter       => POE::Filter::Stream->new,
		AutoFlush    => 1,
		FlushedEvent => 'flush',
		ErrorEvent   => 'error',
	);
	$_[SELF]->{state} = 'IDLE';

	# Do an initial queue flush if we have anything
	if ( defined $_[SELF]->{buffer} ) {
		$_[SELF]->{state} = 'BUSY';
		$_[SELF]->post('flush');
	}

	return;
}

sub flush : Event {
	if ( defined $_[SELF]->{buffer} ) {
		# Almost all the time we should arrive here already
		# busy. But if we do arrive IDLE accidentally, set as well.
		if ( $_[SELF]->{state} eq 'IDLE' ) {
			$_[SELF]->{state} = 'BUSY';
		}

		# Merge the queued messages ourself to prevent having to use a heavier
		# POE line filter in the Read/Write wheel.
		$_[SELF]->{wheel}->put( delete $_[SELF]->{buffer} );
		$_[SELF]->{buffer} = undef;

	} else {
		# Nothing (left) to do
		if ( $_[SELF]->{state} eq 'HALT' ) {
			$_[SELF]->{state} = 'STOP';
			$_[SELF]->finish;
			$_[SELF]->ShutdownEvent;

		} else {
			$_[SELF]->{state} = 'IDLE';
		}
	}

	return;
}

sub error : Event {
	$_[SELF]->{state} = 'CRASH';

	# Prevent additional message and flush queue
	delete $_[SELF]->{buffer};

	# Clean up streaming resources
	$_[SELF]->clean;

	return;
}

sub shutdown : Event {
	my $state = $_[SELF]->{state};

	# Superfluous crash shutdown
	if ( $state eq 'CRASH' ) {
		$_[SELF]->finish;
		$_[SELF]->ShutdownEvent;
		return;
	}

	# Shutdown with nothing pending to write
	if ( $state eq 'IDLE' or $state eq 'LAZY') {
		$_[SELF]->{state} = 'STOP';
		$_[SELF]->finish;
		$_[SELF]->ShutdownEvent;
		return;
	}

	# Shutdown while writing
	if ( $state eq 'BUSY' ) {
		# Signal we want to stop as soon as the queue is empty,
		# but otherwise just wait for the natural end.
		$_[SELF]->{state} = 'HALT';
		return;
	}

	# Must be a shutdown while HALT, just keep waiting
	return;
}





######################################################################
# POE::Declare::Object Methods

sub finish {
	my $self = shift;

	# Prevent additional messages and flush the queue
	delete $self->{buffer};

	# Clean up streaming resources
	$self->clean;

	# Pass through as normal
	$self->SUPER::finish(@_);
}

sub clean {
	my $self = shift;

	# Shutdown the wheel
	if ( $self->{wheel} ) {
		$self->{wheel}->shutdown_output;
		delete $self->{wheel};
	}

	# If we opened a file, close it
	if ( $self->Filename and $self->{Handle} ) {
		close delete $self->{Handle};
	}

	return;
}





######################################################################
# Support Methods

sub _handle {
	my $self = shift;
	my $filename = $self->Filename;
	my $handle   = Symbol::gensym();
	if ( open( $handle, '>>', $filename ) ) {
		$handle->autoflush(1);
		return $handle;
	}
	Carp::croak("Failed to open $filename");
}

compile;

=pod

=head1 SUPPORT

Bugs should be always be reported via the CPAN bug tracker at

L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Declare-Log-File>

For other issues, or commercial enhancement or support, contact the author.

=head1 AUTHORS

Adam Kennedy E<lt>adamk@cpan.orgE<gt>

=head1 SEE ALSO

L<POE::Declare>, L<POE>, L<http://ali.as/>



( run in 1.224 second using v1.01-cache-2.11-cpan-39bf76dae61 )