Hello World!\n

I have an existing script that's rather large and contains many, many print commands. It's got a Tk GUI, but it logs things in the DOS console window.

Since completing it, I've realised that it would be nice to give the user the opportunity to log everything that's printed to the console window to a file.

I have managed to work out that I can print to the screen and a file by using:

use IO::Tee; my $tee = IO::Tee -> new(">stdout.txt", \*STDOUT); print $tee "Cheese!\n";
So, is there a way that I can call a sub that will turn file logging on and off, or must I find every print statement, and replace it, like this?
my $log = 0; print "Do you want to log to a file?"; my $resp = <STDIN>; if ($resp =~ /^y/i) { $log = IO::Tee -> new(">stdout.txt", \*STDOUT); } ... if $log { print $log "What did I want to say?\n" } else { print "What did I want to say?\n" }
Thanks,
Spike.

NEW UPDATE:

I've now found a much better solution, that doesn't need any fiddling about with changing print commands. Actually, it's so obvious, I don't know how I missed it in the first place!

my $tee; sub log_to_file { my ($on, $logfile) = @_; if ($on) { $tee = IO::Tee->new(">>$logfile", \*STDOUT); select $tee; print "Logging console window to $logfile\n"; } else { print "Console window log OFF.\n"; undef $tee; select STDOUT; close $logfile; } }
OLD UPDATE:

Since reading ccn's response (ccn++), I have come up with the following sub to turn file logging on and off:

sub log_to_file { my $on = $_[0]; if ($on) { Log::Log4perl->easy_init ({ level => $DEBUG, file => 'STDOUT', layout => '%m', }, {level => $DEBUG, file => ">> $console_log", layout => '%m', }); } else { Log::Log4perl->easy_init ({ level => $DEBUG, file => 'STDOUT', layout => '%m', }); } }
Then all I did was to search for all print commands, and replaced them with DEBUG. Really cool, thanks ccn!

The only thing I'm now trying to do is incorporate STDERR into the process, but I've had no success, so far.


In reply to Redirecting STDOUT to file and screen as an afterthought, on Windows. by spikey_wan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.