# Redirecting gcc diagnostics into a file

**URL:** https://discourse.cmake.org/t/redirecting-gcc-diagnostics-into-a-file/3041
**Category:** Usage
**Created:** [March 30, 2021, 9:23am UTC](https://discourse.cmake.org/t/redirecting-gcc-diagnostics-into-a-file/3041 "2021-03-30T09:23:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Lothar\_Scholz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lothar_scholz/32/1332_2.png) [@Lothar\_Scholz](https://discourse.cmake.org/u/Lothar_Scholz)
#### Post date: [March 30, 2021, 9:23am UTC](https://discourse.cmake.org/t/redirecting-gcc-diagnostics-into-a-file/3041/1 "2021-03-30T09:23:32Z")

</div>

During my CI build i want to use -fdiagnostics-format=json to generate a nice build report.  
How can i redirect the output of every compiler call into a file?  
On a command line i would write

`gcc -c -fdiagnostics-format=json foo.c |& tee > foo.c.log`

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 30, 2021, 7:18pm UTC](https://discourse.cmake.org/t/redirecting-gcc-diagnostics-into-a-file/3041/2 "2021-03-30T19:18:55Z")

</div>

You’d need to probably edit the compiler command template to have an additional set of flags. I don’t think CMake supports shell logic like `|& tee > foo.log` in general however. I would recommend using [`CMAKE_<LANG>_COMPILER_LAUNCHER`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_LAUNCHER.html) to wrap this up in a tool that can launch the compiler in an explicit shell that handles the redirection.

---

<div class="post-metadata">

### Author: ![Lothar\_Scholz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lothar_scholz/32/1332_2.png) [@Lothar\_Scholz](https://discourse.cmake.org/u/Lothar_Scholz)
#### Post date: [March 30, 2021, 7:49pm UTC](https://discourse.cmake.org/t/redirecting-gcc-diagnostics-into-a-file/3041/3 "2021-03-30T19:49:24Z")

</div>

Thanks. Yes i think you are right.

I spend the afternoon with writing a shell command wrapper that replaces “gcc” and inject it via CC and CXX environment variables. Works very well now.

I think the feature to specify a special error file is something that must be implemented by the compiler developers and not in the build system. So i will post on the gcc/clang forum. Its time in 2021 to stop parsing output for structured data.

---

<div class="post-metadata">

### Author: ![axel](https://discourse.cmake.org/user_avatar/discourse.cmake.org/axel/32/1196_2.png) [@axel](https://discourse.cmake.org/u/axel)
#### Post date: [April 2, 2021, 5:27pm UTC](https://discourse.cmake.org/t/redirecting-gcc-diagnostics-into-a-file/3041/4 "2021-04-02T17:27:07Z")

</div>

Do you plan to publish your script?

---

<div class="post-metadata">

### Author: ![Lothar\_Scholz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lothar_scholz/32/1332_2.png) [@Lothar\_Scholz](https://discourse.cmake.org/u/Lothar_Scholz)
#### Post date: [April 3, 2021, 8:05am UTC](https://discourse.cmake.org/t/redirecting-gcc-diagnostics-into-a-file/3041/5 "2021-04-03T08:05:39Z")

</div>

```auto
#!/usr/bin/bash

# We need at least three arguments with the last "-c path_to_src.cpp"
if [$# -le 2]
then
    gcc "$@" ; exit $?
fi

# Last argument must have the right file extension, ".c"
last=${@: -1}
filename=$(basename -- "$last")
extension="${filename##*.}"
filename="${filename%.*}"
if ["$extension" != "c"]
then
    gcc "$@" ; exit $?
fi

# Find out we are compiling one of our own source files
case "$last" in
  */infosqueezer/src/*)
	     echo " " > /dev/null
    ;;
  *)
      gcc "$@" ; exit $?
    ;;
esac

# check second last argument is a -c
switch=${@: -2:1}
case "$switch" in
  -c)
	echo " " > /dev/null
    ;;
  *)
      gcc "$@" ; exit $?
    ;;
esac

out_path=${INFOSQUEEZER_BUILD_STATUS_PATH}/${filename}.${extension}.olog
err_path=${INFOSQUEEZER_BUILD_STATUS_PATH}/${filename}.${extension}.elog

gcc "$@" > >(tee ${out_path}) 2> >(tee ${err_path} >&2)

```

---

<div class="post-metadata">

### Author: ![Lothar\_Scholz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lothar_scholz/32/1332_2.png) [@Lothar\_Scholz](https://discourse.cmake.org/u/Lothar_Scholz)
#### Post date: [April 3, 2021, 8:09am UTC](https://discourse.cmake.org/t/redirecting-gcc-diagnostics-into-a-file/3041/6 "2021-04-03T08:09:59Z")

</div>

I will extend and program it in c now when i now it works and i tasted the blood of good build status information Bash is just to ugly for me and i also want to catch linking steps now and a lot of other things like timing of the individual calls.
