This answer may receive criticism from some readers, but first, please look at my blog post , which I prepared specifically for this answer, and read the introduction, why I choose this solution.
As promised, I updated my old Lua 5.1 output redirection patch to the latest version.
Patches are available here: 5.1.5 and 5.2.1 .
Patch
patch -p1 < ~/download/lua-5.2.1-output-redirect.patch
Build
cd src make a LUA_A="liblua-5.2.1-redirect.a" SYSCFLAGS="-DLUA_USE_LINUX -DLUA_REDIRECT" SYSLIBS="-Wl,-E -ldl -lreadline -lncurses"
Check
nm liblua-5.xy-redirect.a | grep printf nm liblua-5.xy-redirect.a | grep fputs nm liblua-5.xy-redirect.a | grep fwrite
Test
Get the test program here (C / C ++ mixed, sorry). Build it through:
g++ -DLUA_REDIRECT -I/path/to/lua-5.2.1/src/ -L. lua-redirect-test.cpp -llua-5.2.1-redirect -ldl -o lua-redirect-test
Output:
===== Test 1 output ===== Lua stdout buffer: --- hello world! --- Lua stderr buffer: --- --- Lua error message: --- (null) --- ===== Test 2 output ===== Lua stdout buffer: --- --- Lua stderr buffer: --- --- Lua error message: --- [string "bad_function()"]:1: attempt to call global 'bad_function' (a nil value) ---
source share