| 1 | package Object::Remote::Logging::Router; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Scalar::Util qw(weaken); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Sys::Hostname; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | with 'Log::Contextual::Role::Router'; | ||||||||||
| 8 | with 'Object::Remote::Role::LogForwarder'; | ||||||||||
| 9 | |||||||||||
| 10 | has _connections => ( is => 'ro', required => 1, default => sub { [] } ); | ||||||||||
| 11 | has _remote_metadata => ( is => 'rw' ); | ||||||||||
| 12 | |||||||||||
| 13 | 0 | ▶ | sub before_import { } | ||||||||
|
Subroutine
before_import: not called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 0 | ▶ | sub after_import { } | ||||||||
|
Subroutine
after_import: not called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 0 | ▶ | sub _get_loggers { | ||||||||
|
Subroutine
_get_loggers: not called CC=7 SCAR=40.3
| |||||||||||
| 18 | 0 | my ($self, %metadata) = @_; | |||||||||
| 19 | 0 | my $package = $metadata{caller_package}; | |||||||||
| 20 | 0 | my $level = $metadata{message_level}; | |||||||||
| 21 | 0 | my $is_level = "is_$level"; | |||||||||
| 22 | 0 | my $need_clean = 0; | |||||||||
| 23 | 0 | my @loggers; | |||||||||
| 24 | |||||||||||
| 25 | 0 | foreach my $selector (@{$self->_connections}) { | |||||||||
| 26 | 0 | ▶ | unless(defined $selector) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 0 | $need_clean = 1; | |||||||||
| 28 | 0 | next; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 0 | foreach my $logger ($selector->($package, { %metadata })) { | |||||||||
| 32 | 0 | ▶ | next unless defined $logger; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | ▶ | next unless $logger->$is_level; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 0 | push(@loggers, $logger); | |||||||||
| 35 | } | ||||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | $self->_clean_connections if $need_clean; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | 0 | return @loggers; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | #overloadable so a router can invoke a logger | ||||||||||
| 44 | #in a different way | ||||||||||
| 45 | 0 | ▶ | sub _invoke_logger { | ||||||||
|
Subroutine
_invoke_logger: not called CC=1 SCAR=6.9
| |||||||||||
| 46 | 0 | my ($self, $logger, $level_name, $content, $metadata) = @_; | |||||||||
| 47 | #Invoking the logger like this gets all available data to the | ||||||||||
| 48 | #logging object with out losing any information from the datastructure. | ||||||||||
| 49 | #This is not a backwards compatible way to invoke the loggers | ||||||||||
| 50 | #but it enables a lot of flexibility in the logger. | ||||||||||
| 51 | #The l-c router could have this method invoke the logger in | ||||||||||
| 52 | #a backwards compatible way and router sub classes invoke | ||||||||||
| 53 | #it in non-backwards compatible ways if desired | ||||||||||
| 54 | 0 | $logger->$level_name($content, $metadata); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | #overloadable so forwarding can have the updated | ||||||||||
| 58 | #metadata but does not have to wrap get_loggers | ||||||||||
| 59 | #which has too many drawbacks | ||||||||||
| 60 | 0 | ▶ | sub _deliver_message { | ||||||||
|
Subroutine
_deliver_message: not called CC=3 SCAR=24.8
| |||||||||||
| 61 | 0 | my ($self, %message_info) = @_; | |||||||||
| 62 | 0 | my @loggers = $self->_get_loggers(%message_info); | |||||||||
| 63 | 0 | my $generator = $message_info{message_sub}; | |||||||||
| 64 | 0 | my $args = $message_info{message_args}; | |||||||||
| 65 | 0 | my $level = $message_info{message_level}; | |||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | return unless @loggers > 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | #this is the point where the user provided log message code block is executed | ||||||||||
| 69 | 0 | my @content = $generator->(@$args); | |||||||||
| 70 | 0 | foreach my $logger (@loggers) { | |||||||||
| 71 | 0 | $self->_invoke_logger($logger, $level, \@content, \%message_info); | |||||||||
| 72 | } | ||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 0 | ▶ | sub handle_log_request { | ||||||||
|
Subroutine
handle_log_request: not called CC=2 SCAR=17.9
| |||||||||||
| 76 | 0 | my ($self, %message_info) = @_; | |||||||||
| 77 | 0 | my $level = $message_info{message_level}; | |||||||||
| 78 | 0 | my $package = $message_info{caller_package}; | |||||||||
| 79 | 0 | my $need_clean = 0; | |||||||||
| 80 | |||||||||||
| 81 | #caller_level is useless when log forwarding is in place | ||||||||||
| 82 | #so we won't tempt people with using it | ||||||||||
| 83 | 0 | my $caller_level = delete $message_info{caller_level}; | |||||||||
| 84 | 0 | $message_info{object_remote} = $self->_remote_metadata; | |||||||||
| 85 | 0 | $message_info{timestamp} = time; | |||||||||
| 86 | 0 | $message_info{pid} = $$; | |||||||||
| 87 | 0 | $message_info{hostname} = hostname; | |||||||||
| 88 | |||||||||||
| 89 | 0 | my @caller_info = caller($caller_level); | |||||||||
| 90 | 0 | $message_info{filename} = $caller_info[1]; | |||||||||
| 91 | 0 | $message_info{line} = $caller_info[2]; | |||||||||
| 92 | |||||||||||
| 93 | 0 | @caller_info = caller($caller_level + 1); | |||||||||
| 94 | 0 | $message_info{method} = $caller_info[3]; | |||||||||
| 95 | 0 | ▶ | $message_info{method} =~ s/^${package}::// if defined $message_info{method}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | |||||||||||
| 97 | 0 | $self->_deliver_message(%message_info); | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 0 | ▶ | sub connect { | ||||||||
|
Subroutine
connect: not called CC=3 SCAR=24.8
| |||||||||||
| 101 | 0 | my ($self, $destination, $is_weak) = @_; | |||||||||
| 102 | 0 | my $wrapped; | |||||||||
| 103 | |||||||||||
| 104 | 0 | ▶ | if (ref($destination) ne 'CODE') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 0 | ▶ | $wrapped = sub { $destination }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 106 | } else { | ||||||||||
| 107 | 0 | $wrapped = $destination; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 0 | push(@{$self->_connections}, $wrapped); | |||||||||
| 111 | 0 | ▶ | weaken($self->_connections->[-1]) if $is_weak; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | ▶ | sub _clean_connections { | ||||||||
|
Subroutine
_clean_connections: not called CC=1 SCAR=6.9
| |||||||||||
| 115 | 0 | my ($self) = @_; | |||||||||
| 116 | 0 | @{$self->{_connections}} = grep { defined } @{$self->{_connections}}; | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 1; | ||||||||||