3

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?

JxB
  • 5,111
  • 1
  • 23
  • 40
  • 1
    Related http://mathematica.stackexchange.com/questions/3762/how-do-i-re-direct-fileprints-output – Dr. belisarius Sep 11 '12 at 18:18
  • @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 :( – Dr. belisarius Sep 11 '12 at 19:06

1 Answers1

4

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];
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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