Lua & C++ API getting the executing information - c++

In Lua I have a function called utils.debug() and what I would like to do is use it in my Lua code as follows:
function Foo:doSomething
if (/* something */) then
print("Success!")
else
utils.debug()
end
end
function Foo:doSomethingElse
if (/* something else */) then
print("Awesome!")
else
utils.debug()
end
end
I would like to use it throughout my Lua code to help me debug. As a result, I would like my C++ code to know where in the Lua code the utils.debug() was called from. I looked into lua_Debug and lua_getinfo and they seem pretty close to what I want, but I'm missing a piece:
int MyLua::debug(lua_State* L)
{
lua_Debug ar;
lua_getstack(L, 1, &ar);
lua_getinfo(L, ??????, &ar);
// print out relevant info from 'ar'
// such as in what function it was called, line number, etc
}
Is this what the lua_Debug struct is for or is there another facility or method I should use to do this?

This is what I use to produce a Lua stack trace:
lua_Debug info;
int level = 0;
while (lua_getstack(l, level, &info)) {
lua_getinfo(l, "nSl", &info);
fprintf(stderr, " [%d] %s:%d -- %s [%s]\n",
level, info.short_src, info.currentline,
(info.name ? info.name : "<unknown>"), info.what);
++level;
}
See the documentation for lua_getinfo for more info.

Related

LuaBind c++ error handler when calling a Lua function

I am little newbie in C++ scripting but I know some things.
I'm compiling a plugin that use a function to call an Lua callback using the LuaBind, but the client crashes when function doesn't exists on main.lua, also I'm trying to add an error handler to that functions... I don't know what I can do to solve it.
Here is my code:
{
try
{
luabind::call_function<int>(L, "onServerFrame", elapsedTime);
}
catch (luabind::error& e)
{
std::string error = lua_tostring(e.state(), -1);
std::cout << error << "\n";
}
}
Solved. Thanks to my friend habi.
luabind::object func = g[name];
if( func ) { if( luabind::type(func) == LUA_TFUNCTION ) luabind::call_function<void>(L,"onServerFrame", elapsedTime);}

per thread c++ guard to prevent re-entrant function calls

I've got function that call the registry that can fail and print the failure reason.
This function can also be called directly or indirectly from the context of a dedicated built-in printing function, and I wish to avoid printing the reason in this case to avoid endless recursion.
I can use thread_local to define per thread flag to avoid calling the print function from this function, but I guess it's rather widespread problem, so I'm looking for std implementation for this guard or any other well debugged code.
Here's an example that just made to express the problem.
Each print function comes with log level, and it's being compared with the current log level threshold that reside in registry. if lower than threshold, the function returns without print. However, in order to get the threshold, additional print can be made, so I wanted to create a guard that will prevent the print from getPrintLevelFromRegistry if it's called from print
int getPrintLevelFromRegistry() {
int value = 0;
DWORD res = RegGetValueW("//Software//...//logLevel" , &value);
if (res != ERROR_SUCCESS) {
print("couldn't find registry key");
return 0;
}
return value;
}
void print(const char * text, int printLoglevel) {
if (printLogLevel < getPrintLevelFromRegistry()) {
return;
}
// do the print itself
...
}
Thanks !
The root of the problem is that you are attempting to have your logging code log itself. Rather than some complicated guard, consider the fact that you really don't need to log a registry read. Just have it return a default value and just log the error to the console.
int getPrintLevelFromRegistry() {
int value = 0;
DWORD res = RegGetValueW("//Software//...//logLevel" , &value);
if (res != ERROR_SUCCESS) {
OutputDebugStringA("getPrintLevelFromRegistry: Can't read from registry\r\n");
}
return value;
}
Further, it's OK to read from the registry on each log statement, but it's redundant and unnecessary.
Better:
int getPrintLevelFromRegistry() {
static std::atomic<int> cachedValue(-1);
int value = cachedValue;
if (value == -1) {
DWORD res = RegGetValueW("//Software//...//logLevel" , &value);
if (res == ERROR_SUCCESS) {
cachedValue = value;
}
}
return value;
}

Construction convention for multiple if method

