File Coverage

blib/lib/Class/Prevayler/CommandLogger.pm
Criterion Covered Total %
statement 29 46 63.0
branch 0 8 0.0
condition 0 6 0.0
subroutine 9 11 81.8
pod 0 2 0.0
total 38 73 52.0


line stmt bran cond sub pod time code
1              
2             package Class::Prevayler::CommandLogger;
3 1     1   5 use strict;
  1         2  
  1         32  
4 1     1   5 use warnings;
  1         2  
  1         26  
5 1     1   5 use Carp;
  1         1  
  1         53  
6 1     1   5 use File::Spec;
  1         1  
  1         31  
7              
8 1         101 use constant INSTANCE_DEFAULTS => (
9             max_file_size => 680000000, #cd-rom
10             _file_length => 0,
11 1     1   6 );
  1         2  
12              
13             BEGIN {
14 1     1   5 use Exporter ();
  1         2  
  1         40  
15 1     1   5 use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  1         2  
  1         132  
16 1     1   4118 $VERSION = 0.02;
17 1         19 @ISA = qw (Exporter);
18              
19             #Give a hoot don't pollute, do not export more than needed by default
20 1         3 @EXPORT = qw ();
21 1         2 @EXPORT_OK = qw ();
22 1         365 %EXPORT_TAGS = ();
23             use Class::MethodMaker
24 1         15 new_with_init => 'new',
25             new_hash_init => 'hash_init',
26             get_set => [
27             'serializer', 'directory', 'number', 'file_counter',
28             '_filehandle', 'max_file_size', '_file_length',
29 1     1   6 ];
  1         2  
30             }
31              
32             ########################################### main pod documentation begin ##
33             # Below is the stub of documentation for your module. You better edit it!
34              
35             =head1 NAME
36              
37             Class::Prevayler::CommandLogger - Prevayler implementation - www.prevayler.org
38              
39             =head1 DESCRIPTION
40              
41             this class is an internal part of the Class::Prevayler module.
42              
43             =head1 AUTHOR
44              
45             Nathanael Obermayer
46             CPAN ID: nathanael
47             natom-pause@smi2le.net
48             http://a.galaxy.far.far.away/modules
49              
50             =head1 COPYRIGHT
51              
52             This program is free software; you can redistribute
53             it and/or modify it under the same terms as Perl itself.
54              
55             The full text of the license can be found in the
56             LICENSE file included with this module.
57              
58              
59             =head1 SEE ALSO
60              
61             Class::Prevayler.
62              
63             =cut
64              
65             sub init {
66 0     0 0   my $self = shift;
67 0           my %values = ( INSTANCE_DEFAULTS, @_ );
68 0           $self->hash_init(%values);
69 0 0 0       ( $self->directory && $self->serializer )
70             or croak "need a directory and a serializer!";
71 0           return;
72             }
73              
74             sub write_command {
75 0     0 0   my ( $self, $cmd_obj ) = @_;
76 0           my $serialized = $self->serializer->($cmd_obj);
77 0           my $length = length($serialized);
78 0 0 0       if ( $self->_file_length + $length > $self->max_file_size
79             or not defined( $self->_filehandle ) )
80             {
81 0 0         close $self->_filehandle
82             if defined $self->_filehandle;
83 0           $self->_file_length(0);
84 0           my $filehandle;
85 0 0         open $filehandle, '>>'
86             . File::Spec->catfile( $self->directory,
87             sprintf( '%016d', $self->file_counter->reserve_number_for_command )
88             . '.commandLog' )
89             or croak "couldn't open file: $!";
90 0           $self->_filehandle($filehandle);
91             }
92 0           $self->_file_length( $self->_file_length + $length + length($length) + 2 );
93 0           my $filehandle = $self->_filehandle;
94 0           print $filehandle $length . "\n" . $serialized . "\n";
95             }
96              
97             1; #this line is important and will help the module return a true value
98             __END__