bash 如何突出显示 make 输出中的警告和错误行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6436563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:40:43  来源:igfitidea点击:

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

bashshellmakefilegnu-make

提问by Eric guan

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?

有时,make 的输出会填满整个屏幕。识别所有警告和错误消息行有点困难。我知道外壳颜色输出可以帮助任何人都可以帮助我吗?

回答by Fredrik Pihl

Have a look at colormake, found here

看看colormake,找到here

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

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

借助 google 的力量,我也找到了这个 bash 函数。

make()
{
  pathpat="(/[^/]*)+:[0-9]+"
  ccred=$(echo -e "3[0;31m")
  ccyellow=$(echo -e "3[0;33m")
  ccend=$(echo -e "3[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]}
}

回答by Daniel Sokolowski

I have came to this questions searching for a solution to colorize makeoutput 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. 它适用于我从 Minecraft 服务器日志到 Exim MTA 抛出的任何内容。

make | ccze -A

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

注意:指定 -A 选项会启用“raw-ansi”,否则根据我的经验,在运行结束时会“清除”某些输出。 enter image description here

回答by Dan

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.

如果您是 emacs 用户,则可以使用命令M-x compile. 这会将 make 输出放在突出显示的缓冲区中,错误将作为指向源代码中相关行的链接。

回答by Jain Rach

Just another bash function , much concise

只是另一个 bash 函数,非常简洁

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

回答by gospes

How about the following?

以下情况如何?

colored output of make

colored output of make

It is produced by a simplified version of this Makefile.

它是由这个 Makefile的简化版本产生的。

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

RESET          = 3[0m
make_std_color = 3[3m      # defined for 1 through 7
make_color     = 3[38;5;m  # 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 srcdirectory (extention .cc) and header files are in the includedirectory.

它假设所有 C++ 源文件都在src目录中(扩展名 .cc),头文件在include目录中。

回答by Zsolt Botykai

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

我曾经将multitail用于日志文件,它可以根据各种标准突出显示(和过滤)行。

回答by Robert Ranjan

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

在 Mac 上,它通过tput在错误字符串周围打印颜色代码来工作。

First export tputcolor codes as below:

第一个export tput颜色代码如下:

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

then, add a target to Makefile as below:

然后,将目标添加到 Makefile,如下所示:

...
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

然后,运行它 make check-env

Screen shot - enter image description here

截屏 - enter image description here