ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/LogCatcher.pm
Revision: 1.1
Committed: Sun Sep 6 00:13:21 2009 UTC (14 years, 8 months ago) by root
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     AnyEvent::MP::LogCatcher - catch all logs from all nodes
4    
5     =head1 SYNOPSIS
6    
7     use AnyEvent::MP::LogCatcher;
8    
9     =head1 DESCRIPTION
10    
11     This relatively simple module overrides C<$AnyEvent::MP::Kernel::WARN> on
12     every node and sends all log messages to the node running this service.
13    
14     No attempt to buffer log messages on connection loss, or retransmit lost
15     messages, is done.
16    
17     =head1 GLOBALS AND FUNCTIONS
18    
19     =over 4
20    
21     =cut
22    
23     package AnyEvent::MP::LogCatcher;
24    
25     use common::sense;
26     use Carp ();
27     use POSIX ();
28    
29     use AnyEvent ();
30     use AnyEvent::Util ();
31    
32     use AnyEvent::MP;
33     use AnyEvent::MP::Kernel;
34    
35     use base "Exporter";
36    
37     $AnyEvent::MP::Kernel::WARN->(7, "starting log catcher service.");
38    
39     our $LOGLEVEL;
40     our %lport; # local logging ports
41    
42     # other nodes connect via this
43     sub connect {
44     my ($version, $rport, $loglevel) = @_;
45    
46     my $cb = sub {
47     snd $rport, @_
48     if $_[0] <= $loglevel;
49     };
50    
51     rcv $SELF, sub { };#d#
52    
53     push @AnyEvent::MP::Kernel::WARN, $cb;
54    
55     # monitor them, silently die
56     mon $rport, sub {
57     @AnyEvent::MP::Kernel::WARN = grep $_ != $cb, @AnyEvent::MP::Kernel::WARN;
58     };
59     }
60    
61     sub mon_node {
62     my ($node, $is_up) = @_;
63    
64     return unless $is_up;
65    
66     my $lport = $lport{$node} = port {
67     my ($level, $msg) = @_;
68    
69     $msg =~ s/\n$//;
70    
71     printf STDERR "%s [%s] <%d> %s\n",
72     (POSIX::strftime "%Y-%m-%d %H:%M:%S", localtime time),
73     $node,
74     $level,
75     $msg;
76     };
77    
78     # establish connection
79     my $rport = spawn $node, "AnyEvent::MP::LogCatcher::connect", 0, $lport, $LOGLEVEL;
80    
81     mon $rport, $lport;
82     mon $lport, sub {
83     warn "$lport $rport <@_>\n";#d#
84     };
85     }
86    
87     sub catch {
88     $LOGLEVEL = $_[0];
89     kil $_, "restart" for values %lport;
90     %lport = ();
91    
92     return unless defined $LOGLEVEL;
93    
94     mon_node $_, 1
95     for up_nodes;
96    
97     mon_nodes \&mon_node;
98     ()
99     }
100    
101     =back
102    
103     =head1 SEE ALSO
104    
105     L<AnyEvent::MP>.
106    
107     =head1 AUTHOR
108    
109     Marc Lehmann <schmorp@schmorp.de>
110     http://home.schmorp.de/
111    
112     =cut
113    
114     1
115