Debugging Lua embedded in C ++

1

I am trying to integrate Lua with C ++ and I have some doubts when debugging errors .

When any part of a lua script fails, whether it is a run-time error or a syntax error, Lua returns control to the host program quite silently. Basically luaL_dofile ends up returning 1 .

From what I've been reading in the official reference , when it happens Lua error fills the structure lua_Debug with more detailed information if we call the function lua_getinfo ... or that I thought I understood. : - (

On the one hand I have an intuition about the procedure to obtain more information to debug errors, but in practice ... it does not go.

I paste here the main.cpp code

#include <iostream>
#include "GetHttp.h"
#include "Conf.h"
#include "App.h"
#include "Poco/Logger.h"

extern "C"
{
  #include "lua5.2/lua.h"
  #include "lua5.2/lualib.h"
  #include "lua5.2/lauxlib.h"
}

#include <luabind/luabind.hpp>
#include <luabind/out_value_policy.hpp>
#include <luabind/adopt_policy.hpp>
#include <luabind/return_reference_to_policy.hpp>

using namespace std;

int main(int argc, char **argv)
{
  Conf conf;

  GetHttp ini;
  ini.setConf(&conf);

  conf.logger.information("- Iniciando :-D");
  const char file[]="mods/test.lua";
  lua_State *L = luaL_newstate();
  conf.logger.information("Leido lua_state");

  luaL_openlibs(L);
  conf.logger.information("Leido openlibs");

  luabind::open(L);
  conf.logger.information("Abierto luabind");

  luabind::module(L)
        [
                luabind::class_<GetHttp>("GetHttp")
                        .def(luabind::constructor<>())
                        .def("Get",&GetHttp::miGet)
        ];
 conf.logger.information("Cargado módulo");

 int retorno = luaL_dofile(L,file);
 lua_close(L);
 if(retorno == 0)
 {
        conf.logger.information("Saliendo...");
 }
 else
 {
        cout << "Saliendo con errores" << endl;
        conf.logger.information("Saliendo... con errores");
        lua_Debug ar;
        lua_getglobal(L, "extraerTelefonos"); 
        lua_getinfo(L, ">Sn", &ar);
        cout << "-- " << ar.what << endl;
        cout << "-- " << ar.source << endl;
        cout << "-- " << ar.linedefined << endl;
        cout << "-- " << ar.name << endl;
        cout << "-- " << ar.namewhat << endl;
 }

 return 0;
} 

The second argument of lua_getglobal I put the name of the function that I think is causing the failure ... but I do not know if the second parameter is worth exactly for that.

As you can see at the end of the progroma there is only garbage in lua_Debug .

Program output:

Encontradas 30 coincidencias 
Saliendo con errores
-- C
-- =[C]
-- -1
-- % 

How do I have to do to get information about the errors of a Lua script inside a C ++ program?

What am I doing wrong?

    
asked by Neoniet 15.04.2017 в 21:51
source

1 answer

1
lua_close(L); // <<---
if(retorno == 0)
{
  // ...
}
else
{
  cout << "Saliendo con errores" << endl;
  conf.logger.information("Saliendo... con errores");
  lua_Debug ar;
  lua_getglobal(L, "extraerTelefonos"); 
  lua_getinfo(L, ">Sn", &ar);
  cout << "-- " << ar.what << endl;
  cout << "-- " << ar.source << endl;
  cout << "-- " << ar.linedefined << endl;
  cout << "-- " << ar.name << endl;
  cout << "-- " << ar.namewhat << endl;
}

Move the line indicated at the end of if-else . lua_close releases the resources related to the script, among them are the recoverable data by lua_getinfo .

It should look like this:

if(retorno == 0)
{
  // ...
}
else
{
  cout << "Saliendo con errores" << endl;
  conf.logger.information("Saliendo... con errores");
  lua_Debug ar;
  lua_getglobal(L, "extraerTelefonos"); 
  lua_getinfo(L, ">Sn", &ar);
  cout << "-- " << ar.what << endl;
  cout << "-- " << ar.source << endl;
  cout << "-- " << ar.linedefined << endl;
  cout << "-- " << ar.name << endl;
  cout << "-- " << ar.namewhat << endl;
}
lua_close(L);
    
answered by 18.04.2017 / 14:54
source