Stdout

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

Stdout

Nick Trout-2
With respect to the output stream problem on Windows. ie. you cannot
redirect stdout easily to catch Lua output.

How about adding:

typedef void (*lua_print)(char*,...);
lua_print lua_stderr = lua_defaulterr;
lua_print lua_stdout = lua_defaultout;

then all Lua output can be sent to lua_stdout & lua_stderr,

eg.
lua_stderr("An error occured %s", "this file.");

instead of


fprintf(stderr,"An error occured %s", "this file.");

with default values:

void lua_defaulterr(char* p_print,...)
{
 va_list ap;
    va_start(ap, p_print);
 vfprintf(stderr,p_print, ap);
    va_end(ap);
}

void lua_defaultout(char* p_print,...)
{
 va_list ap;
    va_start(ap, p_print);
 vfprintf(stdout,p_print, ap);
    va_end(ap);
}

then you can use change lua_stdout to, say:

void lua_mystdout(char* fmt,...)
{
 char buff[256];
 va_list arglist;

 va_start(arglist, fmt);
 vsprintf(buff, fmt, arglist);
 va_end(arglist);

 lua_error(luastate,buff);
}


It's a bit more elegant than macros?

Nick



----------------------------------------------------------------------------
------------
This email transmission and the information contained herein or attached
hereto is intended only for the person or entity to which it is addressed
and may contain confidential and/or privileged material.

Any review, transmission, disclosure, distribution, copying, dissemination
or other use of, or taking of action in reliance upon, this information by
persons or entities other than the intended recipient is prohibited.

If you have received this communication in error, please contact Video
System Ltd. at [hidden email] and delete the material from any
computer.

Video System Ltd. +44 (0)161 955 4402
----------------------------------------------------------------------------
------------


Reply | Threaded
Open this post in threaded view
|

Re: Stdout

Reuben Thomas-3
> With respect to the output stream problem on Windows. ie. you cannot
> redirect stdout easily to catch Lua output.

See previous messages. There are very few functions that use _OUTPUT: print
and write are the only ones that spring to mind. You can trap these. Ditto
for _INPUT and read. If you want to use the handles directly in your own
routines it's admittedly trickier.

> [suggestions snipped]

This is all be done within Lua already: _STDOUT, _STDIN and _STDERR.

-- 
http://sc3d.org/rrt/ | aphorism, n.  a wise lie


Reply | Threaded
Open this post in threaded view
|

Re: Stdout

Nick Trout-2
> With respect to the output stream problem on Windows. ie. you cannot
> redirect stdout easily to catch Lua output.
>>See previous messages. There are very few functions that use _OUTPUT:
print
and write are the only ones that spring to mind. You can trap these. Ditto
for _INPUT and read. If you want to use the handles directly in your own
routines it's admittedly trickier.
>>This is all be done within Lua already: _STDOUT, _STDIN and _STDERR.

