I am using Luainterface 2.0.3 to embed Lua in a c# application.
Everything is working fine, except in visual Studio's debug mode, the Lua's print function does not get written to the console (nor to Output).
using System;
using LuaInterface;
namespace Lua1 {
class Program {
static void Main(string[] args) {
Lua lua = new Lua();
lua.DoString("print 'Hello from Lua!'");
}
}
}
Running it in non debugging mode, print is working fine.
Am I missing something?
Thanks!
I haven't used LuaInterface, so I can't say for sure, but you might want to try manually calling
Looking at Programming in Lua ch 21.1, they explain how you can redirect where
print
's output goes by setting io.output. See also the IO Library Tutorial.I'm unsure whether this will actually solve the problem, however, as I can't find anything related to io.output being set in the LuaInterface source on Google Code.
Here a code sample how to use the LuaDLL class used in the LuaInterface to redirect the lua print function:
The initialization of lua in C# look like:
Unfortunately, you are probably up against a known deficiency in the
print()
function, which is really intended for quick and dirty debugging at a console prompt, and is missing some necessary flexibility.The base library function
print()
implemented byluaB_print()
in lbaselib.c explicitly uses the C runtime'sstdout
stream as its destination. Since it refers to the global variablestdout
explicitly in its implementation, the only way to redirect it is to cause that file handle to be redirected. In a C program that can be done by callingfreopen(stdout,...)
. Unfortunately, there isn't a stock library function in Lua that can do that.The
io
library is implemented in liolib.c. It uses the function environment to hold a table of open file descriptors, and during its initialization it createsfile
objects namedio.stdin
,io.stdout
andio.stderr
for the three standard descriptors. It also provides functions namedio.output
andio.input
to allow those two descriptors to be modified to point to any openfile
object (or a newly opened file if a file name is passed). However, those functions only change the function environment table and do not callfreopen()
to modify the C runtime's table ofFILE
values.I have no idea how LuaInterface attempts to treat the standard C runtime's idea of
stdout
. It may very well be thatstdout
is not connected to anything useful in the VS debugger because it probably takes advantage of some .NET feature to capture output from the module being debugged that may not be all that compatible with C in any case.That said, it is easy to replace the standard
print
function. Just use existing features of LuaInterface to write a global function namedprint
that callstostring()
on each argument and passes it to the whatever .NET things is the standard output device.Replace line 7 in file "lua/etc/luavs.bat"
with
and recompile lua with the debug version of MSVCRT. After that your output will be redirected as expected.