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.
36 use Term::ANSIColor qw(:constants :pushpop);
37 $Term::ANSIColor::AUTOLOCAL = 1;
39 my %levels = ( NONE => 0, ERROR => 1, WARNING => 2, INFO => 4, DEBUG => 8 );
41 # Message type numbers to names
43 my $prefix = $ENV{GNUNET_PREFIX} || '/usr';
44 my $filename = "$prefix/include/gnunet/gnunet_protocols.h";
45 $ipc = $opts{s} || $DEFAULT_SOCKET;
47 if (open HEADER, $filename)
51 $msgtypes{$2} = $1 if /^\s*#define\s+GNUNET_MESSAGE_TYPE_(\w+)\s+(\d+)/i;
56 Could not read $filename for message codes:
58 Please provide a \$GNUNET_PREFIX environment variable to replace "/usr".
59 Try also '$0 -h' for help
64 die "You can't read and write the socket at the same time"
65 if exists $opts{f} and exists $opts{n};
67 if ((exists $opts{n} or exists $opts{f}) and not -r $ipc) {
69 die "Could not mkfifo $ipc: $!" unless mkfifo $ipc, 0600;
70 system('chgrp', 'gnunet', $ipc);
71 die "Could not chgrp $ipc to 'gnunet': $!" if $!;
73 die "Could not chmod $ipc to allow gnunet group writes: $!" if $!;
76 if (exists $opts{n}) {
78 $msg_level = $opts{L} && exists $levels{$opts{L}} ? $levels{$opts{L}} : 0;
79 $msg_regex = $opts{m};
80 print STDERR "RE: /$msg_regex/\n" if defined $msg_regex;
81 open O, '>', $ipc or die "Cannot write to $ipc: $!";
84 if (exists $opts{f}) {
85 open(I, $ipc) or die "Cannot read from $ipc: $!";
97 my ($time, $type, $size, $from, $to, $level, $msg);
98 if (($time, $type, $size, $from, $to) =
99 /^([A-Z][a-z]{2}\ .[0-9]\ [0-9:]{8}(?:-[0-9]{6})?)\ util-.*\b
100 (?: Received | Transmitting )\ message \b.*?\b
101 type \s+ (\d+) \b.*?\b
102 size \s+ (\d+) \b.*?\b
108 my ($time, $type, $size, $from, $to) = ($1, $2, $3,
109 $4 || $name, $5 || $name);
110 my $msg = exists $msgtypes{$type} ? $msgtypes{$type} : $type;
112 print O "$time\t$from -> $to\t$msg ($size)\n";
116 if (($time, $level, $msg) =
117 /^([A-Z][a-z]{2}\ .[0-9]\ [0-9:]{8}(?:-[0-9]{6})?)
118 \s+\S+\s+(\S+)\s+(.+)/x
119 and (exists $levels{$level}
120 && $levels{$level} <= $msg_level
121 && (!defined $msg_regex || $msg =~ /$msg_regex/i)))
123 print O "$time\t$name\t$level: $msg\n";
126 return if $opts{x} and /$opts{x}/io;
127 return if $opts{i} and not /$opts{i}/io;
129 # Timestamp (e.g. Nov 01 19:36:11-384136)
130 s/^([A-Z][a-z]{2} .[0-9] [0-9:]{8}(?:-[0-9]{6})?)/YELLOW $1/e;
133 s/\b(ERROR )\b/RED $1/ex;
134 s/\b(WARNING)\b/YELLOW $1/ex;
135 s/\b(INFO )\b/GREEN $1/ex;
136 s/\b(DEBUG )\b/BRIGHT_BLACK $1/ex;
139 # TODO: might read the list from $GNUNET_PREFIX/libexec/gnunet/
140 s/\b(multicast|psyc|psycstore|social)\b/BLUE $1/gex;
142 # Add message type names
144 $1 . BRIGHT_CYAN (exists $msgtypes{$2} ? $msgtypes{$2} : 'UNKNOWN') .
148 s/(\s+)([A-Z_]+)( \(\d+\))$/$1 . BRIGHT_CYAN $2 . CYAN $3/e;