boat73 has asked for the wisdom of the Perl Monks concerning the following question:

Hello wise ones, I don't see a lot of questions about this, which leads me to believe I might be missing something simple. What I want to do is have users run a script and be able to view the output/erros as it executes. At the same time I would like to have the output/erros write to a log. I know how to redirect STDOUT/STDERR, but can that be done and also display it to the screen in real time? Thanks in advance I asked another question about this issue as well as posting additional code. here
  • Comment on Copy STDOUT and STDERR to logfile and also display to screen

Replies are listed 'Best First'.
Re: Copy STDOUT and STDERR to logfile and also display to screen
by japhy (Canon) on Aug 10, 2005 at 17:38 UTC
      No such module, that I can find. I think you meant to say IO::Tee.
      -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
Re: Copy STDOUT and STDERR to logfile and also display to screen
by pboin (Deacon) on Aug 10, 2005 at 17:36 UTC

    You might want to look into man tee if you're on a *nix system. Description: Copy standard input to each FILE, and also to standard output.

    Don't know if it works w/ STDERR though...

Re: Copy STDOUT and STDERR to logfile and also display to screen
by kirbyk (Friar) on Aug 10, 2005 at 18:05 UTC
    In addition to the more generic advice, if you're on a unix system, most people just do 'tail -f filename'. The 'tail' command gives you the last lines of a file, and the -f argument 'streams' new input into the screen. You can specify multiple files and it will intersperse changes sensibly.

    -- Kirby, WhitePages.com

Re: Copy STDOUT and STDERR to logfile and also display to screen
by sk (Curate) on Aug 10, 2005 at 17:51 UTC
    If you are on non UNIX platform i wrote this small script that does tee - useful with pipes: Creating an outputfile and processing it at once

    tee is useful when you cannot change the script that was given to you and you want to save and view. But if you are the author of the script why can't a simple print statement work?

    for example -

    if ($error) { print STDERR ("Error: $errmsg\n"); print ERRLOG ("Error: $errmsg\n"); } else { print ("Output: $outstr\n"); print CLEANLOG ("Output: $outstr\n"); }

    Of course if you if you are interpolating the string inside print I would probably first do that in a variable and use it inside print to avoid interpolating twice

    Am i missing somethign here?

      It works, but now you've got two copies of every message. At the very least wrap that up in a pair of subs.

      sub my_print { print @_; print CLEANLOG @_ } sub err_print { print STDERR @_; print ERRLOG @_ }

      --
      We're looking for people in ATL

        Thanks for the tip on the sub.

        regarding your other comment: It works, but now you've got two copies of every message

        Could you please explain what do you mean by copy? it writes the same msg twice but it does it in two different places...the output on the screen will not have dups and the output in the log will not have dupliactes...

        if you say it is inefficient to do it this then i would agree but i am not sure whether i understand the term "copy" there.

        thanks

        SK

Re: Copy STDOUT and STDERR to logfile and also display to screen
by salva (Canon) on Aug 11, 2005 at 09:12 UTC
    on Unix, pipeing the output to a new forked process is the only way that would work in any case:
    select STDERR; $|=1 unless (open STDERR, '|-') { eval { open ERRLOG, ">/tmp/errlog" or die; while(<>) { print ERRLOG $_; print STDERR $_; } }; exit(0); } # repeat for STDOUT
      Sorry guys, I should have mentioned I am doing this on a Win32 system at the moment. The Unix stuff will come in handy though.