diff options
Diffstat (limited to 'contrib/logread.pl')
-rwxr-xr-x | contrib/logread.pl | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/contrib/logread.pl b/contrib/logread.pl index a72f7232d..c6f82a68d 100755 --- a/contrib/logread.pl +++ b/contrib/logread.pl | |||
@@ -3,13 +3,25 @@ | |||
3 | # Usage: | 3 | # Usage: |
4 | # gnunet-service |& gnunet-logread | 4 | # gnunet-service |& gnunet-logread |
5 | # gnunet-logread service.log | 5 | # gnunet-logread service.log |
6 | # | ||
7 | # Options: | ||
8 | # -n <component_name> Name of this component to use for IPC logging. | ||
9 | # -i </path/to/ipc.sock> Path to IPC logging socket. | ||
10 | # Passing on log messages to IPC socket: | ||
11 | # -L <LOGLEVEL> Minimum level of messages to pass on. | ||
12 | # Log levels: NONE, ERROR, WARNING, INFO, DEBUG. | ||
13 | # -m <regex> Only pass on messages matching a regular expression. | ||
6 | 14 | ||
7 | use strict; | 15 | use strict; |
8 | use warnings; | 16 | use warnings; |
9 | 17 | ||
18 | use Getopt::Std; | ||
10 | use Term::ANSIColor qw(:constants :pushpop); | 19 | use Term::ANSIColor qw(:constants :pushpop); |
11 | $Term::ANSIColor::AUTOLOCAL = 1; | 20 | $Term::ANSIColor::AUTOLOCAL = 1; |
12 | 21 | ||
22 | my (%opts, $name, $ipc, $msg_level, $msg_regex); | ||
23 | getopts ('n:i:L:m:', \%opts); | ||
24 | |||
13 | # Message type numbers to names | 25 | # Message type numbers to names |
14 | my %msgtypes; | 26 | my %msgtypes; |
15 | my $prefix = $ENV{GNUNET_PREFIX} || '/usr'; | 27 | my $prefix = $ENV{GNUNET_PREFIX} || '/usr'; |
@@ -28,8 +40,50 @@ else | |||
28 | warn "$filename: $!, try setting \$GNUNET_PREFIX"; | 40 | warn "$filename: $!, try setting \$GNUNET_PREFIX"; |
29 | } | 41 | } |
30 | 42 | ||
43 | my %levels = ( NONE => 0, ERROR => 1, WARNING => 2, INFO => 4, DEBUG => 8 ); | ||
44 | if (exists $opts{n}) | ||
45 | { | ||
46 | $name = $opts{n}; | ||
47 | $ipc = $opts{i} || '/tmp/gnunet-logread-ipc.sock'; | ||
48 | $msg_level = exists $levels{$opts{L}} ? $levels{$opts{L}} : 0; | ||
49 | $msg_regex = $opts{m}; | ||
50 | print STDERR "RE: /$msg_regex/\n"; | ||
51 | open IPC, '>', $ipc or die "$ipc: $!\n"; | ||
52 | } | ||
53 | |||
31 | while (<>) | 54 | while (<>) |
32 | { | 55 | { |
56 | if (fileno IPC) { | ||
57 | my ($time, $type, $size, $from, $to, $level, $msg); | ||
58 | if (($time, $type, $size, $from, $to) = | ||
59 | /^([A-Z][a-z]{2}\ .[0-9]\ [0-9:]{8}(?:-[0-9]{6})?)\ util-.*\b | ||
60 | (?: Received | Transmitting )\ message \b.*?\b | ||
61 | type \s+ (\d+) \b.*?\b | ||
62 | size \s+ (\d+) \b.*?\b | ||
63 | (?: from \s+ (\S+) | ||
64 | | to \s+ (\S+) ) /x) | ||
65 | { | ||
66 | $from ||= $name; | ||
67 | $to ||= $name; | ||
68 | my ($time, $type, $size, $from, $to) = ($1, $2, $3, | ||
69 | $4 || $name, $5 || $name); | ||
70 | my $msg = exists $msgtypes{$type} ? $msgtypes{$type} : $type; | ||
71 | my $ofh = select IPC; | ||
72 | print IPC "$time\t$from -> $to\t$msg ($size)\n"; | ||
73 | $|++; | ||
74 | select $ofh; | ||
75 | } | ||
76 | if (($time, $level, $msg) = | ||
77 | /^([A-Z][a-z]{2}\ .[0-9]\ [0-9:]{8}(?:-[0-9]{6})?) | ||
78 | \s+\S+\s+(\S+)\s+(.+)/x | ||
79 | and (exists $levels{$level} | ||
80 | && $levels{$level} <= $msg_level | ||
81 | && (!defined $msg_regex || $msg =~ /$msg_regex/i))) | ||
82 | { | ||
83 | print IPC "$time\t$name\t$level: $msg\n"; | ||
84 | } | ||
85 | } | ||
86 | |||
33 | # Timestamp (e.g. Nov 01 19:36:11-384136) | 87 | # Timestamp (e.g. Nov 01 19:36:11-384136) |
34 | s/^([A-Z][a-z]{2} .[0-9] [0-9:]{8}(?:-[0-9]{6})?)/YELLOW $1/e; | 88 | s/^([A-Z][a-z]{2} .[0-9] [0-9:]{8}(?:-[0-9]{6})?)/YELLOW $1/e; |
35 | 89 | ||
@@ -50,3 +104,5 @@ while (<>) | |||
50 | 104 | ||
51 | print; | 105 | print; |
52 | } | 106 | } |
107 | |||
108 | fileno IPC and close IPC; | ||