views:

467

answers:

4

I'd like to log the output of a command to stdout as well as to a log file. I've got Cygwin installed and I'm trying to use the tee command to accomplish this.

devenv mysolution.sln /build myproject "Release|Win32" | tee build.log

Trouble is that tee seems to insist on waiting for the end of file before outputting anything to either stdout or the log file. This takes away the point of it all, which is to have a log file for future reference, but also some stdout logging so I can easily see the build progress.

tee's options appear to be limited to --append, --ignore-interrupts, --help, and --version. So is there another method to get to what I'm trying to do?

+4  A: 

You can output to the file and tail -f the file.

devenv mysolution.sln /build myproject "Release|Win32" > build.log &

tail -f build.log

Sam Reynolds
wintail is also possible
mana
+1  A: 

Write your own! (The point here is that the autoflush ($|) setting is turned on, so every line seen is flushed straight away. This may perhaps be what the real tee command lacked.)

#!/usr/bin/perl -w
use strict;
use IO::File;
$| = 1;
my @fhs = map IO::File->new(">$_"), @ARGV;
while (my $line = <STDIN>) {
    print $line;
    $_->print($line) for @fhs;
}
$_->close for @fhs;

You can call the script anything you want. I call it perlmilktee! :-P

Chris Jester-Young
A: 

@Sam: Thanks; I didn't know about -f on tail. Is there a way to tell tail to terminate when devenv is done?

Owen
I can't think of a way short of writing a little shell script to watch for the devenv process to go away and then kill (gently) the tail process.
Sam Reynolds
With tail --pid offers a way to terminate. With the bash shell, you'll have to use pgrep to get the pid for script automation.
Chris
+1  A: 

tee seems to insist on waiting for the end of file before outputting anything to either stdout or the log file.

This should definitely not be happening - it would render tee nearly useless. Here's a simple test that I wrote that puts this to the test, and it's definitely not waiting for eof.

$ cat test
#!/bin/sh
echo "hello"
sleep 5
echo "goodbye"

$ ./test | tee test.log
hello
<pause>
goodbye
jon
@jon: Yeah, it seems my problem was actually with devenv.exe choking things up somehow when it's building a big solution. I tried using my own tee replacement and it had the same trouble.
Owen