Skip to content

Commit

Permalink
Add file and line to NodeDef field deprecation warning
Browse files Browse the repository at this point in the history
WARNING[Main]: Field dug_item: Deprecated; use 'drop' field (at ...netest/bin/../games/minetest_game/mods/default/nodes.lua:1668)
  • Loading branch information
rubenwardy committed Dec 10, 2017
1 parent 02cc257 commit ade7515
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/script/common/c_content.cpp
Expand Up @@ -1109,7 +1109,8 @@ void warn_if_field_exists(lua_State *L, int table,
lua_getfield(L, table, name);
if (!lua_isnil(L, -1)) {
warningstream << "Field \"" << name << "\": "
<< message << std::endl;
<< message << " (at " << script_get_caller_file_line(L, 1) << ")" << std::endl;

infostream << script_get_backtrace(L) << std::endl;
}
lua_pop(L, 1);
Expand Down
20 changes: 15 additions & 5 deletions src/script/common/c_internal.cpp
Expand Up @@ -29,6 +29,19 @@ std::string script_get_backtrace(lua_State *L)
return luaL_checkstring(L, -1);
}

std::string script_get_caller_file_line(lua_State *L, unsigned int offset)
{
lua_Debug ar;

if (!lua_getstack(L, 1 + offset, &ar))
return "error-badstack:0";

if (!lua_getinfo(L, "Sl", &ar))
return "error-badgetinfo:0";

return std::string(ar.short_src) + ":" + std::to_string(ar.currentline);
}

int script_exception_wrapper(lua_State *L, lua_CFunction f)
{
try {
Expand Down Expand Up @@ -132,7 +145,7 @@ void script_run_callbacks_f(lua_State *L, int nargs,
lua_remove(L, error_handler);
}

void log_deprecated(lua_State *L, const std::string &message)
void log_deprecated(lua_State *L, const std::string &message, unsigned int offset)
{
static bool configured = false;
static bool do_log = false;
Expand All @@ -152,10 +165,7 @@ void log_deprecated(lua_State *L, const std::string &message)
if (do_log) {
warningstream << message;
if (L) { // L can be NULL if we get called from scripting_game.cpp
lua_Debug ar;
FATAL_ERROR_IF(!lua_getstack(L, 2, &ar), "lua_getstack() failed");
FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed");
warningstream << " (at " << ar.short_src << ":" << ar.currentline << ")";
warningstream << " (at " << script_get_caller_file_line(L, offset) << ")";
}
warningstream << std::endl;

Expand Down
3 changes: 2 additions & 1 deletion src/script/common/c_internal.h
Expand Up @@ -99,9 +99,10 @@ enum RunCallbacksMode
};

std::string script_get_backtrace(lua_State *L);
std::string script_get_caller_file_line(lua_State *L, unsigned int offset = 0);
int script_error_handler(lua_State *L);
int script_exception_wrapper(lua_State *L, lua_CFunction f);
void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
void script_run_callbacks_f(lua_State *L, int nargs,
RunCallbacksMode mode, const char *fxn);
void log_deprecated(lua_State *L, const std::string &message);
void log_deprecated(lua_State *L, const std::string &message, unsigned int offset = 0);
2 changes: 1 addition & 1 deletion src/script/lua_api/l_util.cpp
Expand Up @@ -59,7 +59,7 @@ int ModApiUtil::l_log(lua_State *L)
std::string name = luaL_checkstring(L, 1);
text = luaL_checkstring(L, 2);
if (name == "deprecated") {
log_deprecated(L, text);
log_deprecated(L, text, 1);
return 0;
}
level = Logger::stringToLevel(name);
Expand Down

0 comments on commit ade7515

Please sign in to comment.