Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to tee STDERR on Windows

by freonpsandoz (Beadle)
on Jan 13, 2022 at 07:11 UTC ( #11140418=perlquestion: print w/replies, xml ) Need Help??

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

I'm looking for a way to tee STDERR so that all output to it also goes to a file. I'm using ActivePerl v5.20.2 for Windows. I tried code suggested in the topic https://www.perlmonks.org/?node_id=843501. It does tee STDERR, but not to the file I want. It creates a file named "GLOB(0xf7b278)" and the output goes to that file. It also creates the file I specified, but it's empty

use strict; use warnings; use open qw(:std :utf8); # Set the default encoding for STDIN, STDOUT +& STDERR to UTF-8 use IO::Tee (); my $logname = 'test-tee$.log'; open(my $logfile, ">:encoding(UTF-8)", $logname) or die qq(Can't open +"$logname for writing"\n); # Tee STDERR to log file. If running as Administrator, console window +may disappear quickly! open STDERR2, ">&=STDERR" or die "Failed to alias STDERR: $!"; *STDERR = IO::Tee->new(\*STDERR2, ">$logfile") or die "Failed to tee to aliased STDERR and '$logfile': $!"; print(STDERR "This is a test\n");

Any suggestions? Thanks in advance.

UPDATE: I found the problem. Line 9 should be:

*STDERR = IO::Tee->new(\*STDERR2, $logfile)

I thought I had tried that also, but apparently not!

Replies are listed 'Best First'.
Re: How to tee STDERR on Windows
by Marshall (Canon) on Jan 13, 2022 at 08:15 UTC
    Interesting question. Windows supports redirection of STDERR and STDOUT, like test.pl >log 2>&1 will send both to a file. "tee" is available in the powershell. just typing "powershell" at the windows cmd prompt will start the powershell. But the problem is that there is no easy way to pipe STDERR into another program like tee. You can tee STDOUT and send STDERR to a file, but you want both on the screen and in the file.

    This is one heck of an ugly Win10 command line solution. But I tested this on my machine and it does work. I admit that I am still struggling to understand exactly why it works. I make no claims other than I did test this on Win10 home edition with an actual Perl program.

    Win10prompt>for /f "delims=" %L in ('perl testchompwhile.pl 2^>^&1') do @echo %L & echo %L >> log

    I happened to have a short Perl program that I wrote for another post for testing. This appends both STDERR and STDOUT to log and of course the also both go to screen as requested.

    update: The only other info I have about this is: "The for command iterates over the output lines of the script. The carets escape the characters that follow them. The first echo goes to the screen and the second is appended to the file named "log"."

Re: How to tee STDERR on Windows
by LanX (Sage) on Jan 13, 2022 at 07:19 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11140418]
Approved by LanX
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2022-08-10 04:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?