4

Currently I have a make file that will build my software using gnumake and currently I redirect my output text (like build logs, warnings, errors) in a file. But now I think it's very helpful to also display all the output text while building and while redirecting the output text in a file. Below is my current command,

gnumake -f Build.mak  1>Logs.txt 2>>&1

Is it possible to display the output text while redirecting the output text in a file?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
domlao
  • 15,663
  • 34
  • 95
  • 134
  • 1
    Don't know about `dos`, but in unix land this is done by `tee`, i.e. `make | tee logfile` – mvds Sep 08 '11 at 00:29
  • 1
    possible duplicate of [How do I echo and send console output to a file in a bat script?](http://stackoverflow.com/questions/503846/how-do-i-echo-and-send-console-output-to-a-file-in-a-bat-script) – fejese Sep 05 '15 at 18:22

4 Answers4

4

I know this is a old post, but was never answered correctly. Here is one answer for other future viewers. You don't need tee to be able to log a file and show the text. There is another way.

This question was answered on StackOverflow. Here are the 2 links:

If you don't want to use tee check this: When adding a TextLog to Echo in Command Line, Input wont show up

For tee you can see this: Displaying Windows command prompt output and redirecting it to a file

Just going to throw the answer here.

In dos yo can use this without using tee.

set LogFile=path\logfile.txt
set TempLog=path\temp
set logg=^> %TempLog%^&^& type %TempLog%^&^&type %TempLog%^>^>%LogFile%

If you need to create directory or/and logfile.txt do this.

if not exist "path" mkdir "path" >>nul
echo. 2>  %LogFile%  >nul
echo. 2>  %TempLog%  >nul

Now just use echo like this:

echo This will show me a text and logg  %logg%
Community
  • 1
  • 1
Avrumi Sherman
  • 341
  • 2
  • 12
2

if you install cygwin (unix tools on ms windows), you can use the "tee" command: gnumake -f Build.mak | tee Logs.txt

(this will save the output to Logs.txt and at the same time show the output to the console).

David Portabella
  • 12,390
  • 27
  • 101
  • 182
0

Enhanced version of Avrumi Sherman with handling of STDERR and STDOUT:

D:\>dir ERROR 1>log.txt 2>&1 & type log.txt
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\Projekte\UDG_TMP\build-deploy

 Datei nicht gefunden

So it displays the STDTOUT (first lines) and the STDERR (last line) on screen and the log-file.

For usage in a batch file just do it like Avrumi Sherman did but use a single & in order to catch the ERROR-cases as well:

D:\>set logFile=log.txt
D:\>set tmpLog=tmp.txt
D:\>set logCmd=1^>%tmpLog% 2^>^&1 ^& type %tmpLog% ^& type %tmpLog%^>^>%logFile%

Now you can use the logCmd variable like a logCmd ;)

Note the ERROR messages of the first dir and the second rmdir are displayed on screen as well as appearing in the log-file:

D:\>echo START %logCmd%
START

D:\>dir DIRECTORY %logCmd%
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\

Datei nicht gefunden

D:\>mkdir DIRECTORY %logCmd%

D:\>dir DIRECTORY %logCmd%
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\DIRECTORY

26.08.2020  17:50    <DIR>          .
26.08.2020  17:50    <DIR>          ..
               0 Datei(en),              0 Bytes
               2 Verzeichnis(se), 68.391.989.248 Bytes frei

D:\>rmdir DIRECTORY %logCmd%

D:\>rmdir DIRECTORY %logCmd%
Das System kann die angegebene Datei nicht finden.

D:\>echo STOP %logCmd%
STOP

At the end of your batch file you can/should delete %tmpLog%. Here the content of %logFile% using the example above:

START
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\

Datei nicht gefunden
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\DIRECTORY

26.08.2020  17:50    <DIR>          .
26.08.2020  17:50    <DIR>          ..
               0 Datei(en),              0 Bytes
               2 Verzeichnis(se), 68.391.989.248 Bytes frei
Das System kann die angegebene Datei nicht finden.
STOP
Alexander Schäl
  • 126
  • 2
  • 12
0

Take a look at this SOq:

Basically, you can use tee command e.g. from here:

Community
  • 1
  • 1
icyrock.com
  • 27,952
  • 4
  • 66
  • 85