Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a Makefile to log both command and its output to a file?

I want to log both the command and its output to a log file. It seems easy. Just redirect stdout to the log file.

myrule:  
  mycommand >> logfile  

But this only logs the output of the command. Not the command itself.
Do I also echo the command and redirect that output to the log file?

myrule:
  @echo mycommand >> logile
  mycommand >> logfile

This duplication of 'mycommand' doesn't look good and it takes up space in the recipe. Especially if the recipe is long.

Should I create a function and call it for every command I want to log?

define log_and_run
@echo $(1) >> logfile
$(1) >> logfile
endef

myrule:
  $(call log_and_run,mycommand)

Now I don't have to duplicate 'mycommand'. But now 'mycommand' is less visible in the recipe. Attention is on the 'call', not on 'mycommand'.

And what if 'mycommand' is something like

$(CC) -o $@ -Wl,--linkeroption $<

The comma will split the command into two arguments when calling 'log_and_run'.

Does anybody have a solution for this that doesn't take attention away from the commands and works for arbitrary commands?

like image 222
rAAbert Avatar asked Nov 14 '25 20:11

rAAbert


1 Answers

Let the shell do all the heavy lifting, maybe? I.e. something like this:

.PHONY: all myrule myrule-with-macro
all: myrule myrule-with-macro

mycommand = echo "Running $@..."

myrule:
    (set -x; $(mycommand)) >>[email protected] 2>&1

define log_and_run
(set -x; $(1)) >>[email protected] 2>&1
endef

myrule-with-macro:
    $(call log_and_run,$(mycommand))

Test run:

$ make
(set -x; echo "Running myrule...") >>myrule.log 2>&1
(set -x; echo "Running myrule-with-macro...") >>myrule-with-macro.log 2>&1

$ ls myrule*.log
myrule.log  myrule-with-macro.log

$ cat myrule*.log
+ echo 'Running myrule...'
Running myrule...
+ echo 'Running myrule-with-macro...'
Running myrule-with-macro...
like image 134
Stefan Becker Avatar answered Nov 17 '25 10:11

Stefan Becker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!