d65ed1724dce2e21f3d2426ba7feeef0f0c6b32f
[scpubgit/Object-Remote.git] / lib / Object / Remote / Role / LogForwarder.pm
1 #This is an experimental method for working with
2 #Log::Contextual crossing Object::Remote connections
3 #transparently 
4
5 package Object::Remote::Role::LogForwarder;
6
7 use Moo::Role; 
8 use Object::Remote::Logging; 
9 use Scalar::Util qw(weaken);
10 use Carp qw(cluck);
11
12 with 'Log::Contextual::Role::Router';
13
14 #TODO re-weaken router references when object::remote
15 #weak reference operation is figured out
16
17 has child_routers => ( is => 'ro', required => 1, default => sub { [] } );
18 has parent_router => ( is => 'rw', );#weak_ref => 1 );
19
20 sub BUILD { }
21
22 after BUILD => sub {
23   my ($self) = @_; 
24 #  my $parent = $self->parent_router; 
25 #  return unless defined $parent ; 
26 #  $parent->add_child_router($self);
27 };
28
29 sub describe {
30   my ($self, $depth) = @_; 
31   $depth = -1 unless defined $depth; 
32   $depth++;
33   my $buf = "\t" x $depth . $self->description . "\n";
34   foreach my $child (@{$self->child_routers}) {
35       next unless defined $child; 
36       $buf .= $child->describe($depth);
37   }
38     
39   return $buf; 
40 }
41
42 sub add_child_router {
43   my ($self, $router) = @_;
44   push(@{ $self->child_routers }, $router);
45   #TODO re-weaken when object::remote proxied
46   #weak references is figured out
47 #   weaken(${ $self->child_routers }[-1]);
48   return; 
49 }
50
51 #sub remove_child_router {
52 #   my ($self, $description) = @_;
53 #   return delete $self->child_routers->{$description};
54 #}
55
56 after handle_log_message => sub {
57   my ($self, @args) = @_;
58   my $parent = $self->parent_router;
59       
60   return unless defined $parent;
61   $parent->handle_log_message(@args);
62 };
63
64 1;
65