Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 21st, 2012, 11:59 PM
kuldeek kuldeek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 7 kuldeek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 54 sec
Reputation Power: 0
Problem logging STDOUT in perl

I have a program, which call a subroutine and logs the STDOUT to a different file for each subroutine called.


foreach $test_run (keys(%test_hash)) {
if ($test_hash{$test_run} == 1) {
$test_execute = $test_run;
$test_execute1 = $test_run."."."pm";
my $logfile = $test_execute."_log.txt";
print "the logfile is $logfile \n";
open (STDOUT, "| tee $logfile");
require "$test_execute1";
my $op = $test_execute->$test_execute();
}

On executing my script with above code creates all the log files, but lets say foreach loop runs for 3 times; then first log file have output of all the 3 calls of test_execute,2nd log file have output of 2nd and 3rd calls of test_execute and last log file will have only last call of test_execute.
Please help me get all the logg files which contains logs for every single test_execute call.

Reply With Quote
  #2  
Old August 22nd, 2012, 06:40 AM
OmegaZero OmegaZero is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2007
Posts: 737 OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 13 m 32 sec
Reputation Power: 928
On each iteration of the loop, you're just adding to the pipeline so by the last iteration you have "Perl -> tee log_file_3 -> tee log_file_2 -> tee log_file_1". You can use "local" to save & restore STDOUT on each iteration:

Code:
C:\temp>cat test.pl
for ( 1 .. 3 ) {
    local *STDOUT;
    open STDOUT, "| tee $_.log" or die $!;
    print "Iteration $_\n";
}

C:\temp>cat 1.log
Iteration 1

C:\temp>cat 2.log
Iteration 2

C:\temp>cat 3.log
Iteration 3
__________________
sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

Reply With Quote
  #3  
Old August 22nd, 2012, 09:04 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,653 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 7 h 15 m 4 sec
Reputation Power: 1170
Personally, I'd use either PerlIO::tee or File::Tee instead of your piped open.

Is that a direct copy/paste of your actual code, or is it just an approximation of your real code? The reason I ask is that I see a problem with this line:
Code:
my $op = $test_execute->$test_execute();



$test_execute is a copy of $test_run, which is the hash key and hash keys are strings. In this line of code you're using it as an object as well as the method being called. That does not make any sense.

Here's how your loop would look if you used File::Tee. Note, I cleaned up some of the duplication but left in the issue noted above.

Code:
use File::Tee qw(tee);

foreach my $test_run ( keys %test_hash ) {

    if ( $test_hash{$test_run} == 1 ) {

        my $test_execute = $test_run . '.pm';
        require $test_execute;

        my $logfile = $test_run . '_log.txt';
        open my $log_fh, '>', $logfile or die "failed to open '$logfile' $!";

        tee STDOUT, $log_fh;
        my $op = $test_run->$test_run();

       close $log_fh;
    }

}

Last edited by FishMonger : August 22nd, 2012 at 09:08 AM.

Reply With Quote
  #4  
Old August 22nd, 2012, 09:24 AM
kuldeek kuldeek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 7 kuldeek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 54 sec
Reputation Power: 0
This code is more of less equivalent to what I currently have. Issue still persists.

Reply With Quote
  #5  
Old August 22nd, 2012, 09:26 AM
kuldeek kuldeek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 7 kuldeek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 54 sec
Reputation Power: 0
Quote:
Originally Posted by OmegaZero
On each iteration of the loop, you're just adding to the pipeline so by the last iteration you have "Perl -> tee log_file_3 -> tee log_file_2 -> tee log_file_1". You can use "local" to save & restore STDOUT on each iteration:

Code:
C:\temp>cat test.pl
for ( 1 .. 3 ) {
    local *STDOUT;
    open STDOUT, "| tee $_.log" or die $!;
    print "Iteration $_\n";
}

C:\temp>cat 1.log
Iteration 1

C:\temp>cat 2.log
Iteration 2

C:\temp>cat 3.log
Iteration 3


I have tried this earlier and I am sure this will work. But there is a big hidden issue which I am not able to identify, when I use local *STDOUT in my scripts; device on which i running these scripts, device crashes. .. so I am looking for any other solution than this.

Reply With Quote
  #6  
Old August 22nd, 2012, 09:33 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,653 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 7 h 15 m 4 sec
Reputation Power: 1170
It's difficult for us to provide possible solutions when you don't provide an accurate representation of your code or what you're needing to accomplish.

Reply With Quote
  #7  
Old August 22nd, 2012, 09:38 AM
kuldeek kuldeek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 7 kuldeek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 54 sec
Reputation Power: 0
Quote:
Originally Posted by FishMonger
It's difficult for us to provide possible solutions when you don't provide an accurate representation of your code or what you're needing to accomplish.

Solution which I am looking is exactly what OmegaZero has provided. But I want to achieve same thing using some other trick.

Reply With Quote
  #8  
Old August 22nd, 2012, 09:45 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,653 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 7 h 15 m 4 sec
Reputation Power: 1170
Did you try either of the modules I suggested?

Reply With Quote
  #9  
Old August 22nd, 2012, 09:54 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,653 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 7 h 15 m 4 sec
Reputation Power: 1170
Example:

[root@099-91-RKB01 ~]# cat ./test.pl
Code:
#!/usr/bin/perl

use strict;
use warnings;
use File::Tee qw(tee);

for ( 1 .. 3 ) {
    my $logfile = "file$_.log";
    open my $fh, '>', $logfile or die "failed to open '$logfile' $!";

    tee STDOUT, $fh;
    print "Iteration $_\n";
    close $fh;
}


output:
[root@099-91-RKB01 ~]# ./test.pl
Iteration 1
Iteration 2
Iteration 3

=====

[root@099-91-RKB01 ~]# cat 1.log
Iteration 1
[root@099-91-RKB01 ~]# cat 2.log
Iteration 2
[root@099-91-RKB01 ~]# cat 3.log
Iteration 3

Reply With Quote
  #10  
Old August 22nd, 2012, 10:06 AM
kuldeek kuldeek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 7 kuldeek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 54 sec
Reputation Power: 0
Quote:
Originally Posted by FishMonger
Example:

[root@099-91-RKB01 ~]# cat ./test.pl
Code:
#!/usr/bin/perl

use strict;
use warnings;
use File::Tee qw(tee);

for ( 1 .. 3 ) {
    my $logfile = "file$_.log";
    open my $fh, '>', $logfile or die "failed to open '$logfile' $!";

    tee STDOUT, $fh;
    print "Iteration $_\n";
    close $fh;
}


output:
[root@099-91-RKB01 ~]# ./test.pl
Iteration 1
Iteration 2
Iteration 3

=====

[root@099-91-RKB01 ~]# cat 1.log
Iteration 1
[root@099-91-RKB01 ~]# cat 2.log
Iteration 2
[root@099-91-RKB01 ~]# cat 3.log
Iteration 3


yes I tried it and got following output:

automation@automation:~$ cat file1.log
Iteration 1
Iteration 2
Iteration 3
automation@automation:~$ cat file2.log
Iteration 2
Iteration 3
automation@automation:~$ cat file3.log
Iteration 3
automation@automation:~$

I tried following code:

Code:
#!/usr/bin/perl

use strict;
use warnings;
use File::Tee qw(tee);

for ( 1 .. 3 ) {
    my $logfile = "file$_.log";
    open my $fh, '>', $logfile or die "failed to open '$logfile' $!";

    tee STDOUT, $fh;
    print "Iteration $_\n";
    close $fh;
}

Reply With Quote
  #11  
Old August 22nd, 2012, 10:29 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,653 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 7 h 15 m 4 sec
Reputation Power: 1170
I am unable to duplicate your problem.

Here's another example using PerlIO::tee
Code:
#!/usr/bin/perl

use strict;
use warnings;
use PerlIO::tee;

for ( 1 .. 3 ) {
    my $logfile = "file-IO-tee$_.log";
    open my $fh, '>', $logfile or die "failed to open '$logfile' $!";

    *STDOUT->push_layer(tee => $fh);
    print "Iteration $_\n";

    *STDOUT->pop_layer();
    close $fh;
}


Quote:
[root@099-91-RKB01 ~]# cat file-IO-tee1.log
Iteration 1
[root@099-91-RKB01 ~]# cat file-IO-tee2.log
Iteration 2
[root@099-91-RKB01 ~]# cat file-IO-tee3.log
Iteration 3

Reply With Quote
  #12  
Old August 22nd, 2012, 09:01 PM
kuldeek kuldeek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 7 kuldeek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 54 sec
Reputation Power: 0
Quote:
Originally Posted by FishMonger
I am unable to duplicate your problem.

Here's another example using PerlIO::tee
Code:
#!/usr/bin/perl

use strict;
use warnings;
use PerlIO::tee;

for ( 1 .. 3 ) {
    my $logfile = "file-IO-tee$_.log";
    open my $fh, '>', $logfile or die "failed to open '$logfile' $!";

    *STDOUT->push_layer(tee => $fh);
    print "Iteration $_\n";

    *STDOUT->pop_layer();
    close $fh;
}

Thanks, using PerlIO::tee worked perfectly. Thanks a lot.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Problem logging STDOUT in perl

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap