Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何替换Lua默认错误打印?_C++_Lua_Win32gui - Fatal编程技术网

C++ 如何替换Lua默认错误打印?

C++ 如何替换Lua默认错误打印?,c++,lua,win32gui,C++,Lua,Win32gui,我正在将Lua作为脚本语言实现到Windows应用程序中。由于应用程序的结构,打印输出不使用流式io,例如stdout和stderror 我已成功覆盖Lua打印以适应我的结构 lua_register(L,"print", cs_print); …但如何在不使用流的情况下覆盖所有错误和调试打印输出?我需要在一个函数中处理这个问题(类似于print)。Lua写入stderr的唯一位置是安装的panic函数。如果要将Lua嵌入到应用程序中,请确保从受保护的调用启动Lua,并且不会出现死机。有关想法

我正在将Lua作为脚本语言实现到Windows应用程序中。由于应用程序的结构,打印输出不使用流式io,例如
stdout
stderror

我已成功覆盖Lua
打印
以适应我的结构

lua_register(L,"print", cs_print);

…但如何在不使用流的情况下覆盖所有错误和调试打印输出?我需要在一个函数中处理这个问题(类似于
print
)。

Lua写入
stderr
的唯一位置是安装的panic函数。如果要将Lua嵌入到应用程序中,请确保从受保护的调用启动Lua,并且不会出现死机。有关想法,请参阅。

在谷歌搜索了很多次之后,我想出了这个解决方案来获取编译器和运行时错误消息,以及重定向standar Lua打印函数

<>我使用C++ Builder,但我认为对于任何寻找相同答案的人来说都是有用的。 该脚本在TScriptLua对象实例中运行,为了将Lua状态映射到正确的脚本实例,我使用std::map列表查找对象指针

// list for mapping Lua state with object pointers.
static std::map<lua_State*,TScriptLua*> LuaObjMap;
此功能将取代标准Lua打印功能。对象指针(f)的基类中有一个名为Out()的成员函数,该函数将在关联的窗口控件中输出一个字符缓冲区

   // New Lua print function
   static int cs_print (lua_State *L) {

     // Map Lua state to object
     TScriptLua* f = GetScriptObject(L);

     if (f) {
       int count = lua_gettop(L);
       for (int i=1; i <= count; ++i) {
         const char *str = lua_tostring(L, i); // Get string
         size_t len = lua_rawlen(L,i);         // Get string length

         // Output string.
         f->Out(str,len);
       }
     }

     return 0;
   }
//新的Lua打印函数
静态int cs_打印(lua_状态*L){
//将Lua状态映射到对象
TScriptLua*f=GetScriptObject(L);
如果(f){
int count=lua_gettop(L);
for(int i=1;i Out(str,len);
}
}
返回0;
}
这是我的错误打印例程,将显示编译器/运行时错误。对于Lua打印函数,再次使用f->Out打印错误消息

   // Error print routine
   static int cs_error(lua_State *L, char *msg) {

     // Map Lua state to object
     TScriptLua* f = GetScriptObject(L);

     // Get error message
     AnsiString m = String(msg) + " " + String(lua_tostring(L, -1));

     // "print" error message
     f->Out(m.c_str(),m.Length());

     // Clenaup Lua stack
     lua_pop(L, 1);

     return 0;
   }

 } // <--- End extern C
//错误打印例程
静态int cs_错误(lua_State*L,char*msg){
//将Lua状态映射到对象
TScriptLua*f=GetScriptObject(L);
//获取错误消息
AnsiString m=String(msg)+“”+String(lua_-tostring(L,-1));
//“打印”错误消息
f->Out(m.c_str(),m.Length());
//Clenaup Lua栈
卢厄波普(L,1);
返回0;
}
}//对象
insert(std::pair(L,this));
//覆盖Lua打印
lua_寄存器(L,“打印”,cs_打印);
//加载并编译脚本
翻译脚本(框架->脚本编辑->文本);
if(luaL_loadbuffer(L,script.c_str(),script.Length(),AnsiString(Name).c_str())==0){
if(lua_pcall(L,0,0,0))//运行加载的lua脚本
cs_错误(L,“运行时错误:”);//打印运行时错误
}否则{
cs_错误(L,“编译器错误:”);//打印编译器错误
}
//关闭Lua状态
卢厄关闭(L);
//删除Lua-->对象映射
LuaObjMap.erase(LuaObjMap.find(L));
}

这不是我的最终解决方案,但到目前为止,它确实起到了作用。我认为最好的办法是将流处理写入TScriptLua对象,这样我就可以直接将它注册到Lua中。

问题不是我想隐藏错误消息,而是想在特定的窗口控件中显示它们。因此,我需要在一个有趣的窗口中捕获所有与错误相关的消息操作,以便在正确的窗口中打印它们。如上所述,这不是嵌入应用程序,而是Win32 GUI应用程序。@MaxKielland,当您使用受保护的调用启动Lua时,错误消息会留在Lua堆栈上。是的,经过一天的Google搜索后,我发现我可能需要使用Lua_pcall()而不是luaL_dostring()要获取编译器错误。@MaxKielland,
luaL\u-dostring
工作正常:它调用
lua\u-pcall
。请参阅。
   // Error print routine
   static int cs_error(lua_State *L, char *msg) {

     // Map Lua state to object
     TScriptLua* f = GetScriptObject(L);

     // Get error message
     AnsiString m = String(msg) + " " + String(lua_tostring(L, -1));

     // "print" error message
     f->Out(m.c_str(),m.Length());

     // Clenaup Lua stack
     lua_pop(L, 1);

     return 0;
   }

 } // <--- End extern C
void __fastcall TScriptLua::Compile(void) {

   // Create new Lua state
   lua_State *L = luaL_newstate();

   // Store mapping Lua state --> object
   LuaObjMap.insert( std::pair<lua_State*,TScriptLua*>(L,this) );

   // Override Lua Print
   lua_register(L,"print",  cs_print);

   // Load and compile script
   AnsiString script(Frame->Script_RichEdit->Text);
   if (luaL_loadbuffer(L,script.c_str(),script.Length(),AnsiString(Name).c_str()) == 0) {
     if (lua_pcall(L, 0, 0, 0))        // Run loaded Lua script
       cs_error(L, "Runtime error: "); // Print runtime error
   } else {
     cs_error(L, "Compiler error: ");  // Print compiler error
   }

   // Close Lua state
   lua_close(L);

   // Remove Lua --> object mapping
   LuaObjMap.erase( LuaObjMap.find(L) );

 }