6
$\begingroup$

The newish function Echo is tremendously useful in quick-and-dirty debugging, since you can stick it pretty much anywhere in an expression and get feedback about what's going on. However, like almost everything else in Mathematica (except $Messages on Linux), all of the output it generates is sent straight to STDOUT.

For interactive use/debugging, this is a minor issue. However, a lot of the time I need to debug scripts that are run on the command line using wolframscript, or even launching them on remote machines using ssh. This means that being able to direct program output to log files, et c. is invaluable. Experimentation suggests changing $Output doesn't do the trick at all. Here's what happens if you send $Output to a file and use Print:

stream = OpenWrite[];

Block[{$Output = {stream}},
 Print["foo"]];
(* note: no output appears in notebook *)

ReadLine@Close@stream
(* "foo" *)

Here's what happens with Echo:

stream = OpenWrite[];

Block[{$Output = {stream}},
 Echo["foo"];]
(* note: >> foo appears in notebook *)

ReadLine@Close@stream
(* EndOfFile *)

Note redirecting $Urgent and $Messages doesn't help either. $Echo isn't, despite the promising name, what I'm looking for at all.

Just to check if I might have missed something, I tried to find other stream variables using the following:

Quiet@Select[
  Names["System`$*"], 
  ! FreeQ[ToExpression[#, InputForm, OwnValues], _OutputStream] &]

(* {"$Messages", "$Output", "$Urgent"} *) 
$\endgroup$
  • 4
    $\begingroup$ If you are running without a FE, Echo should just use Print. Otherwise, try Block[{$Output = {stream}, $Notebooks}, Echo["foo"];]. $\endgroup$ – ilian Apr 17 '17 at 18:18
  • 1
    $\begingroup$ You want to collect those printed by Echo? If so, check this post: mathematica.stackexchange.com/a/99170/1871 $\endgroup$ – xzczd Apr 18 '17 at 13:41
2
$\begingroup$

As ilian says, when you are running Mathematica without a notebook, Echo uses Print directly. But with a notebook, there is a bit of sugar added to make the output distinct from normal Print statements. You can see the little orange arrows beside the cell

enter image description here

If you like, you can redefine it with

Unprotect[Echo];
Echo[expression_] := (Print[expression]; expression)
Protect[Echo];

giving

enter image description here

This also makes Echo just as fast as Print, if that matters to you

$\endgroup$
  • $\begingroup$ Echo doesn't respect Print actions e.g. PrintToConsole, now it will be tied. Also, all that sugar looks nice in messages window anyway so maybe it could be left. $\endgroup$ – Kuba Apr 24 '17 at 12:14
  • $\begingroup$ I don't use this myself, I also like the "Echo" cell look, but I know someone that does they don't like the ~0.02 seconds it takes for the CellPrint[Cell[.. to work. $\endgroup$ – Jason B. Apr 24 '17 at 14:18

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.