39

I'm writing a C program in Windows, my printf calls print to the command line, and I know that I can redirect all this output to a text file using:

myProgram.exe > mylog.txt

However, I wish to also see the output that would have been printed to the console as well as log all of it in a text file.

Is there a way to do this? I was thinking of using tail to monitor the log file.

studiohack
  • 13,468
  • 19
  • 88
  • 118
user79397
  • 493
  • 1
  • 4
  • 5
  • What i've done sometimes is open another cmd prompt window and repeatedly execute type mylog.txt (or could do notepad mylog.txt) and see how it develops. tee looks like a great one though. – barlop May 02 '11 at 21:09
  • 2
    [Displaying Windows command prompt output and redirecting it to a file](http://stackoverflow.com/q/796476/995714) – phuclv Mar 18 '16 at 04:07
  • 1
    Possible duplicate of [How to pipe output and duplicate it to STDOUT?](https://superuser.com/questions/602335/how-to-pipe-output-and-duplicate-it-to-stdout) – phuclv Oct 24 '18 at 04:46

4 Answers4

26

The windows PowerShell has a tool that can do that, named tee after the unix tool which does the same.

Alternatively, there are ports of the unix tee for windows:

Lesmana
  • 19,803
  • 6
  • 45
  • 44
  • 3
    powershell is a great tip, could be cmd is just for people that haven't moved on yet! Using gnuwin32 is better than using unxutils, unxutils is rather old. gnuwin32 is newer and probably has everything in unxutils too, and has a lot more utilities. as mentioned elsewhere, gnuwin32 coreutils has tee – barlop May 03 '11 at 04:58
  • Same works in CMDer, another 3rd party command-line util. – Rauni Oct 31 '17 at 14:00
  • 1
    In this answer https://superuser.com/a/273112/213743 BaconBits points out that the PowerShell _tee_ is line-oriented: it won't output a line "until an end-of-line character is reached" newline character is encountered. BaconBits suggests that the unix _tee_ always immediately forwards content. – buzz3791 Mar 14 '18 at 13:50
12

Under Windows all I can think is to do this:

myProgram.exe > mylog.txt & type mylog.txt

This is based on the command example in your question - if in fact you wanted to append the output to mylog.txt then you'd want to use >> instead of >, but type would print out the entire log file, not just what had been appended.

If you download the GnuWin32 CoreUtils, you can use the Unix method (tee command) for doing this:

myProgram.exe | tee mylog.txt

This will write the output of myProgram.exe to mylog.txt but also display it to the console at the same time. If you only want to append to mylog.txt then you can pass the -a parameter to tee.

Gaff
  • 18,569
  • 15
  • 57
  • 68
  • 3
    The tee solution looks much better. The other thing you mention is not simultaneous. – barlop May 02 '11 at 21:08
  • 1
    @barlop - I agree, the first solution is a bit of a workaround since you can't do it natively in Windows. tee does the trick nicely here :) – Gaff May 02 '11 at 21:10
  • Thanks for the help,upvoted, going with tee but lesmana got in a couple of mins earlier when he/she mentioned tee :) – user79397 May 02 '11 at 21:37
  • If you leave out 'type' it will open the file in the default editor (notepad for me) when the task is complete. – Josh Stribling Apr 24 '15 at 21:30
1

I use Visual Studio Code and open the log file from there, it keeps the view up to date in near real-time as the log file changes

Jaime Botero
  • 111
  • 2
0

I just had a similar need and used Tail as the OP suggested they might:

>C:\Temp\Commands_Log.txt (
START tail.exe -f C:\Temp\Commands_Log.txt

Some_Commands
Other_Commands

echo.
echo ALL DONE HERE!
echo.
echo IT IS NOW SAFE TO CLOSE THIS WINDOW!
)

The ">C:\Temp\Commands_Log.txt" creates the log file and adds the output from the all the commands located inside the (parentheses).

The first command inside the parentheses should be to start the Tail, which will open in a new command window.

The echo's at the end are for unfamiliar users to let them know when everything is complete.

DBADon
  • 473
  • 4
  • 11