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;
Print["Contents of file:"];


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?

  • 1
    $\begingroup$ Related mathematica.stackexchange.com/questions/3762/… $\endgroup$ – Dr. belisarius Sep 11 '12 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 Sep 11 '12 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$ – Dr. belisarius Sep 11 '12 at 19:06

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:


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

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

| improve this answer | |
  • $\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 Sep 12 '12 at 12:27
  • $\begingroup$ @rcollyer it's merely an example; feel free to edit it or post a more complete answer. $\endgroup$ – Mr.Wizard Sep 12 '12 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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