2 # helper tool to make gnunet logs more readable
3 # try 'gnunet-logread -h' for usage
7 my $DEFAULT_SOCKET = '/tmp/gnunet-logread-ipc.sock';
10 my (%opts, $name, $ipc, $msg_level, $msg_regex);
11 getopts ('i:x:n:s:L:m:fh', \%opts);
15 <gnunet-service> |& $0 [<options>]
17 $0 [<options>] [<logfile>]
20 -f Follow input from IPC FIFO socket.
22 Regular screen output options:
23 -i <regex> Include only messages that match <regex>.
24 -x <regex> Exclude all messages that match <regex>.
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.
34 use Term::ANSIColor qw(:constants :pushpop);
35 $Term::ANSIColor::AUTOLOCAL = 1;
37 # Message type numbers to names
39 my $prefix = $ENV{GNUNET_PREFIX} || '/usr';
40 my $filename = "$prefix/include/gnunet/gnunet_protocols.h";
41 $ipc = $opts{s} || $DEFAULT_SOCKET;
43 if (open HEADER, $filename)
47 $msgtypes{$2} = $1 if /^\s*#define\s+GNUNET_MESSAGE_TYPE_(\w+)\s+(\d+)/i;
52 Could not read $filename for message codes:
54 Please provide a \$GNUNET_PREFIX environment variable to replace "/usr".
55 Try also '$0 -h' for help
60 my %levels = ( NONE => 0, ERROR => 1, WARNING => 2, INFO => 4, DEBUG => 8 );
63 die "You can't read and write the socket at the same time" if exists $opts{f};
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: $!";
71 if (exists $opts{f}) {
72 system('/bin/mkfifo', $ipc) unless -r $ipc;
73 open(I, $ipc) or die "Cannot read from $ipc: $!";
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
96 my ($time, $type, $size, $from, $to) = ($1, $2, $3,
97 $4 || $name, $5 || $name);
98 my $msg = exists $msgtypes{$type} ? $msgtypes{$type} : $type;
100 print O "$time\t$from -> $to\t$msg ($size)\n";
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)))
111 print O "$time\t$name\t$level: $msg\n";
114 return if $opts{x} and /$opts{x}/io;
115 return if $opts{i} and not /$opts{i}/io;
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;
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;
127 # TODO: might read the list from $GNUNET_PREFIX/libexec/gnunet/
128 s/\b(multicast|psyc|psycstore|social)\b/BLUE $1/gex;
130 # Add message type names
132 $1 . BRIGHT_CYAN (exists $msgtypes{$2} ? $msgtypes{$2} : 'UNKNOWN') .
136 s/(\s+)([A-Z_]+)( \(\d+\))$/$1 . BRIGHT_CYAN $2 . CYAN $3/e;