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?
Answer
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];
Comments
Post a Comment