catching Lua output

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

catching Lua output

Philipp Kraus
Hello,

how can I catch Lua print statements within my C code?
My Lua scripts run within a simulation on a different host and if the scripts runs a print statement I would like to catch the output and store it in a C variable for send the output back to the client.
I run a lua_call on the C code for running the Lua function.

Thanks

Phil
Reply | Threaded
Open this post in threaded view
|

Re: catching Lua output

Tangent 128
On 12/02/2012 03:18 PM, Philipp Kraus wrote:
> Hello,
>
> how can I catch Lua print statements within my C code?
> My Lua scripts run within a simulation on a different host and if the scripts runs a print statement I would like to catch the output and store it in a C variable for send the output back to the client.
> I run a lua_call on the C code for running the Lua function.
>
> Thanks
>
> Phil
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.)

-Joseph

Reply | Threaded
Open this post in threaded view
|

Re: catching Lua output

Paul K
In reply to this post by Philipp Kraus
Hi Phil,

I was just asking a similar question (but doing this from my Lua code)
and got some good suggestions:
http://lua-users.org/lists/lua-l/2012-11/msg00125.html. I ended up
defining my own "print" function that does what I need.

Paul.

On Sun, Dec 2, 2012 at 12:18 PM, Philipp Kraus
<[hidden email]> wrote:
> Hello,
>
> how can I catch Lua print statements within my C code?
> My Lua scripts run within a simulation on a different host and if the scripts runs a print statement I would like to catch the output and store it in a C variable for send the output back to the client.
> I run a lua_call on the C code for running the Lua function.
>
> Thanks
>
> Phil

Reply | Threaded
Open this post in threaded view
|

Re: catching Lua output

Philipp Kraus
In reply to this post by Tangent 128

Am 02.12.2012 um 21:59 schrieb Tangent 128:

> On 12/02/2012 03:18 PM, Philipp Kraus wrote:
>> Hello,
>>
>> how can I catch Lua print statements within my C code?
>> My Lua scripts run within a simulation on a different host and if the scripts runs a print statement I would like to catch the output and store it in a C variable for send the output back to the client.
>> I run a lua_call on the C code for running the Lua function.
>>
>> Thanks
>>
>> Phil
> 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
Reply | Threaded
Open this post in threaded view
|

Re: catching Lua output

Tangent 128
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

Reply | Threaded
Open this post in threaded view
|

Re: catching Lua output

Philipp Kraus



Am 02.12.2012 um 23:57 schrieb Tangent 128:

> 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
>


Great, thanks, this is perfect :-)

Phil