32

Sometimes, make's output fills the screen. It's a little bit hard to identify all the warning and error message lines. I know may shell color output can help Can anyone can help me?

poolie
  • 9,289
  • 1
  • 47
  • 74
Eric guan
  • 333
  • 1
  • 3
  • 5
  • 1
    If you are only interested in error and warnings, then you can simply grep those. And grep will also color "warning" and "error" :D. – Priyank Bhatnagar Jun 22 '11 at 08:39
  • 3
    To do what Priyank suggests: `make 2>&1 | grep -E --color=always 'error|warning|$'`. The $ is there to match every line so that you see all output, but only the 'error' and 'warning' strings will be highlighted. – richardr Apr 10 '14 at 14:59
  • See also [c++ - Improving g++ output - Stack Overflow](https://stackoverflow.com/questions/5732562/improving-g-output) if you're using g++. – user202729 Jul 14 '19 at 14:23

9 Answers9

44

Have a look at colormake, found here

$ apt-cache search colormake
colormake - simple wrapper around make to colorize output

Using the power of google, I also found this bash-function.

make()
{
  pathpat="(/[^/]*)+:[0-9]+"
  ccred=$(echo -e "\033[0;31m")
  ccyellow=$(echo -e "\033[0;33m")
  ccend=$(echo -e "\033[0m")
  /usr/bin/make "$@" 2>&1 | sed -E -e "/[Ee]rror[: ]/ s%$pathpat%$ccred&$ccend%g" -e "/[Ww]arning[: ]/ s%$pathpat%$ccyellow&$ccend%g"
  return ${PIPESTATUS[0]}
}
Mort
  • 3,379
  • 1
  • 25
  • 40
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • 3
    the pathpat given here didn't quite work for me, so I used pathpat="^.*:[0-9]+" instead – Wade Mar 12 '12 at 19:42
23

I have came to this questions searching for a solution to colorize make output and then remembered a while back I have researched a good generic log colorizer and found ccze. It works with anything I throw at it from Minecraft server logs to Exim MTA.

make | ccze -A

NOTE: specifying -A option enables 'raw-ansi' otherwise some output is 'cleared' at end of run in my experience. enter image description here

Daniel Sokolowski
  • 11,982
  • 4
  • 69
  • 55
  • +1 This app is super cool. I wish there was a plugin for GNU-Make. – Dilawar Jun 25 '14 at 09:55
  • 4
    Who needs a "plugin" when you have a pipe? Being able to pipe programs (those things people now call "apps" for some reason :P) together is perhaps 'the' core of the Unix philosophy. Any proper pipe-receiving program should have no problem doing stuff with the output of GNU `make`; certainly, the ones I've just tested work great. – underscore_d Dec 15 '15 at 00:30
7

If you're an emacs user, you can use the command M-x compile. This puts the make output in a highlighted buffer, with errors acting as links to the relevant line in the source code.

Dan
  • 12,157
  • 12
  • 50
  • 84
5

Just another bash function , much concise

make()
{
  /usr/bin/make "$@" 2>&1 | sed -E -e "s/error/ $(echo -e "\\033[31m" ERROR "\\033[0m"/g)"   -e "s/warning/ $(echo -e "\\033[0;33m" WARNING "\\033[0m"/g)"
  return ${PIPESTATUS[0]}
}
badp
  • 11,409
  • 3
  • 61
  • 89
Jain Rach
  • 4,069
  • 1
  • 16
  • 25
5

How about the following?

colored output of make

It is produced by a simplified version of this Makefile.

PROJECT = programname
SHELL   = /bin/bash
OBJS    = $(patsubst src/%.cc,obj/%.o,$(wildcard src/*.cc))

RESET          = \033[0m
make_std_color = \033[3$1m      # defined for 1 through 7
make_color     = \033[38;5;$1m  # defined for 1 through 255
WRN_COLOR = $(strip $(call make_std_color,3))
ERR_COLOR = $(strip $(call make_std_color,1))
STD_COLOR = $(strip $(call make_color,8))

COLOR_OUTPUT = 2>&1 |                                   \
    while IFS='' read -r line; do                       \
        if  [[ $$line == *:[\ ]error:* ]]; then         \
            echo -e "$(ERR_COLOR)$${line}$(RESET)";     \
        elif [[ $$line == *:[\ ]warning:* ]]; then      \
            echo -e "$(WRN_COLOR)$${line}$(RESET)";     \
        else                                            \
            echo -e "$(STD_COLOR)$${line}$(RESET)";     \
        fi;                                             \
    done; exit $${PIPESTATUS[0]};

.PHONY: $(PROJECT)

$(PROJECT): bin/$(PROJECT)

bin/$(PROJECT): $(OBJS)
    @mkdir -p bin
    @echo g++ -o $@ $(OBJS) -Iinclude
    @g++ -o $@ $(OBJS) -Iinclude $(COLOR_OUTPUT)

obj/%.o: src/%.cc
    @mkdir -p obj
    @echo g++ -o $@ -c $< -Wall -Wextra
    @g++ -o $@ -c $< -Wall -Wextra $(COLOR_OUTPUT)

It assumes all C++ source files are in the src directory (extention .cc) and header files are in the include directory.

gospes
  • 3,819
  • 1
  • 28
  • 31
1

I used to use multitail for log files it can highlight (and filter) lines based on various criteria.

hlovdal
  • 26,565
  • 10
  • 94
  • 165
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
0

On Mac, it worked by printing tput color codes around the error string.

First export tput color codes as below:

export red=`tput setaf 1`
export reset=`tput sgr0`

then, add a target to Makefile as below:

...
check-env:
ifndef ENV
    $(error ${red}ENV is undefined. Please export it using command [ export ENV=dev ]${reset})
endif
...

then, run it as make check-env

Screen shot - enter image description here

Robert Ranjan
  • 1,538
  • 3
  • 19
  • 17
0

GCC has an environment variable for that:

export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
Micha
  • 61
  • 6
0

I recently looked into this for our team and I modified the source of make to produce output where CL.EXE (Windows) output get the text 'warning' and 'error' in a color using ANSI escape codes.

I dropped it on github here: https://github.com/XtheOne/make-colorized

It could be further developed by making the words that needs replacing configurable as parameter or in a file. But for now it works just fine for us.

MichelDG
  • 11
  • 2