I have an application where I would like to capture STDOUT and STDERR to files, given references to their filehandles... Ideally I would tee them so shell redirection would still work. Here are some relevant code snippets:
The code that actually opens the file and returns the filehandle reference:
sub open_logfile
{
my $filename = shift;
# Our filehandle reference object
local *FH;
# Attempt to open the file
if(open(FH, "> " . $filename))
{
# Opened the file
print $FH_STATUS "Opened logfile: " . $filename . "\n";
select(FH);
$| = 1; # Unbuffer
select(STDOUT); # Restore default print to STDOUT
return *FH; # Return the new fh reference
}
else
{
# Couldn't open the file...
print $FH_ERROR "Problem opening logfile: \n";
print $FH_ERROR $filename . ":\n";
print $FH_ERROR $! . "\n";
print $FH_ERROR "Redirecting logging to Error log\n\n";
sleep 5;
return $FH_ERROR;
}
}
Code that I would likely use to tee the output:
use IO::Tee;
$FH_STATUS = new IO::Tee(\*STDOUT,
new_logfile(name_logfile('logs/QTStatus')));
And then redirect all STDOUT to the new $FH_STATUS. If I can get that working, I'll do the same with STDERR.
--isotope
http://www.skylab.org/~isotope/
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.