Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to log messages that I send to the screen, even temporary ones.

By setting $Output appropriately, it is possible to log Print messages, but not PrintTemporary messages:

oldout = $Output;
    logfile = FileNameJoin[{$TemporaryDirectory, "example.log"}];
    log = OpenWrite[logfile];
    $Output = Append[oldout, log];
Print["Log message printed on screen."];
PrintTemporary["Log message printed temporarily on screen."]; Pause[1];
$Output = oldout;
Close[log];
Print["Contents of file:"];
FilePrint[logfile];

gives

Log message printed on screen.

(Log message printed temporarily on screen.)

Contents of file:

"Log message printed on screen."

Perhaps this is not surprising, but if you start Mathematica in non-gui mode (i.e., "MathKernel" on MacOsX), the PrintTemporary output stays permanently on the screen.

How can one direct the output of PrintTemporary permanently to an additional output stream?

share|improve this question
1  
    
@belisarius The two questions may indeed have similar answers, although the workaround you commented there does not apply here. –  JxB Sep 11 '12 at 19:04
1  
Yes. That's why I haven't voted to close. My comment was for OP, as that question has been idle for a long time :( –  belisarius Sep 11 '12 at 19:06
add comment

1 Answer 1

up vote 4 down vote accepted

Since PrintTemporary does not use output streams, but rather is akin to CellPrint, I believe you will need to add this functionality manually. For example:

Unprotect[PrintTemporary];

$log = OpenAppend["logfile.txt"];

PrintTemporary[expr_] /; ! TrueQ[ptLog] :=
 Block[{ptLog = True},
  WriteString[$log, expr];
  PrintTemporary @ expr
 ]

Protect[PrintTemporary];
share|improve this answer
    
I would consider adding functionality where it would detect if $log had anything assigned to it, and revert to the default behavior. +1 –  rcollyer Sep 12 '12 at 12:27
    
@rcollyer it's merely an example; feel free to edit it or post a more complete answer. –  Mr.Wizard Sep 12 '12 at 12:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.