4 # gnunet-service |& gnunet-logread
5 # gnunet-logread service.log
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.
19 use Term::ANSIColor qw(:constants :pushpop);
20 $Term::ANSIColor::AUTOLOCAL = 1;
22 my (%opts, $name, $ipc, $msg_level, $msg_regex);
23 getopts ('n:i:L:m:', \%opts);
25 # Message type numbers to names
27 my $prefix = $ENV{GNUNET_PREFIX} || '/usr';
28 my $filename = "$prefix/include/gnunet/gnunet_protocols.h";
30 if (open HEADER, $filename)
34 $msgtypes{$2} = $1 if /^\s*#define\s+GNUNET_MESSAGE_TYPE_(\w+)\s+(\d+)/i;
40 warn "$filename: $!, try setting \$GNUNET_PREFIX";
43 my %levels = ( NONE => 0, ERROR => 1, WARNING => 2, INFO => 4, DEBUG => 8 );
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" if defined $msg_regex;
51 open IPC, '>', $ipc or die "$ipc: $!\n";
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
68 my ($time, $type, $size, $from, $to) = ($1, $2, $3,
69 $4 || $name, $5 || $name);
70 my $msg = exists $msgtypes{$type} ? $msgtypes{$type} : $type;
72 print IPC "$time\t$from -> $to\t$msg ($size)\n";
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)))
83 print IPC "$time\t$name\t$level: $msg\n";
87 # Timestamp (e.g. Nov 01 19:36:11-384136)
88 s/^([A-Z][a-z]{2} .[0-9] [0-9:]{8}(?:-[0-9]{6})?)/YELLOW $1/e;
91 s/\b(ERROR )\b/RED $1/ex;
92 s/\b(WARNING)\b/YELLOW $1/ex;
93 s/\b(INFO )\b/GREEN $1/ex;
94 s/\b(DEBUG )\b/BRIGHT_BLACK $1/ex;
97 # TODO: might read the list from $GNUNET_PREFIX/libexec/gnunet/
98 s/\b(multicast|psyc|psycstore|social)\b/BLUE $1/gex;
100 # Add message type names
102 $1 . BRIGHT_CYAN (exists $msgtypes{$2} ? $msgtypes{$2} : 'UNKNOWN') .
106 s/(\s+)([A-Z_]+)( \(\d+\))$/$1 . BRIGHT_CYAN $2 . CYAN $3/e;
111 fileno IPC and close IPC;