a72f7232d2cac5b7a412187dc8b4b6f667423c44
[oweals/gnunet.git] / contrib / logread.pl
1 #!/usr/bin/env perl
2
3 # Usage:
4 #   gnunet-service |& gnunet-logread
5 #   gnunet-logread service.log
6
7 use strict;
8 use warnings;
9
10 use Term::ANSIColor qw(:constants :pushpop);
11 $Term::ANSIColor::AUTOLOCAL = 1;
12
13 # Message type numbers to names
14 my %msgtypes;
15 my $prefix = $ENV{GNUNET_PREFIX} || '/usr';
16 my $filename = "$prefix/include/gnunet/gnunet_protocols.h";
17
18 if (open HEADER, $filename)
19 {
20     while (<HEADER>)
21     {
22         $msgtypes{$2} = $1 if /^\s*#define\s+GNUNET_MESSAGE_TYPE_(\w+)\s+(\d+)/i;
23     }
24     close HEADER;
25 }
26 else
27 {
28     warn "$filename: $!, try setting \$GNUNET_PREFIX";
29 }
30
31 while (<>)
32 {
33     # 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;
35
36     # Log levels
37     s/\b(ERROR  )\b/RED $1/ex;
38     s/\b(WARNING)\b/YELLOW $1/ex;
39     s/\b(INFO   )\b/GREEN $1/ex;
40     s/\b(DEBUG  )\b/BRIGHT_BLACK $1/ex;
41
42     # Service names
43     # TODO: might read the list from $GNUNET_PREFIX/libexec/gnunet/
44     s/\b(multicast|psyc|psycstore|social)\b/BLUE $1/ex;
45
46     # Add message type names
47     s/(message(?:\s+of)?\s+type\s+)(\d+)/
48       $1 . BRIGHT_CYAN (exists $msgtypes{$2} ? $msgtypes{$2} : 'UNKNOWN') .
49       CYAN " ($2)"/e;
50
51     print;
52 }