ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/LogCatcher.pm
Revision: 1.4
Committed: Mon Sep 7 18:42:09 2009 UTC (14 years, 10 months ago) by root
Branch: MAIN
CVS Tags: rel-1_23, rel-1_1, rel-1_2, rel-1_28, rel-1_29, rel-1_24, rel-1_26, rel-1_27, rel-1_21, rel-1_22, rel-1_30
Changes since 1.3: +2 -4 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     push @AnyEvent::MP::Kernel::WARN, $cb;
52    
53     # monitor them, silently die
54     mon $rport, sub {
55     @AnyEvent::MP::Kernel::WARN = grep $_ != $cb, @AnyEvent::MP::Kernel::WARN;
56     };
57     }
58    
59     sub mon_node {
60     my ($node, $is_up) = @_;
61    
62     return unless $is_up;
63    
64     my $lport = $lport{$node} = port {
65     my ($level, $msg) = @_;
66    
67     $msg =~ s/\n$//;
68    
69     printf STDERR "%s [%s] <%d> %s\n",
70     (POSIX::strftime "%Y-%m-%d %H:%M:%S", localtime time),
71     $node,
72     $level,
73     $msg;
74     };
75    
76     # establish connection
77 root 1.4 AnyEvent::MP::Kernel::snd_to_func $node, "AnyEvent::MP::LogCatcher::connect", 0, $lport, $LOGLEVEL;
78 root 1.1
79 root 1.4 mon $node, $lport;
80 root 1.1 }
81    
82 root 1.3 =item AnyEvent::MP::LogCatcher::catch [$level]
83    
84     Starts catching all log messages from all nodes with level C<$level> or
85     lower. If the C<$level> is C<undef>, then stop catching all messages
86     again.
87    
88     Example: start a node that catches all messages (you might have to specify
89     a suitable profile name).
90    
91     aemp run profilename services '[["AnyEvent::MP::LogCatcher::catch",9]]'
92    
93     =cut
94    
95 root 1.1 sub catch {
96     $LOGLEVEL = $_[0];
97     kil $_, "restart" for values %lport;
98     %lport = ();
99    
100     return unless defined $LOGLEVEL;
101    
102     mon_node $_, 1
103     for up_nodes;
104    
105     mon_nodes \&mon_node;
106     ()
107     }
108    
109     =back
110    
111     =head1 SEE ALSO
112    
113     L<AnyEvent::MP>.
114    
115     =head1 AUTHOR
116    
117     Marc Lehmann <schmorp@schmorp.de>
118     http://home.schmorp.de/
119    
120     =cut
121    
122     1
123