How can I highlight the warning and error lines in the make output?

て烟熏妆下的殇ゞ 提交于 2019-12-02 15:12:59
Fredrik Pihl

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]}
}

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.

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.

How about the following?

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.

Jain Rach

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]}
}
Zsolt Botykai

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

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 -

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!