3
$\begingroup$

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?

$\endgroup$
3
  • 1
    $\begingroup$ Related mathematica.stackexchange.com/questions/3762/… $\endgroup$ Commented Sep 11, 2012 at 18:18
  • $\begingroup$ @belisarius The two questions may indeed have similar answers, although the workaround you commented there does not apply here. $\endgroup$
    – JxB
    Commented Sep 11, 2012 at 19:04
  • 1
    $\begingroup$ 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 :( $\endgroup$ Commented Sep 11, 2012 at 19:06

1 Answer 1

4
$\begingroup$

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];
$\endgroup$
2
  • $\begingroup$ I would consider adding functionality where it would detect if $log had anything assigned to it, and revert to the default behavior. +1 $\endgroup$
    – rcollyer
    Commented Sep 12, 2012 at 12:27
  • $\begingroup$ @rcollyer it's merely an example; feel free to edit it or post a more complete answer. $\endgroup$
    – Mr.Wizard
    Commented Sep 12, 2012 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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