Use warningstream for deprecated field messages and refactor log_deprecated

This commit is contained in:
ShadowNinja 2015-10-15 01:08:18 -04:00 committed by kwolekr
parent 659922fd30
commit 7b8d372947
2 changed files with 30 additions and 40 deletions

View File

@ -76,7 +76,7 @@ ItemDefinition read_item_definition(lua_State* L,int index,
getboolfield(L, index, "liquids_pointable", def.liquids_pointable); getboolfield(L, index, "liquids_pointable", def.liquids_pointable);
warn_if_field_exists(L, index, "tool_digging_properties", warn_if_field_exists(L, index, "tool_digging_properties",
"deprecated: use tool_capabilities"); "Deprecated; use tool_capabilities");
lua_getfield(L, index, "tool_capabilities"); lua_getfield(L, index, "tool_capabilities");
if(lua_istable(L, -1)){ if(lua_istable(L, -1)){
@ -427,17 +427,17 @@ ContentFeatures read_content_features(lua_State *L, int index)
// Warn about some deprecated fields // Warn about some deprecated fields
warn_if_field_exists(L, index, "wall_mounted", warn_if_field_exists(L, index, "wall_mounted",
"deprecated: use paramtype2 = 'wallmounted'"); "Deprecated; use paramtype2 = 'wallmounted'");
warn_if_field_exists(L, index, "light_propagates", warn_if_field_exists(L, index, "light_propagates",
"deprecated: determined from paramtype"); "Deprecated; determined from paramtype");
warn_if_field_exists(L, index, "dug_item", warn_if_field_exists(L, index, "dug_item",
"deprecated: use 'drop' field"); "Deprecated; use 'drop' field");
warn_if_field_exists(L, index, "extra_dug_item", warn_if_field_exists(L, index, "extra_dug_item",
"deprecated: use 'drop' field"); "Deprecated; use 'drop' field");
warn_if_field_exists(L, index, "extra_dug_item_rarity", warn_if_field_exists(L, index, "extra_dug_item_rarity",
"deprecated: use 'drop' field"); "Deprecated; use 'drop' field");
warn_if_field_exists(L, index, "metadata_name", warn_if_field_exists(L, index, "metadata_name",
"deprecated: use on_add and metadata callbacks"); "Deprecated; use on_add and metadata callbacks");
// True for all ground-like things like stone and mud, false for eg. trees // True for all ground-like things like stone and mud, false for eg. trees
getboolfield(L, index, "is_ground_content", f.is_ground_content); getboolfield(L, index, "is_ground_content", f.is_ground_content);
@ -639,14 +639,13 @@ void pushnode(lua_State *L, const MapNode &n, INodeDefManager *ndef)
/******************************************************************************/ /******************************************************************************/
void warn_if_field_exists(lua_State *L, int table, void warn_if_field_exists(lua_State *L, int table,
const char *fieldname, const std::string &message) const char *name, const std::string &message)
{ {
lua_getfield(L, table, fieldname); lua_getfield(L, table, name);
if(!lua_isnil(L, -1)){ if (!lua_isnil(L, -1)) {
//TODO find way to access backtrace fct from here warningstream << "Field \"" << name << "\": "
// infostream<<script_get_backtrace(L)<<std::endl; << message << std::endl;
infostream<<"WARNING: field \""<<fieldname<<"\": " infostream << script_get_backtrace(L) << std::endl;
<<message<<std::endl;
} }
lua_pop(L, 1); lua_pop(L, 1);
} }
@ -705,7 +704,7 @@ ItemStack read_item(lua_State* L, int index,Server* srv)
} }
catch(SerializationError &e) catch(SerializationError &e)
{ {
infostream<<"WARNING: unable to create item from itemstring" warningstream<<"unable to create item from itemstring"
<<": "<<itemstring<<std::endl; <<": "<<itemstring<<std::endl;
return ItemStack(); return ItemStack();
} }
@ -840,14 +839,14 @@ ToolCapabilities read_tool_capabilities(
getintfield(L, table_groupcap, "uses", groupcap.uses); getintfield(L, table_groupcap, "uses", groupcap.uses);
// DEPRECATED: maxwear // DEPRECATED: maxwear
float maxwear = 0; float maxwear = 0;
if(getfloatfield(L, table_groupcap, "maxwear", maxwear)){ if (getfloatfield(L, table_groupcap, "maxwear", maxwear)){
if(maxwear != 0) if (maxwear != 0)
groupcap.uses = 1.0/maxwear; groupcap.uses = 1.0/maxwear;
else else
groupcap.uses = 0; groupcap.uses = 0;
infostream<<script_get_backtrace(L)<<std::endl; warningstream << "Field \"maxwear\" is deprecated; "
infostream<<"WARNING: field \"maxwear\" is deprecated; " << "replace with uses=1/maxwear" << std::endl;
<<"should replace with uses=1/maxwear"<<std::endl; infostream << script_get_backtrace(L) << std::endl;
} }
// Read "times" table // Read "times" table
lua_getfield(L, table_groupcap, "times"); lua_getfield(L, table_groupcap, "times");

View File

@ -163,35 +163,26 @@ void script_run_callbacks_f(lua_State *L, int nargs,
void log_deprecated(lua_State *L, const std::string &message) void log_deprecated(lua_State *L, const std::string &message)
{ {
static bool configured = false; static bool configured = false;
static bool dolog = false; static bool do_log = false;
static bool doerror = false; static bool do_error = false;
// performance optimization to not have to read and compare setting for every logline // Only read settings on first call
if (!configured) { if (!configured) {
std::string value = g_settings->get("deprecated_lua_api_handling"); std::string value = g_settings->get("deprecated_lua_api_handling");
if (value == "log") { if (value == "log") {
dolog = true; do_log = true;
} else if (value == "error") { } else if (value == "error") {
dolog = true; do_log = true;
doerror = true; do_error = true;
} }
} }
if (doerror) { if (do_log) {
if (L != NULL) { warningstream << message << std::endl;
if (do_error)
script_error(L, LUA_ERRRUN, NULL, NULL); script_error(L, LUA_ERRRUN, NULL, NULL);
} else { else
FATAL_ERROR("Can't do a scripterror for this deprecated message, " infostream << script_get_backtrace(L) << std::endl;
"so exit completely!");
}
}
if (dolog) {
/* abusing actionstream because of lack of file-only-logged loglevel */
actionstream << message << std::endl;
if (L != NULL) {
actionstream << script_get_backtrace(L) << std::endl;
}
} }
} }