title: How do I make a Makefile to log both command and its output to a file?
body: <p>I want to log both the command and its output to a log file. It seems easy. Just redirect stdout to the log file.  </p>
  
  <pre><code>myrule:  
    mycommand &gt;&gt; logfile  
  </code></pre>
  
  <p>But this only logs the output of the command. Not the command itself.<br>
  Do I also echo the command and redirect that output to the log file?</p>
  
  <pre><code>myrule:
    @echo mycommand &gt;&gt; logile
    mycommand &gt;&gt; logfile
  </code></pre>
  
  <p>This duplication of 'mycommand' doesn't look good and it takes up space in the recipe. Especially if the recipe is long.</p>
  
  <p>Should I create a function and call it for every command I want to log?</p>
  
  <pre><code>define log_and_run
  @echo $(1) &gt;&gt; logfile
  $(1) &gt;&gt; logfile
  endef
  
  myrule:
    $(call log_and_run,mycommand)
  </code></pre>
  
  <p>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'.  </p>
  
  <p>And what if 'mycommand' is something like</p>
  
  <pre><code>$(CC) -o $@ -Wl,--linkeroption $&lt;
  </code></pre>
  
  <p>The comma will split the command into two arguments when calling 'log_and_run'.  </p>
  
  <p>Does anybody have a solution for this that doesn't take attention away from the commands and works for arbitrary commands?  </p>