| 1 | package Log::Log4perl::Appender::Chunk; | ||||||||||
| 2 | 9 | $Log::Log4perl::Appender::Chunk::VERSION = '0.013'; | |||||||||
| 3 | 9 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 9 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 9 | ▶ | use Class::Load; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 9 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 9 | ▶ | use Log::Log4perl::MDC; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | |||||||||||
| 11 | # State variables: | ||||||||||
| 12 | |||||||||||
| 13 | # State can be: | ||||||||||
| 14 | # OFFCHUNK: No chunk is currently captured. | ||||||||||
| 15 | # INCHUNK: A chunk is currently captured in the buffer | ||||||||||
| 16 | # ENTERCHUNK: Entering a chunk from an OFFCHUNK state | ||||||||||
| 17 | # NEWCHUNK: Entering a NEW chunk from an INCHUNK state | ||||||||||
| 18 | # LEAVECHUNK: Leaving a chunk from an INCHUNK state | ||||||||||
| 19 | |||||||||||
| 20 | 7 | ▶ | has '_creator_pid' => ( is => 'ro', isa => 'Int', required => 1 , default => sub{ $$ ; } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 9 | has 'state' => ( is => 'rw' , isa => 'Str', default => 'OFFCHUNK' ); | |||||||||
| 23 | 9 | has 'previous_chunk' => ( is => 'rw' , isa => 'Maybe[Str]' , default => undef , writer => '_set_previous_chunk' ); | |||||||||
| 24 | 7 | ▶ | has 'messages_buffer' => ( is => 'rw' , isa => 'ArrayRef[Str]' , default => sub{ []; }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | # Settings: | ||||||||||
| 27 | 9 | has 'chunk_marker' => ( is => 'ro' , isa => 'Str', required => 1, default => 'chunk' ); | |||||||||
| 28 | |||||||||||
| 29 | # Store: | ||||||||||
| 30 | 9 | has 'store' => ( is => 'ro', isa => 'Log::Log4perl::Appender::Chunk::Store', | |||||||||
| 31 | required => 1, lazy_build => 1); | ||||||||||
| 32 | 9 | has 'store_class' => ( is => 'ro' , isa => 'Str' , default => 'Null' ); | |||||||||
| 33 | 4 | ▶ | has 'store_args' => ( is => 'ro' , isa => 'HashRef' , default => sub{ {}; }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 7 | ▶ | has 'store_builder' => ( is => 'ro' , isa => 'CodeRef', required => 1, default => sub{ | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 7 | my ($self) = @_; | |||||||||
| 37 | 7 | ▶ | sub{ | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 7 | $self->_full_store_class()->new($self->store_args()); | |||||||||
| 39 | } | ||||||||||
| 40 | 7 | }); | |||||||||
| 41 | |||||||||||
| 42 | 7 | ▶ | sub _build_store{ | ||||||||
|
Subroutine
_build_store: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 7 | my ($self) = @_; | |||||||||
| 44 | 7 | return $self->store_builder()->(); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 7 | ▶ | sub _full_store_class{ | ||||||||
|
Subroutine
_full_store_class: called CC=2 SCAR=6.9
| |||||||||||
| 48 | 7 | my ($self) = @_; | |||||||||
| 49 | 7 | my $full_class = $self->store_class(); | |||||||||
| 50 | 7 | ▶ | if( $full_class =~ /^\+/ ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 1 | $full_class =~ s/^\+//; | |||||||||
| 52 | }else{ | ||||||||||
| 53 | 6 | $full_class = 'Log::Log4perl::Appender::Chunk::Store::'.$full_class; | |||||||||
| 54 | } | ||||||||||
| 55 | 7 | Class::Load::load_class($full_class); | |||||||||
| 56 | 7 | return $full_class; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | |||||||||||
| 60 | 56 | ▶ | sub log{ | ||||||||
|
Subroutine
log: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 61 | 56 | my ($self, %params) = @_; | |||||||||
| 62 | |||||||||||
| 63 | ## Any log within this method will be discarded. | ||||||||||
| 64 | 56 | ▶ | if( Log::Log4perl::MDC->get(__PACKAGE__.'-reentrance') ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 5 | return; | |||||||||
| 66 | } | ||||||||||
| 67 | 51 | Log::Log4perl::MDC->put(__PACKAGE__.'-reentrance', 1); | |||||||||
| 68 | |||||||||||
| 69 | 51 | my $chunk = Log::Log4perl::MDC->get($self->chunk_marker()); | |||||||||
| 70 | |||||||||||
| 71 | # Change the state according to the chunk param | ||||||||||
| 72 | 51 | $self->state( $self->_compute_state($chunk) ); | |||||||||
| 73 | |||||||||||
| 74 | # Act according to the state. | ||||||||||
| 75 | 51 | my $m_name = '_on_'.$self->state(); | |||||||||
| 76 | |||||||||||
| 77 | 51 | $self->$m_name(\%params); | |||||||||
| 78 | |||||||||||
| 79 | 51 | $self->_set_previous_chunk($chunk); | |||||||||
| 80 | 51 | Log::Log4perl::MDC->put(__PACKAGE__.'-reentrance', undef); | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 9 | ▶ | sub _on_OFFCHUNK{ | ||||||||
|
Subroutine
_on_OFFCHUNK: called CC=1 SCAR=0.0
| |||||||||||
| 84 | 9 | my ($self, $params) = @_; | |||||||||
| 85 | # Chunk is Off, nothing much to do. | ||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 10 | ▶ | sub _on_ENTERCHUNK{ | ||||||||
|
Subroutine
_on_ENTERCHUNK: called CC=1 SCAR=0.0
| |||||||||||
| 89 | 10 | my ($self,$params) = @_; | |||||||||
| 90 | # Push the message in the buffer. | ||||||||||
| 91 | 10 | push @{$self->messages_buffer()} , $params->{message}; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 22 | ▶ | sub _on_INCHUNK{ | ||||||||
|
Subroutine
_on_INCHUNK: called CC=1 SCAR=0.0
| |||||||||||
| 95 | 22 | my ($self, $params) = @_; | |||||||||
| 96 | # Push the message in the buffer. | ||||||||||
| 97 | 22 | push @{$self->messages_buffer()} , $params->{message}; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 6 | ▶ | sub _on_NEWCHUNK{ | ||||||||
|
Subroutine
_on_NEWCHUNK: called CC=1 SCAR=0.0
| |||||||||||
| 101 | 6 | my ($self, $params) = @_; | |||||||||
| 102 | # Leave the chunk | ||||||||||
| 103 | 6 | $self->_on_LEAVECHUNK($params); | |||||||||
| 104 | # And we are entering the new one. | ||||||||||
| 105 | 6 | $self->_on_INCHUNK($params); | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 16 | ▶ | sub _on_LEAVECHUNK{ | ||||||||
|
Subroutine
_on_LEAVECHUNK: called CC=2 SCAR=7.1
| |||||||||||
| 109 | 16 | my ($self) = @_; | |||||||||
| 110 | |||||||||||
| 111 | # The new message should not be pushed on the buffer, | ||||||||||
| 112 | # As we left a chunk for no chunk. | ||||||||||
| 113 | |||||||||||
| 114 | # Flush the buffer in one big message. | ||||||||||
| 115 | 16 | my $big_message = join('',@{$self->{messages_buffer}}); | |||||||||
| 116 | 16 | $self->messages_buffer( [] ); | |||||||||
| 117 | |||||||||||
| 118 | # The chunk ID is in the previous chunk. This should NEVER be null | ||||||||||
| 119 | 16 | my $chunk_id = $self->previous_chunk(); | |||||||||
| 120 | 16 | ▶ | unless( defined $chunk_id ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 0 | confess("Undefined previous chunk. This should never happen. Dont know where to put the big message:$big_message"); | |||||||||
| 122 | } | ||||||||||
| 123 | 16 | $self->store->store($chunk_id, $big_message); | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | |||||||||||
| 127 | 2 | ▶ | sub DEMOLISH{ | ||||||||
|
Subroutine
DEMOLISH: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||
| 128 | 2 | my ($self) = @_; | |||||||||
| 129 | 0 | ▶ | unless( $self->_creator_pid() == $$ ){ return ; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | |||||||||||
| 131 | 2 | ▶ | if( my $chunk_id = $self->previous_chunk() ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | # Simulate transitioning to an non chunked section of the log. | ||||||||||
| 133 | 2 | Log::Log4perl::MDC->put($self->chunk_marker() , undef ); | |||||||||
| 134 | # Output an empty log. | ||||||||||
| 135 | 2 | $self->log(); | |||||||||
| 136 | } | ||||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 51 | ▶ | sub _compute_state{ | ||||||||
|
Subroutine
_compute_state: called CC=5 SCAR=16.1
| |||||||||||
| 140 | 51 | my ($self, $chunk) = @_; | |||||||||
| 141 | 51 | my $previous_chunk = $self->previous_chunk(); | |||||||||
| 142 | |||||||||||
| 143 | 51 | ▶ | if( defined $chunk ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 32 | ▶ | if( defined $previous_chunk ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | 22 | ▶ | if( $previous_chunk eq $chunk ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | # State is INCHUNK | ||||||||||
| 147 | 16 | return 'INCHUNK'; | |||||||||
| 148 | }else{ | ||||||||||
| 149 | # Chunks are different | ||||||||||
| 150 | 6 | return 'NEWCHUNK'; | |||||||||
| 151 | } | ||||||||||
| 152 | }else{ | ||||||||||
| 153 | # No previous chunk. | ||||||||||
| 154 | 10 | return 'ENTERCHUNK'; | |||||||||
| 155 | } | ||||||||||
| 156 | }else{ | ||||||||||
| 157 | # No chunk defined. | ||||||||||
| 158 | 19 | ▶ | if( defined $previous_chunk ){ # But a previous chunk | ||||||||
|
Branch: 1 branch
| |||||||||||
| 159 | 10 | return 'LEAVECHUNK'; | |||||||||
| 160 | }else{ | ||||||||||
| 161 | # No previous chunk neither | ||||||||||
| 162 | 9 | return 'OFFCHUNK'; | |||||||||
| 163 | } | ||||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | 0 | confess("UNKNOWN CASE. This should never be reached."); | |||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | 9 | __PACKAGE__->meta->make_immutable(); | |||||||||
| 170 | |||||||||||
| 171 | __END__ | ||||||||||