开发者

Redirecting/redefining print() for embedded Lua

I have embedded Lua in my C++ application. I want to redirect print statements (or maybe simply redefine the print function?), so that I can display the evaluated expression somewhere else.

What is the best way to do this: redirect or redefining the print() function?

Any snippets/pointers to snippets that show how to do this would be much appreci开发者_运维技巧ated.


You can redefine the print statement in C:

static int l_my_print(lua_State* L) {
    int nargs = lua_gettop(L);

    for (int i=1; i <= nargs; i++) {
        if (lua_isstring(L, i)) {
            /* Pop the next arg using lua_tostring(L, i) and do your print */
        }
        else {
        /* Do something with non-strings if you like */
        }
    }

    return 0;
}

Then register it in the global table:

static const struct luaL_Reg printlib [] = {
  {"print", l_my_print},
  {NULL, NULL} /* end of array */
};

extern int luaopen_luamylib(lua_State *L)
{
  lua_getglobal(L, "_G");
  // luaL_register(L, NULL, printlib); // for Lua versions < 5.2
  luaL_setfuncs(L, printlib, 0);  // for Lua versions 5.2 or greater
  lua_pop(L, 1);
}

Since you are using C++ you'll need to include your file using 'extern "C"'


You can simply redefine print from a Lua script.

local oldprint = print
print = function(...)
    oldprint("In ur print!");
    oldprint(...);
end


See luaB_print in lbaselib.c. The comment there reads:

 /* If you need, you can define your own `print' function, following this
 model but changing `fputs' to put the strings at a proper place (a
 console window or a log file, for instance). */

You can just edit that function or define a new one. This has the advantage of being simple and portable, but it won't handle io.write (which you may or may not care about).

Redirecting IO isn't going to be platform specific (such as SetStdHandle in Windows), but will take care of print and io.write without redefining either.


Write your own C or Lua function and redefine print.


You can just redefine the following macros:

lua_writestring
lua_writeline
lua_writestringerror

to whatever you like. I'm not sure about the lua version where this was introduced - but it works in 5.3 for me.

Check your lauxlib.h or luaconf.h.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