fix #4546
[oweals/gnunet.git] / contrib / gnunet-logread
1 #!/usr/bin/env perl
2 # helper tool to make gnunet logs more readable
3 # try 'gnunet-logread -h' for usage
4
5 use strict;
6 use warnings;
7 my $DEFAULT_SOCKET = '/tmp/gnunet-logread-ipc.sock';
8
9 use Getopt::Std;
10 my (%opts, $name, $ipc, $msg_level, $msg_regex);
11 getopts ('i:x:n:s:L:m:fh', \%opts);
12
13 die <<X if $opts{h};
14 Usage:
15         <gnunet-service> |& $0 [<options>]
16     or
17         $0 [<options>] [<logfile>]
18
19 Options:
20     -f                          Follow input from IPC FIFO socket.
21
22    Regular screen output options:
23     -i <regex>                  Include only messages that match <regex>.
24     -x <regex>                  Exclude all messages that match <regex>.
25
26    Options to enable message passing to IPC socket:
27     -n <component_name>         Name of this component to use for IPC logging.
28     -s </path/to/ipc.sock>      Default = $DEFAULT_SOCKET
29     -L <LOGLEVEL>               Minimum level of messages to pass on.
30                                 Log levels: NONE, ERROR, WARNING, INFO, DEBUG.
31     -m <regex>                  Only pass messages matching a regular expression.
32 X
33
34 use Term::ANSIColor qw(:constants :pushpop);
35 $Term::ANSIColor::AUTOLOCAL = 1;
36
37 # Message type numbers to names
38 my %msgtypes;
39 my $prefix = $ENV{GNUNET_PREFIX} || '/usr';
40 my $filename = "$prefix/include/gnunet/gnunet_protocols.h";
41 $ipc = $opts{s} || $DEFAULT_SOCKET;
42
43 if (open HEADER, $filename)
44 {
45     while (<HEADER>)
46     {
47         $msgtypes{$2} = $1 if /^\s*#define\s+GNUNET_MESSAGE_TYPE_(\w+)\s+(\d+)/i;
48     }
49     close HEADER;
50 } else {
51     warn <<X;
52 Could not read $filename for message codes:
53         $!.
54 Please provide a \$GNUNET_PREFIX environment variable to replace "/usr".
55 Try also '$0 -h' for help
56
57 X
58 }
59
60 my %levels = ( NONE => 0, ERROR => 1, WARNING => 2, INFO => 4, DEBUG => 8 );
61 if (exists $opts{n})
62 {
63     die "You can't read and write the socket at the same time" if exists $opts{f};
64     $name = $opts{n};
65     $msg_level = $opts{L} && exists $levels{$opts{L}} ? $levels{$opts{L}} : 0;
66     $msg_regex = $opts{m};
67     print STDERR "RE: /$msg_regex/\n" if defined $msg_regex;
68     open O, '>', $ipc or die "Cannot write to $ipc: $!";
69 }
70
71 if (exists $opts{f}) {
72     system('/bin/mkfifo', $ipc) unless -r $ipc;
73     open(I, $ipc) or die "Cannot read from $ipc: $!";
74     &perform while <I>;
75     close I;
76 } else {
77     &perform while <>;
78 }
79 fileno O and close O;
80 exit;
81
82
83 sub perform {
84     if (fileno O) {
85         my ($time, $type, $size, $from, $to, $level, $msg);
86         if (($time, $type, $size, $from, $to) =
87             /^([A-Z][a-z]{2}\ .[0-9]\ [0-9:]{8}(?:-[0-9]{6})?)\ util-.*\b
88              (?: Received | Transmitting )\ message \b.*?\b
89              type \s+ (\d+) \b.*?\b
90              size \s+ (\d+) \b.*?\b
91              (?: from \s+ (\S+)
92                | to   \s+ (\S+) ) /x)
93         {
94             $from ||= $name;
95             $to ||= $name;
96             my ($time, $type, $size, $from, $to) = ($1, $2, $3,
97                                                 $4 || $name, $5 || $name);
98             my $msg = exists $msgtypes{$type} ? $msgtypes{$type} : $type;
99             my $ofh = select O;
100             print O "$time\t$from -> $to\t$msg ($size)\n";
101             $| = 1;
102             select $ofh;
103         }
104         if (($time, $level, $msg) =
105             /^([A-Z][a-z]{2}\ .[0-9]\ [0-9:]{8}(?:-[0-9]{6})?)
106               \s+\S+\s+(\S+)\s+(.+)/x
107             and (exists $levels{$level}
108                  && $levels{$level} <= $msg_level
109                  && (!defined $msg_regex || $msg =~ /$msg_regex/i)))
110         {
111             print O "$time\t$name\t$level: $msg\n";
112         }
113     }
114     return if $opts{x} and /$opts{x}/io;
115     return if $opts{i} and not /$opts{i}/io;
116
117     # Timestamp (e.g. Nov 01 19:36:11-384136)
118     s/^([A-Z][a-z]{2} .[0-9] [0-9:]{8}(?:-[0-9]{6})?)/YELLOW $1/e;
119
120     # Log levels
121     s/\b(ERROR  )\b/RED $1/ex;
122     s/\b(WARNING)\b/YELLOW $1/ex;
123     s/\b(INFO   )\b/GREEN $1/ex;
124     s/\b(DEBUG  )\b/BRIGHT_BLACK $1/ex;
125
126     # Service names
127     # TODO: might read the list from $GNUNET_PREFIX/libexec/gnunet/
128     s/\b(multicast|psyc|psycstore|social)\b/BLUE $1/gex;
129
130     # Add message type names
131     s/(\s+type\s+)(\d+)/
132       $1 . BRIGHT_CYAN (exists $msgtypes{$2} ? $msgtypes{$2} : 'UNKNOWN') .
133       CYAN " ($2)"/gei;
134
135     # logread-ipc output
136     s/(\s+)([A-Z_]+)( \(\d+\))$/$1 . BRIGHT_CYAN $2 . CYAN $3/e;
137
138     print;
139 }
140