ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/LogCatcher.pm
Revision: 1.2
Committed: Sun Sep 6 00:13:51 2009 UTC (14 years, 9 months ago) by root
Branch: MAIN
Changes since 1.1: +0 -3 lines
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     }
83    
84     sub catch {
85     $LOGLEVEL = $_[0];
86     kil $_, "restart" for values %lport;
87     %lport = ();
88    
89     return unless defined $LOGLEVEL;
90    
91     mon_node $_, 1
92     for up_nodes;
93    
94     mon_nodes \&mon_node;
95     ()
96     }
97    
98     =back
99    
100     =head1 SEE ALSO
101    
102     L<AnyEvent::MP>.
103    
104     =head1 AUTHOR
105    
106     Marc Lehmann <schmorp@schmorp.de>
107     http://home.schmorp.de/
108    
109     =cut
110    
111     1
112