11

I am having a problem with logging to output from an automated build.

The build is done with a Makefile and the makefile utility.

The problem is that normal output like compiler command lines go to stdout and compile errors go to stderr.

I want to get the output from the build as it would show on the screen. So something like:

(stdout) CC -c file.cpp
(stderr) Compile error at file.cpp line 232, blah blah blah
(stdout) CC -c file2.cpp

What I tried (from a ksh script) is:

make -k > build.log 2> build.log

This results in a single log file but the problem is that the streams are buffered and so the result in the log file is all mixed up.

I could capture the output into 2 separate log files but then I would have no info on how to glue them back together into a single log file.

Is there a way to turn off buffering for stdout and stderr in this case?

Jeroen Dirks
  • 7,705
  • 12
  • 50
  • 70

4 Answers4

29
make -k > build.log 2>&1

This should work better for you because it is not redirecting stderr and stdout separately, but redirecting stderr to stdout, which should make the buffering sync up.

If you want to log it to a file as well as print it to the console:

make -k 2>&1 | tee build.log
Ryann Graham
  • 8,079
  • 2
  • 29
  • 32
  • Hi Ryan, It works like a charm, but I have a question, How to both log and view message at make time? Your command only log the message. Thank you – Loi Dang May 05 '15 at 04:09
  • 1
    @LoiDang updated to add an alternative that does both a file and the console. – Ryann Graham May 07 '15 at 04:28
5

Try this

make -k > build.log 2>&1
S.Lott
  • 384,516
  • 81
  • 508
  • 779
0

I could capture the output into 2 separate log files but then I would have no info on how to glue them back together into a single log file.

Gluing them back together is tricky but there is a right answer for that too, and it works! See "How do I save or redirect stdout and stderr into different files?" by Vivek Gite.

talkaboutquality
  • 1,312
  • 2
  • 16
  • 34
-2

setbuf(stdout,NULL); ->turns off stdout buffering