ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/LogCatcher.pm
Revision: 1.3
Committed: Mon Sep 7 18:33:44 2009 UTC (14 years, 8 months ago) by root
Branch: MAIN
Changes since 1.2: +13 -0 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 root 1.3 =item AnyEvent::MP::LogCatcher::catch [$level]
85    
86     Starts catching all log messages from all nodes with level C<$level> or
87     lower. If the C<$level> is C<undef>, then stop catching all messages
88     again.
89    
90     Example: start a node that catches all messages (you might have to specify
91     a suitable profile name).
92    
93     aemp run profilename services '[["AnyEvent::MP::LogCatcher::catch",9]]'
94    
95     =cut
96    
97 root 1.1 sub catch {
98     $LOGLEVEL = $_[0];
99     kil $_, "restart" for values %lport;
100     %lport = ();
101    
102     return unless defined $LOGLEVEL;
103    
104     mon_node $_, 1
105     for up_nodes;
106    
107     mon_nodes \&mon_node;
108     ()
109     }
110    
111     =back
112    
113     =head1 SEE ALSO
114    
115     L<AnyEvent::MP>.
116    
117     =head1 AUTHOR
118    
119     Marc Lehmann <schmorp@schmorp.de>
120     http://home.schmorp.de/
121    
122     =cut
123    
124     1
125