-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathjive_colorizer
executable file
·65 lines (58 loc) · 1.51 KB
/
jive_colorizer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/perl
# this script takes jive console output and colorizes it according to log level
# it has been rendered mostly obsolete now, as Richard has coded color output directly
# into the jive console output code, but there's some code that's worth saving here.
#
# one alternative use for it is that saved console output could be piped through it a la
# jive_colorizer 'cat /path/to/a/messagesFile'
#
# first version: bklaas 05.09
use strict;
use IO::Handle;
use IO::Pty;
use Term::ANSIColor;
my $command = shift || die "usage: jive_colorizer path/to/jive/executable";
local $Term::ANSIColor::AUTORESET = 1;
my $colors = {
INFO => 'blue',
WARN => 'yellow',
ERROR => 'red',
DEBUG => 'green',
};
my $jive = forkptycmd($command);
my @line;
my $stackTraceOn = 0;
while (<$jive>) {
chomp;
@line = split /\s+/;
# this is a log line with a known level
if ($colors->{$line[2]}) {
print colored( $_ . "\n", $colors->{$line[2]} );
$stackTraceOn = 0;
} elsif (/stack trace/ || $stackTraceOn) {
print colored( $_ . "\n", 'white on_red' );
$stackTraceOn = 1;
} else {
print colored( $_ . "\n", 'green' );
$stackTraceOn = 0;
}
}
sub forkptycmd($) {
my $cmd = shift;
my $pty = new IO::Pty;
my $pid = fork;
if ( !defined($pid) ) {
die "error forking: $!";
}
if ( $pid==0 ) {
my $slave = $pty->slave;
close($pty);
STDOUT->fdopen($slave,'>') || die $!;
STDIN->fdopen($slave,'<') || die $!;
STDERR->fdopen(\*STDOUT,'>') || die $!;
close($slave);
exec($cmd);
}
$pty->close_slave();
return $pty;
}