Nevertheless, the thread was started saying that evaluation of computer 
> generated code was just one of the potential applications of capturing the 
> console output. Logging Julia output to a file was another, and there are 
> several more.
>
> Here we are after 40 posts in this thread and we still don't know, how to 
> capture the Julia REPL output.
>

Short answer: declare your own subtype MyTerminal <: 
Base.Terminals.TextTerminal <: IO, implementing the abstract TextTerminal 
interface in base/Terminals.jl but with a write(t::MyTerminal, x) method 
that also writes x to a file or whatever it is that you want to do with it, 
then set Base.active_repl.t to an instance of MyTerminal.

For example, I put together a quick hack at 
https://gist.github.com/stevengj/88502943fe0478933492 that allows you to 
log the output of the REPL to a file.  Just do
     Base.active_repl.t = LoggingTerminal(Base.active_repl.t, 
open("foo.out", "w"))
and the REPL output will be logged to "foo.out" (though you may need to 
flush(Base.active_repl.t) to see it).

However, the real answer is that hacking the REPL like this exposes a lot 
of undocumented guts, and is ugly, and is probably not the way to do what 
you want. 

* If you want to send commands from an external program to Julia and then 
send the results back, the right thing is probably to implement your own 
read-eval-print loop like in the example code I showed earlier.
        -- If you want to do this while still being able to execute Julia 
commands interactively in the REPL, you can run something like my example 
code in a background task.

* If you want logging of REPL output, and you don't just want to 
copy-and-paste from your terminal into a text editor to save the output, 
then probably you want a more full-featured environment like the IJulia 
notebook.

--SGJ

Reply via email to