Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Both print and redirect STDOUT and STDERR

by chessgui (Scribe)
on Feb 09, 2012 at 14:00 UTC ( [id://952723]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Both print and redirect STDOUT and STDERR
by MidLifeXis (Monsignor) on Feb 09, 2012 at 14:09 UTC
    give me a complete, foolproof, runnable solution

    Sure. My rate is $75 USD / hour, 2 hour minimum.

    That is not how perlmonks works. The people here are volunteering their time. To demand a full and complete solution is rather presumptuous.

    I have heard that File::Tee will do the job, but that is a link to a module, so you are probably not interested, even though it does look like the documentation even provides a full solution.

    --MidLifeXis

      File::Tee fails on Windows.

      However this is not as simple a problem as I first thought. First I thought you should tie STDOUT to a self defined package. However if you use 'print' in method 'PRINT' it becomes recursive. I have given up on this.

      The only solution with which I can come up is to open the program as a pipe from an other script. This is my ad hoc very unpolished solution:
      my $what=shift; use IPC::Open3; use IO::Handle; use threads; unlink('out.txt'); print "Going to run $what.n"; $pid = open3( \*CHILD_IN, \*CHILD_OUT, \*CHILD_ERR, "perl.exe $what" ) +; autoflush CHILD_OUT; autoflush CHILD_ERR; threads->create(\&handle_child_out)->detach; threads->create(\&handle_child_err)->detach; while(1){}; sub handle_child_out { do { sysread CHILD_OUT, $content, 10; if($content ne '') { print "$content"; open OUT,'>>out.txt'; print OUT $content; close OUT; } } while(1); } sub handle_child_err { do { sysread CHILD_ERR, $content, 10; if($content ne '') { print "$content"; open OUT,'>>out.txt'; print OUT $content; close OUT; } } while(1); }

        IO::Tee then.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Re: Both print and redirect STDOUT and STDERR
by ikegami (Patriarch) on Feb 09, 2012 at 22:48 UTC

    Add 2>&1 | tee out.txt to your command (or equivalent in the script).

    You'll get better results if you use $| = 1; in the script, but it's not necessary to fulfill your requirements.

Re: Both print and redirect STDOUT and STDERR
by runrig (Abbot) on Feb 10, 2012 at 16:55 UTC
Re: Both print and redirect STDOUT and STDERR
by Perlbotics (Archbishop) on Feb 09, 2012 at 21:01 UTC

    I need a solution which prints STDOUT and STDERR both to the screen and to a file at the same time.
    So, here is one solution. Maybe it is too complicated, but your question smells like logging anyhow.

    use strict; use Log::Log4perl qw(get_logger); # Define configuration for file test.log, STDERR, and STDOUT my $conf = q( log4perl.logger = ALL, FileApp, Screen +STDERR, ScreenSTDOUT log4perl.appender.FileApp = Log::Log4perl::Appen +der::File log4perl.appender.FileApp.filename = test.log log4perl.appender.FileApp.layout = PatternLayout log4perl.appender.FileApp.layout.ConversionPattern = %m%n log4perl.appender.ScreenSTDERR = Log::Log4perl::Appen +der::Screen log4perl.appender.ScreenSTDERR.stderr = 1 log4perl.appender.ScreenSTDERR.layout = PatternLayout log4perl.appender.FcreenSTDERR.layout.ConversionPattern = %m%n log4perl.appender.ScreenSTDOUT = Log::Log4perl::Appen +der::Screen log4perl.appender.ScreenSTDOUT.stderr = 0 log4perl.appender.ScreenSTDOUT.layout = PatternLayout log4perl.appender.ScreenSTDOUT.layout.ConversionPattern = %m%n ); # Initialize logging behaviour Log::Log4perl->init( \$conf ); # Obtain a logger instance my $logger = get_logger("Chess::GUI"); $logger->info("This is going to test.log, STDERR, and STDOUT.");

    Log::Log4perl is quite powerful. You could try experimenting with different conversion patterns as explained in Log::Log4perl::Layout::PatternLayout.

Re: Both print and redirect STDOUT and STDERR
by kielstirling (Scribe) on Feb 09, 2012 at 22:13 UTC
    So you want someone else to do it for you then?? If you really what to learn Perl you'll have to read a tut or 2 at some point.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-26 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found