[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: catching Lua output
- From: Tangent 128 <tangent128@...>
- Date: Sun, 02 Dec 2012 17:57:49 -0500
On 12/02/2012 04:11 PM, Philipp Kraus wrote:
> Am 02.12.2012 um 21:59 schrieb Tangent 128:
>
>> <snip>
>> You want to define your own print function, I think.
>>
>> After you've opened any standard libraries into the Lua state, set the
>> global "print" value to your custom function.
>>
>> See lua_pushcfunction, lua_CFunction, and lua_setglobal in the docs.
>>
>> (This would not intercept write()s from the io library, but I am
>> guessing that that isn't used by simulation actors.)
> can I catch also the io.write calls? I will add also my own "simulation package"
> with some different function, so I can add my own print call, but on usability I
> would like to catch the out (if I can), because some user will use the io.write
> and the output is lost, if there is no catching
>
> Phil
All the libraries are just functions kept in tables; you can replace any
of them with your own versions before you let the user code run. You
just need to write a replacement.
Depending on the application though, you may not need to provide the io
library to the user scripts in the first place.
And there are two limited solutions that don't require custom functions
at all:
* if you don't otherwise use the stdout of your Lua-running process, you
can use dup() and pipe() system calls to capture stdout directly.
* you can use io.output(filename) to direct the io.write() function's
output to a temporary file, and then send its contents back to the user
afterwards.
-Joseph