I wounder if there is some construction convention for constructing method with multiple if's. Most of the time before you fire your method you have to check input arguments and other things, eg. is not nullptr, is > 0, is != -1 etc. Sometimes you cannot check this in one if and as a result you have something like this:
if(arg != nullptr)
{
if()
{
if()
{
if()
{
/*actual code here*/
}
else
{
}
}
else
{
}
}
else
{
/* other error message like "License check error: wrong key!" */
}
}
else
{
/* wrong input args! */
}
Good convention is that your line has less than 80 characters which gives us less space for actual code. Code is getting more and more unreadable.
You could return early in the case of issues, or throw an exception:
if(arg == nullptr) {
log("arg was null, not doing anything");
return;
}
//if the user forgot to make the toast, we can do it for them
if(forgotToMakeToast) {
makeToast();
}
if(ranOverDog) {
//we can't continue if the user ran over our dog, throw an exception
throw too_angry_exception;
}
//actual code
This makes your code structure more obvious by relating the error handling to the error checking by locality.
What I usually do is something like this:
if(arg == nullptr)
{
/* wrong input args! */
return;
}
if()
{
/* other error message like "License check error: wrong key!" */
return;
}
...
/*actual code here*/
Then you have all your error "ifs" and error handling in one place, and the actual function code at the end, nicely separated.
When you have too many sub-levels of if, while, for in a function, it is a sign that the function should be split into 2 or more separate functions. Depending on specific code it could look something like this:
public void MyClass::Run(arg)
{
if(arg != nullptr)
{
if()
{
RunActualCode()
}
else
{
/* other error message like "License check error: wrong key!" */
}
}
else
{
/* wrong input args! */
}
}
private void MyClass::RunActualCode(...)
{
if()
{
if()
{
/*actual code here*/
}
else
{
}
}
else
{
}
}
There are many recommendation about this, for example:
Rec 4.7 Do not have too complex functions.
Everyone that has ever had to take over code written by someone else
knows that complex code is hard to maintain. There are many ways in
which a function can be complex, such as the number of lines of code,
the number of parameters, or the number of possible paths through a
function. The number of possible paths through a function, which is
the result from the use of many control flow primitives, is the main
reason to why functions are complex. Therefore you should be aware of
the fact that heavy use of control flow primitives will make your code
more difficult to maintain.
http://www.tiobe.com/content/paperinfo/CodingStandards/hem/industrial/bookindex.htm
Limiting complexity during development
Your original construction could be written like this:
do
{
if(nullptr == arg) // Note: *negate* your original conditions!
{
/* wrong input args! */
break;
}
if(...)
{
/* other error message like "License check error: wrong key!" */
break;
}
if(...)
{
...
break;
}
if(...)
{
...
break;
}
/*actual code here*/
} while (0);
Advantages:
no nested ifs;
use break instead of goto to jump out of the whole block;
the logic is clearer, and more maintainable: if you want to add a check guard, just append one more if(...){...; break;};
Disadvantages:
do-while(0) looks a bit strange;
you should negate all your original conditions, e.g. if(cond) => if(!cond), which may affect the code clarity;

Creating Timers in C++ using Lua

I was wondering whether the following setup would work for a small game:
Lets assume I have the following functions registered to Lua like so:
lua_register(L, "createTimer", createTimer);
lua_register(L, "getCondition", getCondition);
lua_register(L, "setAction", setAction);
Where: (leaving the type checking behind)
int createTimer(lua_State* L){
string condition = lua_tostring(L, 1);
string action = lua_tostring(L, 2);
double timer = lua_tonumber(L, 3);
double expiration = lua_tonumber(L, 4);
addTimer(condition, action, timer, expiration); // adds the "timer" to a vector or something
return 1;
}
Calling this function in lua by:
createTimer("getCondition=<5", "setAction(7,4,6)", 5, 20);
Can I then do the following(?):
// this function is called in the game-loop to loop through all timers in the vector
void checkTimers(){
for(std::vector<T>::iterator it = v.begin(); it != v.end(); ++it) {
if(luaL_doString(L, *it->condition)){
luaL_doString(L, *it->action)
}
}
}
Would this work? Would luaL_doString pass "getCondition=<5" to the lua state engine, where it will call the c++ function getCondition(), then see if it is =<5 and return true or false? And would the same go for luaL_doString(L, "setAction(7, 4, 6)"); ?
Moreover, would this be a suitable way to create timers by only accessing lua once (to create them) and let c++ handle the rest, only calling the c++ functions through lua and letting lua deal with logic only?
Thanks in advance.
You may want to change the condition string to "return getCondition()<=5" otherwise the string chunk will not compile or run. Then check the boolean return value on the stack when the luaL_doString() returns successfully. Something like this:
// this function is called in the game-loop to loop through all timers in the vector
void checkTimers(){
for(std::vector<T>::iterator it = v.begin(); it != v.end(); ++it) {
lua_settop(L, 0);
if(luaL_doString(L, *it->condition) == 0 && lua_toboolean(1)){
luaL_doString(L, *it->action);
}
}
}
You cannot interrupt Lua while it is running. The best you can do is to set a flag and then handle the interruption at a safe time. The standalone interpreter uses this technique to handle user interrupts (control-C). This technique is also used in my lalarm library, which can be used to implement timer callbacks, though not at the high level you want.

