lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, 17 Jan 2002 14:37:13 -0000, "Chris Percival"
<cpercival@interaxis.co.uk> wrote:

>Ah ok.  For a bit of background to my project, I will be trying to impliment
>some kind of debugger, but first I just need to be able to have some kind of
>syntax checking so the users of my application can 'test' their code before
>actually running it.  I guess I have three options:
>
>1. Don't do any syntax checking.  The script just gets executed when they
>hit go.  Then I would need to know how to get the debug/error messages
>outputted where I wanted them (not to stdout which I guess they goto at the
>moment).

No, they don't go to stdout, they go to the _ERRORMESSAGE function.
You can override _ERRORMESSAGE (either in Lua or in C) and do whatever
you like.

For example, this saves all error messages in a static string
ourStringOutput (I'm using std::string in C++):

	lua_register(L,"_ERRORMESSAGE", StringPrint);

	static int __cdecl StringPrint(lua_State *L)
	{
		int n = lua_gettop(L);  /* number of arguments */
		int i;
		lua_getglobal(L, "tostring");
		for (i=1; i<=n; i++) {
			const char *s;
			lua_pushvalue(L, -1);  /* function to be
called */
			lua_pushvalue(L, i);   /* value to print */
			lua_rawcall(L, 1, 1);
			s = lua_tostring(L, -1);  /* get result */
			if (s == NULL)
				lua_error(L, "`tostring' must return a
string to `print'");
			if (i>1) ourStringOutput += "\t";
			ourStringOutput += s;
			lua_pop(L, 1);  /* pop result */
		}
		ourStringOutput += "\n";
		return 0;
	}


Francis