Error log of make command in Linux - makefile

I am compiling a kernel module and it has many compilation errors in it. After running "make", the errors thrown out are too many to fit in the screen. Scrolling up doesn't reach the first error. I tried capturing the errors by doing make &2 > log which didn't work (log file was empty and the error messages were still dumped on screen).
Can someone please tell me how to go about logging all the messages generated during compilation/make into a logfile?

If you want to watch it scroll past, too:
make 2>&1 | tee log
(/bin/sh, bash and related) This sends the standard error to the same place as the standard output, then pipes them through tee to capture the result and still get screen action.

Try doing:
make >&log
the & after the > tells the shell to dump both stdout and stderr to the log. This can also be used with pipes.

Related

How to pass stderr to a command stream, then back to the terminal?

I'm using bash, but perhaps most shells behave similarly in this regard. If not, then my question pertains to bash.
There's a regularly used command that always issues a spurious error message (to stderr), but MAY sometimes issue error messages that are important. I figured I could pipe stderr to grep, then use -v option to filter the offending line that's otherwise noise. Whatever passes through the filter on stderr should go right back to the original destination (presumably the user's terminal). How do I do this?
(Getting the source and editing it to make a custom version that doesn't spit out that error is obviously possible but out of the question for practical reasons.)
Output grep output to stderr.
thecommand 2> >(grep -v 'something' >&2)

Bash script - run process & send to background if good, or else

I need to start up a Golang web server and leave it running in the background from a bash script. If the script in question in syntactically correct (as it will be most of the time) this is simply a matter of issuing a
go run /path/to/index.go &
However, I have to allow for the possibility that index.go is somehow erroneous. I should explain that in Golang this for something as "trival" as importing a module that you then fail to use. In this case the go run /path/to/index.go bit will return an error message. In the terminal this would be something along the lines of
index.go:4:10: expected...
What I need to be able to do is to somehow change that command above so I can funnel any error messages into a file for examination at a later stage. I tried variants on go run /path/to/index.go >> errors.txt with the terminating & in different positions but to no avail.
I suspect that there is a bash way to do this by altering the priority of evaluation of the command via some judiciously used braces/brackets etc. However, that is way beyond my bash capabilities. I would be most obliged to anyone who might be able to help.
Update
A few minutes later... After a few more experiments I have found that this works
go run /path/to/index.go &> errors.txt &
Quite apart from the fact that I don't in fact understand why it works there remains the issue that it produces a 0 byte errors.txt file when the command goes to completion without Golang throwing up any error messages. Can someone shed light on what is going on and how it might be improved?
Taken from man bash.
Redirecting Standard Output and Standard Error
This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word.
There are two formats for redirecting standard output and standard error:
&>word
and
>&word
Of the two forms, the first is preferred. This is semantically equivalent to
>word 2>&1
Appending Standard Output and Standard Error
This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be appended to the file whose name is the expansion of word.
The format for appending standard output and standard error is:
&>>word
This is semantically equivalent to
>>word 2>&1
Narūnas K's answer covers why the &> redirection works.
The reason why the file is created anyway is because the shell creates the file before it even runs the command in question.
You can see this by trying no-such-command > file.out and seeing that even though the shell errors because no-such-command doesn't exist the file gets created (using &> on that test will get the shell's error in the file).
This is why you can't do things like sed 'pattern' file > file to edit a file in place.

Duplicating stderr compilation output into separate file

I have a project, which builds by using make, and I want to add possibility
to analyze overall state of warning messages.
My idea is change make rules in order to duplicate stderr compilation output into separate file during full rebuild. Means each time make all will be done, all output will be printed in console and in addition stderr output will be duplicating into separate file.
This warning report file will be added into repository, so that I will have possibility to compare warnings existing in repository and local warnings.
The question is how to DUPLICATE (not redirect) stderr output into separate file? Means how I should change all target in Makefile?
I know how to redirect stderr output (make all 2>warning_report.txt), but it is not
what I need. Warning messages should be both in main console output and in warning file.
I use Windows 7 as work environment but I had no any deal with Windows command line or batch files before.
Thanks in advance.
Edited:
In my case final solution looks like below:
make all 3>&1 1>&2- 2>&3- | tee.bat warning_report.txt
In this case script tee.bat, which is written in JScript for Windows, I took from link specified by PA (thanks).
What about swapping, I took it from here and here.
I don't know about windows but you can do it using tee command in Linux. tee is used to redirect STDOUT to file as well as console so you can take its advantage and check if you can solve your problem.
make all 2>&1 1>stdout.log | tee stderr.log
redirect STDERR to STDOUT, redirect STDOUT to stdout.log and all the STDERR is copied to stderr.log ans echoed on the console as well.
But the solution is not complete yet. The STDOUT is not printed on the console but only copied to the stdout.log. Try playing around the commands you will get the solution .
I just re-read your question and decided I would try to answer it.
Here's a snippet made to export stderr and display it.
#echo off
if exist stderr.error del stderr.error
this_is_not_a_command 2>stderr.error
if exist stderr.error type stderr.error & del stderr.error
This would export stderr to a file and then display the contents of that file.
Hope that helps.

Hopw to stop the window from exiting during silent installation using batch file?

I wrote a batch file which does silent installation which is working fine. But how do I read the error messages if any appear? Can I write all the error/success messages to a log file? Also is there any command to stop the window from exiting?
There's the PAUSE command, which does nothing else then printing a message (Press any key to continue . . .) and waiting till a key is pressed. That would allow you to read any messages before the window goes. Just add the command to the batch file before the end of the script and/or at other position where you need it.
You could also try redirecting messages to a file. Typically console messages are redirected by adding >filename or 1>filename to the command line.
However, that would only redirect stdout messages, while there might also be stderr ones. Particularly, error messages are often printed to stderr, although that is not a rule and third-party programs may not follow that convention. Anyway, stderr messages would need to be redirected with 2>filename put on the command line.
To redirect both and make sure they go to the same file, use 1>filename 2>&1 on the command line.
You can add the redirection either to specific commands in the script or to the batch file in general. If you redirect specific commands of which there's more than one and you want the results to be logged in the same file, you'll need to use >> instead of > on all or at least all but the first command. That's because > would rewrite the output file if it existed and >> would append to it.

Redirecting the darknet program's result to log file

I'm trying to redirect the results of darknet(yolo) program to my log file.
But there are some troubles in redirecting the result.
I tried ./darknet detect cfg/yolo.cfg yolo.weights image.jpg > log.txt, but it doesn't work.
And also other redirecting methods are not working.
I think the results of darknet program is not printed to stdout but printed to somewhere.( I don't know where it is.)
So, I have no idea to solve this problem.
Please help me...
How do I get this results in forms of file?
This appends all the standard output into a file. Remove 2>&1 if you don't want to log standard errors.
./darknet detect cfg/yolo.cfg yolo.weights image.jpg 2>&1 | tee -a log.txt
Detail info here.

Resources