Fix deprecation spam
This removes the Lua deprecation_message function in favour of exposing the C++ variant to Lua instead. It also moves all deprecation messages to a separate logdomain, making them easily enabled en masse.
This commit is contained in:
parent
5517063fb9
commit
dabf09fce1
10 changed files with 58 additions and 59 deletions
|
@ -1,7 +1,7 @@
|
|||
|
||||
local _ = wesnoth.textdomain "wesnoth-ai"
|
||||
|
||||
wesnoth.deprecation_message('data/ai/lua/patrol.lua', 1, nil, _"Use the Patrols Micro AI instead of patrol.lua.")
|
||||
wesnoth.deprecated_message('data/ai/lua/patrol.lua', 1, nil, _"Use the Patrols Micro AI instead of patrol.lua.")
|
||||
|
||||
function patrol_gen(n, wp)
|
||||
-- n is the name of the unit, like Kiressh
|
||||
|
|
|
@ -147,45 +147,8 @@ end
|
|||
--[========[Deprecation Helpers]========]
|
||||
|
||||
-- Need a textdomain for translatable deprecation strings.
|
||||
-- Note that these strings are all duplicated in src/deprecation.cpp
|
||||
-- Any changes here should also be reflected there.
|
||||
local _ = wesnoth.textdomain "wesnoth"
|
||||
|
||||
-- Note: When using version (for level 2 or 3 deprecation), specify the first version
|
||||
-- in which the feature could be removed... NOT the version at which it was deprecated.
|
||||
function wesnoth.deprecation_message(elem_name, level, version, detail)
|
||||
local message_params = {elem = elem_name}
|
||||
local logger
|
||||
local message
|
||||
if level == 1 then
|
||||
logger = function(msg) wesnoth.log("info", msg) end
|
||||
message = wesnoth.format(_"$elem has been deprecated indefinitely.", message_params)
|
||||
elseif level == 2 then
|
||||
logger = function(msg) wesnoth.log("warn", msg) end
|
||||
if wesnoth.compare_versions(wesnoth.game_config.version, "<", version) then
|
||||
message_params.version = version
|
||||
message = wesnoth.format(_"$elem has been deprecated and may be removed in version $version.", message_params)
|
||||
else
|
||||
message = wesnoth.format(_"$elem has been deprecated and may be removed at any time.", message_params)
|
||||
end
|
||||
elseif level == 3 then
|
||||
logger = function(msg) wesnoth.log("err", msg) end
|
||||
message_params.version = version
|
||||
message = wesnoth.format(_"$elem has been deprecated and will be removed in the next version ($version).", message_params)
|
||||
elseif level == 4 then
|
||||
logger = error
|
||||
message = wesnoth.format(_"$elem has been deprecated and removed.", message_params)
|
||||
else
|
||||
local err_params = {level = level}
|
||||
error(wesnoth.format(_"Invalid deprecation level $level (should be 1-4)", err_params))
|
||||
end
|
||||
if #detail > 0 then
|
||||
logger(tostring(message .. "\n " .. detail))
|
||||
else
|
||||
logger(tostring(message))
|
||||
end
|
||||
end
|
||||
|
||||
-- Marks a function or subtable as deprecated.
|
||||
-- Parameters:
|
||||
---- elem_name: the full name of the element being deprecated (including the module)
|
||||
|
@ -204,6 +167,8 @@ function wesnoth.deprecate_api(elem_name, replacement, level, version, elem, det
|
|||
end
|
||||
if type(level) ~= "number" or level < 1 or level > 4 then
|
||||
local err_params = {level = level}
|
||||
-- Note: This message is duplicated in src/deprecation.cpp
|
||||
-- Any changes should be mirrorred there.
|
||||
error(wesnoth.format(_"Invalid deprecation level $level (should be 1-4)", err_params))
|
||||
end
|
||||
local msg_shown = false
|
||||
|
@ -211,7 +176,7 @@ function wesnoth.deprecate_api(elem_name, replacement, level, version, elem, det
|
|||
return function(...)
|
||||
if not msg_shown then
|
||||
msg_shown = true
|
||||
wesnoth.deprecation_message(elem_name, level, version, message)
|
||||
wesnoth.deprecated_message(elem_name, level, version, message)
|
||||
end
|
||||
return elem(...)
|
||||
end
|
||||
|
@ -220,14 +185,14 @@ function wesnoth.deprecate_api(elem_name, replacement, level, version, elem, det
|
|||
__index = function(self, key)
|
||||
if not msg_shown then
|
||||
msg_shown = true
|
||||
wesnoth.deprecation_message(elem_name, level, version, message)
|
||||
wesnoth.deprecated_message(elem_name, level, version, message)
|
||||
end
|
||||
return elem[key]
|
||||
end,
|
||||
__newindex = function(self, key, val)
|
||||
if not msg_shown then
|
||||
msg_shown = true
|
||||
wesnoth.deprecation_message(elem_name, level, version, message)
|
||||
wesnoth.deprecated_message(elem_name, level, version, message)
|
||||
end
|
||||
elem[key] = val
|
||||
end,
|
||||
|
|
|
@ -893,7 +893,7 @@ function wml_actions.deprecated_message(cfg)
|
|||
local _ = wesnoth.textdomain "wesnoth"
|
||||
helper.wml_error(_"Invalid deprecation level (should be 1-4)")
|
||||
end
|
||||
wesnoth.deprecation_message(cfg.what, cfg.level, cfg.version, cfg.message or '')
|
||||
wesnoth.deprecated_message(cfg.what, cfg.level, cfg.version, cfg.message or '')
|
||||
if not wesnoth.game_config.debug then return end
|
||||
if cfg.level > 1 then
|
||||
wesnoth.log('wml', cfg.message)
|
||||
|
|
|
@ -20,11 +20,14 @@
|
|||
#include "game_config.hpp"
|
||||
#include "version.hpp"
|
||||
|
||||
static lg::log_domain log_wml("wml");
|
||||
// Set the default severity with the second parameter.
|
||||
// -1 means the default is to never log on this domain.
|
||||
// 0 would mean log errors only.
|
||||
// 1 would mean log errors and warnings.
|
||||
// and so on and so on.
|
||||
static lg::log_domain log_deprecate("deprecation", -1);
|
||||
|
||||
// Note that these strings are all duplicated in data/lua/core.lua
|
||||
// Any changes here should also be reflected there.
|
||||
void deprecated_message(const std::string& elem_name, int level, const version_info& version, const std::string& detail) {
|
||||
std::string deprecated_message(const std::string& elem_name, int level, const version_info& version, const std::string& detail) {
|
||||
utils::string_map msg_params = {{"elem", elem_name}};
|
||||
lg::logger* log_ptr = nullptr;
|
||||
std::string message;
|
||||
|
@ -48,16 +51,21 @@ void deprecated_message(const std::string& elem_name, int level, const version_i
|
|||
message = VGETTEXT("$elem has been deprecated and removed.", msg_params);
|
||||
} else {
|
||||
utils::string_map err_params = {{"level", std::to_string(level)}};
|
||||
LOG_STREAM(err, "general") << VGETTEXT("Invalid deprecation level $level (should be 1-4)", err_params);
|
||||
// Note: This message is duplicated in data/lua/core.lua
|
||||
// Any changes should be mirrorred there.
|
||||
std::string msg = VGETTEXT("Invalid deprecation level $level (should be 1-4)", err_params);
|
||||
LOG_STREAM(err, "general") << msg;
|
||||
return msg;
|
||||
}
|
||||
if(!detail.empty()) {
|
||||
message += "\n ";
|
||||
message += detail;
|
||||
}
|
||||
// TODO: Decide when to dump to wml_error()
|
||||
if(log_ptr && !log_ptr->dont_log(log_wml)) {
|
||||
if(log_ptr && !log_ptr->dont_log(log_deprecate)) {
|
||||
const lg::logger& out_log = *log_ptr;
|
||||
out_log(log_wml) << message << '\n';
|
||||
out_log(log_deprecate) << message << '\n';
|
||||
//lg::wml_error() << message << '\n';
|
||||
if(!detail.empty()) {
|
||||
out_log(log_wml) << detail << '\n';
|
||||
//lg::wml_error() << detail << '\n';
|
||||
}
|
||||
}
|
||||
return message;
|
||||
}
|
|
@ -16,4 +16,5 @@
|
|||
// Note: When using version (for level 2 or 3 deprecation), specify the first version
|
||||
// in which the feature could be removed... NOT the version at which it was deprecated.
|
||||
// For level 1 or 4 deprecation, it's fine to just pass an empty string, as the parameter will not be used.
|
||||
void deprecated_message(const std::string& elem_name, int level, const class version_info& version, const std::string& detail = "");
|
||||
// It returns the final translated deprecation message, in case you want to output it elsewhere as well.
|
||||
std::string deprecated_message(const std::string& elem_name, int level, const class version_info& version, const std::string& detail = "");
|
||||
|
|
|
@ -107,12 +107,13 @@ log_domain& general()
|
|||
return dom;
|
||||
}
|
||||
|
||||
log_domain::log_domain(char const *name)
|
||||
log_domain::log_domain(char const *name, int severity)
|
||||
: domain_(nullptr)
|
||||
{
|
||||
// Indirection to prevent initialization depending on link order.
|
||||
if (!domains) domains = new domain_map;
|
||||
domain_ = &*domains->insert(logd(name, 1)).first;
|
||||
domain_ = &*domains->insert(logd(name, severity)).first;
|
||||
domain_->second = severity;
|
||||
}
|
||||
|
||||
bool set_log_domain_severity(const std::string& name, int severity)
|
||||
|
|
|
@ -99,7 +99,7 @@ typedef std::pair<const std::string, int> logd;
|
|||
class log_domain {
|
||||
logd *domain_;
|
||||
public:
|
||||
log_domain(char const *name);
|
||||
log_domain(char const *name, int severity = 1);
|
||||
friend class logger;
|
||||
};
|
||||
|
||||
|
|
|
@ -595,7 +595,7 @@ t_string luaW_checktstring(lua_State *L, int index)
|
|||
return result;
|
||||
}
|
||||
|
||||
bool luaW_isstring(lua_State* L, int index)
|
||||
bool luaW_iststring(lua_State* L, int index)
|
||||
{
|
||||
if(lua_isstring(L, index)) {
|
||||
return true;
|
||||
|
|
|
@ -82,7 +82,7 @@ t_string luaW_checktstring(lua_State *L, int index);
|
|||
* Test if a scalar is either a plain or translatable string.
|
||||
* Also returns true if it's a number since that's convertible to string.
|
||||
*/
|
||||
bool luaW_isstring(lua_State* L, int index);
|
||||
bool luaW_iststring(lua_State* L, int index);
|
||||
|
||||
/**
|
||||
* Converts a config object to a Lua table.
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include "lua_jailbreak_exception.hpp" // for lua_jailbreak_exception
|
||||
#include "random.hpp"
|
||||
#include "seed_rng.hpp"
|
||||
#include "deprecation.hpp"
|
||||
|
||||
#ifdef DEBUG_LUA
|
||||
#include "scripting/debug_lua.hpp"
|
||||
|
@ -274,6 +275,28 @@ static int intf_log(lua_State *L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a deprecation message. See deprecation.cpp for details
|
||||
* Arg 1: Element to be deprecated.
|
||||
* Arg 2: Deprecation level.
|
||||
* Arg 3: Version when element may be removed.
|
||||
* Arg 4: Additional detail message.
|
||||
*/
|
||||
static int intf_deprecated_message(lua_State* L) {
|
||||
const std::string elem = luaL_checkstring(L, 1);
|
||||
const int level = luaL_checkinteger(L, 2);
|
||||
const std::string ver_str = lua_isnoneornil(L, 3) ? "" : luaL_checkstring(L, 3);
|
||||
const std::string detail = luaW_checktstring(L, 4);
|
||||
const version_info ver = ver_str.empty() ? game_config::version : ver_str;
|
||||
const std::string msg = deprecated_message(elem, level, ver, detail);
|
||||
if(level < 1 || level >= 4) {
|
||||
// Invalid deprecation level or level 4 deprecation should raise an interpreter error
|
||||
lua_push(L, msg);
|
||||
return lua_error(L);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the dimension of an image.
|
||||
* - Arg 1: string.
|
||||
|
@ -430,6 +453,7 @@ lua_kernel_base::lua_kernel_base()
|
|||
static luaL_Reg const callbacks[] {
|
||||
{ "compare_versions", &intf_compare_versions },
|
||||
{ "debug", &intf_debug },
|
||||
{ "deprecated_message", &intf_deprecated_message },
|
||||
{ "have_file", &lua_fileops::intf_have_file },
|
||||
{ "read_file", &lua_fileops::intf_read_file },
|
||||
{ "textdomain", &lua_common::intf_textdomain },
|
||||
|
|
Loading…
Add table
Reference in a new issue