Which tool for colorizing output of javac?

2019-06-22 06:58发布

We have a hugely parallelized build process, so I frequently have to browse through large amounts of output from javac to find a build error.

To make this easier it would be nice if there were some tool that will colorize the output of javac to my terminal, highlighting errors in the code.

What tool can I use to colorize the output of javac?

3条回答
干净又极端
2楼-- · 2019-06-22 07:18

using grep with the "--color" option ?

~$ javac Test.java 2>&1 | egrep --color "^|error"
查看更多
倾城 Initia
3楼-- · 2019-06-22 07:22

I ended up using a tool called Generic Colorizer Tool and write my own configuration for colorizing the most important output. Works just fine. :)

查看更多
ら.Afraid
4楼-- · 2019-06-22 07:33

Roll your own javac error colorizer by using any regex matcher to match your text and surround it with terminal color escape codes to apply a color:

Using a readfile and substitute ideology:

#1.  Do your javac and pipe the result to a file:
javac whatever.java 2>/tmp/javac_errors.out;

#define the escape start and stop codes that your terminal 
#uses to apply foreground and background color:
let redbackground        = '\\e[48;5;196m'
let normalbackground     = '\\e[0;0m'

#iterate the lines in the saved file:
for line in readfile("/tmp/javac_errors.out")

    #Use sed, match, substitute or whatever to regex substitute 
    #the text with the text surrounded by the color escape codes
    #find and replace the text 'error:' with the same surrounded by escape codes
    let line = substitute(line, 
                          'error:',
                           redbackground . 
                           'error:' . 
                           normalbackground, 'g')

    #use echo -e flag to tell the terminal to interpret the escape codes:
    echo -e line
endfor

Works for me:

javac colorzing example

This code is the same as above, but it uses a terminal line iterator and a sed replace ideology:

#run javac pipe to file
javac whatever.java 2>/tmp/errors.out

#Define terminal color codes
redbackground='\\e[48;5;196m'
normalbackground='\\e[0;0m'

#read the file and print out each line 
filename="/tmp/errors.out" 
while read -r line; do  
    #replace error surround with escape codes 
    line=`sed "s/error:/${redbackground}error:${normalbackground}/g" <<<"$line"` 
    echo -e "$line" 
done < "$filename" 
查看更多
登录 后发表回答