Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log make output without buffering from stdout and stderr

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?

like image 760
Jeroen Dirks Avatar asked Feb 17 '09 19:02

Jeroen Dirks


People also ask

Should logs go to stdout or stderr?

As mentioned in #6099: all log messages should go to stderr instead of stdout. command API related output should use a dedicated interface that outputs to stdout.

What is stdout logging?

The stdout. log file is a console logging file that has poor log management which causes the file to grow too large. PROBLEM DESCRIPTION. From PingFederate 9.0 onwards: 1) If an OS service start up script is being used or <pfinstall>/sbin/pingfederate-run.sh, output can be sent to stdout.

Can you close stdout?

Programs are also free to explicitly close stdout or stderr ; if they do not do so, these streams will be closed upon program termination. Closing any output stream requires flushing any data that has not yet been written to the stream.

Is stdout buffered in C?

In C, file output is block buffered. Output to stdout is line buffered. The stderr device is unbuffered.


1 Answers

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
like image 185
Ryan Graham Avatar answered Oct 07 '22 13:10

Ryan Graham