so if i have a wrapped funtion say, (I'm using toLua) :

void tprint(char *p)
{
 printf("t: %s",p);
}

and I redirect stdout to this,

lua_dostring(state,"_STDOUT = tprint");

All stdout should be redirected to tprint()? It doesnt appear to be (I may
have made a mistake somewhere?!).

Nick





Reply | Threaded
Open this post in threaded view
|

Re: Stdout

Reuben Thomas-3
> lua_dostring(state,"_STDOUT = tprint");

This doesn't work, because _STDOUT is a handle. You can't replace a handle
with a function and expect things to work. Like I said before, you have to
redefine print and write. Can I make myself clearer somehow?

-- 
http://sc3d.org/rrt/ | humour, n.  unexpected recognition


Reply | Threaded
Open this post in threaded view
|

Re: Stdout

Nick Trout-2
> lua_dostring(state,"_STDOUT = tprint");

>>This doesn't work, because _STDOUT is a handle. You can't replace a handle
with a function and expect things to work. Like I said before, you have to
redefine print and write.

Right okay, I have been fiddling around with the code so I wasnt sure if I
had broken something in trying this. It would be nice if this functionality
worked?

My suggestion for adding lua_stdout and lua_stderr allows you to redefine
stdout and stderr at runtime if you dont have the advantage of being able to
redirect these streams. eg. in Windows. You may want to do this for example
if you use Lua to load some kind of debug library at runtime which catches
stderr. I realise I can edit the Lua code but it would be nice if this were
the default functionality.

>> Can I make myself clearer somehow?

I don't think we need the sarcasm but thanks for the advice.




Reply | Threaded
Open this post in threaded view
|

Re: Stdout

Reuben Thomas-3
> >> Can I make myself clearer somehow?
>
> I don't think we need the sarcasm but thanks for the advice.

Sorry, no sarcasm intended. I often feel I can't keep up with the economy,
precision and clarity of Lua's designers and authors on this list, which is
pretty embarrassing considering English is my native language.

-- 
http://sc3d.org/rrt/
"In 1727, Helen Morrison, a lonely spinster, became the first woman to
place a Lonely Hearts advertisement. It appeared in the Manchester Weekly
Journal. The mayor promptly committed her to a lunatic asylum for four
weeks." - The People's Almanac #2


rje
Reply | Threaded
Open this post in threaded view
|

Re: Stdout

rje
On Wed, Jan 24, 2001 at 11:19:36AM +0000, Reuben Thomas wrote:
> > >> Can I make myself clearer somehow?
> >
> > I don't think we need the sarcasm but thanks for the advice.
> 
> Sorry, no sarcasm intended. I often feel I can't keep up with the economy,
> precision and clarity of Lua's designers and authors on this list, which is
> pretty embarrassing considering English is my native language.

Often people who speak English (or any other language, I imagine) as a
secondary language often speak and write it better than those who speak
it nativly.  This is mainly due to them learning from the rules, rather
than learning from other people who may introduce mistakes, slang etc.

-- 
Rob Kendrick - http://www.digital-scurf.org/
Do something unusual today.  Pay a bill.

Reply | Threaded
Open this post in threaded view
|

RE: Stdout

Falko Poiker
In reply to this post by Nick Trout-2
>> lua_dostring(state,"_STDOUT = tprint");

>>This doesn't work, because _STDOUT is a handle. You can't replace a handle
>>with a function and expect things to work. Like I said before, you have to
>>redefine print and write.

> Right okay, I have been fiddling around with the code so I wasnt sure if I
> had broken something in trying this. It would be nice if this
functionality
> worked?

> My suggestion for adding lua_stdout and lua_stderr allows you to redefine
> stdout and stderr at runtime if you dont have the advantage of being able
to
> redirect these streams. eg. in Windows. You may want to do this for
example
> if you use Lua to load some kind of debug library at runtime which catches
> stderr. I realise I can edit the Lua code but it would be nice if this
were
> the default functionality.

Can't the lua library function "writeto" be used to change stdout at
runtime?  I don't know about stderr though...

Falko


Reply | Threaded
Open this post in threaded view
|

Re: Stdout

Nick Trout-2
> My suggestion for adding lua_stdout and lua_stderr allows you to redefine
> stdout and stderr at runtime if you dont have the advantage of being able
to
> redirect these streams. eg. in Windows. You may want to do this for
example
> if you use Lua to load some kind of debug library at runtime which catches
> stderr. I realise I can edit the Lua code but it would be nice if this
were
> the default functionality.

>> Can't the lua library function "writeto" be used to change stdout at
runtime?  I don't know about stderr though...

The manual says (sect 6.4) you can redirect _OUTPUT into a given file using
writeto(filename). Errors, or stderr, can be caught by redefining the
_ALERT() function. It would be nice to be able to redirect stdout as you can
stderr (using _ALERT) something like _WRITE(text). I dont think you can do
that? (Sorry I'm still a learner) You actually have to alter Lua code to do
this. So with a standard installation of Lua I could do:

lua_dostring(state,"_WRITE,_ALERT = mywrite,myalert");

and all my output is redirected to my handlers.

The changes I suggested before would allow you to do the same thing in code
but all calls to stdout and stderr would have to been done through the
lua_stdout & lua_stderr functions. This could make the above quite easy to
implement.

Do this make any sense! Maybe I just need to get into more of a Lua
mindset?!! :-)

Nick


Reply | Threaded
Open this post in threaded view
|

Re: Stdout

Reuben Thomas-3
> The manual says (sect 6.4) you can redirect _OUTPUT into a given file using
> writeto(filename). Errors, or stderr, can be caught by redefining the
> _ALERT() function. It would be nice to be able to redirect stdout as you can
> stderr (using _ALERT) something like _WRITE(text). I dont think you can do
> that? (Sorry I'm still a learner) You actually have to alter Lua code to do
> this. So with a standard installation of Lua I could do:

You can just reassign _STDOUT...

> Maybe I just need to get into more of a Lua mindset?!! :-)

Quite possibly. I've found over and over again that things I can't see how
to do without changing Lua are in fact trivial.

-- 
http://sc3d.org/rrt/
"In 1727, Helen Morrison, a lonely spinster, became the first woman to
place a Lonely Hearts advertisement. It appeared in the Manchester Weekly
Journal. The mayor promptly committed her to a lunatic asylum for four
weeks." - The People's Almanac #2


Reply | Threaded
Open this post in threaded view
|

Re: Stdout

Luiz Henrique de Figueiredo
In reply to this post by Nick Trout-2
>You can just reassign _STDOUT...

Reassigning _STDOUT (or _STDIN or _STDERR) does nothing.
Reassigning _OUTPUT and _INPUT does.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Stdout

Nick Trout-2
>You can just reassign _STDOUT...

>>Reassigning _STDOUT (or _STDIN or _STDERR) does nothing.
Reassigning _OUTPUT and _INPUT does.
--lhf

Aha! That works if I use write().

Can I redirect output (ie. output from write()) into a memory buffer/Lua
function without altering the Lua source?

If this is not possible consistency would point to me being able to do this
with stdout? i.e. I can redirect stderr to one of my functions using _ALERT.

N






Reply | Threaded
Open this post in threaded view
|

Re: Stdout

Luiz Henrique de Figueiredo
In reply to this post by Nick Trout-2
>Can I redirect output (ie. output from write()) into a memory buffer/Lua
>function without altering the Lua source?

Yes, by redefining write itself. Something like this:

 _OUTBUF=""

 function write(...)
  if arg[1]==_OUTPUT or tag(arg[1])~=tag(_OUTPUT) then
   local i0
   if arg[1]==_OUTPUT then i0=2 else i0=1 end
   for i=i0,arg.n do
    _OUTBUF=_OUTBUF..arg[i] 
   end
  else
   call(%write,arg)
  end
 end


--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Stdout

Nick Trout-2
Of course! and the same with print:

function print(x) %print('redirected:'..x) end

Cheers,
Nick



>Can I redirect output (ie. output from write()) into a memory buffer/Lua
>function without altering the Lua source?

Yes, by redefining write itself. Something like this:

 _OUTBUF=""

 function write(...)
  if arg[1]==_OUTPUT or tag(arg[1])~=tag(_OUTPUT) then
   local i0
   if arg[1]==_OUTPUT then i0=2 else i0=1 end
   for i=i0,arg.n do
    _OUTBUF=_OUTBUF..arg[i] 
   end
  else
   call(%write,arg)
  end
 end

--lhf