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