Lua toString exception for boolean value

I wrote a simple lua function which uses 'C++' function to execute. As my intention of creating a 'C++' function is to use the same across all lua functions to update the 'C++' variables. It works fine for numbers, but when I tried it for boolean values, it give me exception when convert to string.
Here is my code snippet.
C++ code.
#include <lua.hpp>
/* the Lua interpreter */
lua_State *luaState;
std::map<lua_State *, CLuaTest *> luaFbtlookup;
void CLuaTest::CLuaTest() {
// initialize Lua
luaState = luaL_newstate();
lua_register(luaState, "get_value", get_value); // func to get values
lua_register(luaState, "set_value", set_value); // func to set values
// load Lua base libraries
luaL_openlibs(luaState);
luaL_dofile(luaState, "C:\LuaTest.lua");
luaFbtlookup.insert(make_pair(luaState, this));
}
int get_value(lua_State *L);
int set_value(lua_State *L);
extern "C++" int get_value(lua_State *L)
{
string lightName = lua_tostring(L, 1);
FbTLuaLookup::iterator iter = luaFbtlookup.find(L);
if (iter != luaFbtlookup.end()) {
lua_pushstring(L, iter->second->getValueFrom(lightName).c_str());
return 1; // do not return zero
}
return 1;
}
extern "C++" int set_value(lua_State *L)
{
string lightName = lua_tostring(L, 1);
if (NULL == lua_tostring(L, 2))
{
printf("WARNING : Invalid String Argument / Cannot convert arg#2 to string");
}
else {
string value = lua_tostring(L, 2);
FbTLuaLookup::iterator iter = luaFbtlookup.find(L);
if (iter != luaFbtlookup.end()) {
iter->second->setValueTo(lightName, value);
lua_pushnumber(L, true);
return 1; // do not return zero
}
}
return 1;
}
CLuaTest::ExecuteScript(enum Seq) {
switch(Seq) {
case 0:
lua_getglobal(luaState, "AllLightsOff");
break;
case 1:
lua_getglobal(luaState, "RedLightOn");
break;
case 2:
lua_getglobal(luaState, "AmberLightOn");
break;
case 3:
lua_getglobal(luaState, "GreenLightOn");
break;
}
}
My lua script:
function AllLightsOff
set_value("RedLight", 0)
set_value("AmberLight",0)
set_value("GrenLight",0)
end
function RedLightOn
set_value("RedLight", 1)
set_value("AmberLight",0)
set_value("GrenLight",0)
end
function AmberLightOn
set_value("RedLight", 0)
set_value("AmberLight",1)
set_value("GrenLight",0)
end
function GreenLightOn
set_value("RedLight", 0)
set_value("AmberLight",0)
set_value("GrenLight",1)
end
Things work fine with the above code, but when I tried to change the set value to use boolean values like set_value("RedLight", False) I get warning message printing. Should I need to pass False as string?
Lua doesn't have False, so it simply tries to read global _G["False"] which is of course nil.
It has false keyword, however, but it wouldn't work either: lua_tostring is working only for numbers and strings.
We don't see setValueTo code, so it is hard to guess how it works.
If you simply want to pass bool value to it, use lua_toboolean, but be aware that it treats 0 as true (as Lua in general).
If you want to pass "True" or "False" as strings, then yes, you must write set_value("RedLight", "False")
As a side note, consider reading how to implement lua_CFunction protocol. Now, if get_value can't find lightName, it will return the last passed parameter as its result.