CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Problem logging STDOUT in perl

 



kuldeek
Novice

Aug 21, 2012, 9:39 PM

Post #1 of 8 (1833 views)
Problem logging STDOUT in perl Can't Post

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.


(This post was edited by kuldeek on Aug 21, 2012, 9:40 PM)


rovf
Veteran

Aug 22, 2012, 1:12 AM

Post #2 of 8 (1829 views)
Re: [kuldeek] Problem logging STDOUT in perl [In reply to] Can't Post


Quote
Please help me


1. Always test the return value from open

2. use strict;

3. use warnings;

as for (3), I personally prefer even

use warnings FATAL => 'all';

but this is a matter of taste.


kuldeek
Novice

Aug 22, 2012, 1:46 AM

Post #3 of 8 (1825 views)
Re: [rovf] Problem logging STDOUT in perl [In reply to] Can't Post

Thanks for the advice.
But my problem statement is different, please have a look if you can help.


rovf
Veteran

Aug 22, 2012, 2:04 AM

Post #4 of 8 (1824 views)
Re: [kuldeek] Problem logging STDOUT in perl [In reply to] Can't Post


Quote
But my problem statement is different


It is partially different, because, if you don't check the return value of 'open', you can't say for sure whether the open call succeeded, and since your posting is about problems where the output is showing up in the "wrong" files, this is certainly related.

However there is one more point to it: Whenever you ask in a forum for help, you certainly won't throw in some piece of code and hope that a friendly soul will debug it for you. Instead, you will do as much as you can, and ask for help only for those parts of the codes where you are really stuck. With other words, you will try to rule out by yourself "obvious" problems, such as undefined values etc.

The best way to do it is - aside from checking the return values of functions - to use 'strict' and 'warnings'. Once your code runs with this enabled, it makes sense to focus on the remaining problem(s). In many cases, there won't be problems left anyway....


Laurent_R
Enthusiast / Moderator

Aug 22, 2012, 2:31 AM

Post #5 of 8 (1822 views)
Re: [kuldeek] Problem logging STDOUT in perl [In reply to] Can't Post

I definitely agree with Rovf.

I think the problem with your code is that you open your logfiles within the loop.

The first time through tle loop, only one logfile is opened, the second time two are opened, and the third time three logfiles are opened.


kuldeek
Novice

Aug 22, 2012, 2:45 AM

Post #6 of 8 (1821 views)
Re: [Laurent_R] Problem logging STDOUT in perl [In reply to] Can't Post

I have tried lot of things to get this working.
Things works well with following code:

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, "> $logfile");
require "$test_execute1";
my $op = $test_execute->$test_execute();
}

But above code, restricts logs from console i.e. STDOUT. I cannot see running logs of the script, though all the log files are created perfectly.
But when i use "| tee $logfile" while opening STDOUT, problem occurs.
I tried closing STDOUT after each call of test_execute, but that gives following messages because STDOUT is closed:


tee: standard output: Bad file descriptor
tee: write error: Bad file descriptor


FishMonger
Veteran / Moderator

Aug 22, 2012, 7:16 AM

Post #7 of 8 (1812 views)
Re: [kuldeek] Problem logging STDOUT in perl [In reply to] Can't Post

FYI to all, the OP's question is cross posted on devshed and probably elsewhere.

Here's a copy of my devshed response .


Personally, I'd use either http://search.cpan.org/~gfuji/PerlIO-Util-0.72/lib/PerlIO/tee.pm]PerlIO::tee or http://search.cpan.org/~salva/File-Tee-0.07/lib/File/Tee.pm]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;
}

}



kuldeek
Novice

Aug 22, 2012, 7:34 AM

Post #8 of 8 (1807 views)
Re: [FishMonger] Problem logging STDOUT in perl [In reply to] Can't Post

This code is more of less equivalent to what I currently have. Issue still persists.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives