Skip to content

Commit

Permalink
WIP deprecation stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Feb 10, 2018
1 parent 3b88de6 commit 04684fe
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
11 changes: 9 additions & 2 deletions data/lua/core.lua
Expand Up @@ -167,12 +167,14 @@ function wesnoth.deprecation_message(elem_name, level, version, detail)
end
elseif level == 3 then
logger = function(msg) wesnoth.log("err", msg) end
message = wesnoth.format(_"$elem has been deprecated and will be removed in the next version.", message_params)
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
error(_"Invalid deprecation level (should be 1-4)")
local err_params = {level = level}
error(wesnoth.format(_"Invalid deprecation level $level (should be 1-4)", err_params))
end
if #detail > 0 then
logger(message .. "\n " .. detail)
Expand Down Expand Up @@ -218,6 +220,11 @@ function wesnoth.deprecate_api(elem_name, replacement, level, version, elem, det
elem[key] = val
end,
}
-- TODO: Test if this works for deprecating a table that already had metafunctions
local old_mt = getmetatable(elem)
if type(old_mt) == "table" then
setmetatable(mt, old_mt)
end
return setmetatable({}, mt)
else
wesnoth.log('warn', "Attempted to deprecate something that is not a table or function: " ..
Expand Down
2 changes: 2 additions & 0 deletions projectfiles/VC12/wesnoth.vcxproj
Expand Up @@ -744,6 +744,7 @@
<ClCompile Include="..\..\src\controller_base.cpp" />
<ClCompile Include="..\..\src\countdown_clock.cpp" />
<ClCompile Include="..\..\src\cursor.cpp" />
<ClCompile Include="..\..\src\deprecation.cpp" />
<ClCompile Include="..\..\src\desktop\clipboard.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)Desktop\</ObjectFileName>
Expand Down Expand Up @@ -3588,6 +3589,7 @@
<ClInclude Include="..\..\src\controller_base.hpp" />
<ClInclude Include="..\..\src\countdown_clock.hpp" />
<ClInclude Include="..\..\src\cursor.hpp" />
<ClInclude Include="..\..\src\deprecation.hpp" />
<ClInclude Include="..\..\src\desktop\clipboard.hpp" />
<ClInclude Include="..\..\src\desktop\notifications.hpp" />
<ClInclude Include="..\..\src\desktop\open.hpp" />
Expand Down
2 changes: 2 additions & 0 deletions projectfiles/VC12/wesnoth.vcxproj.filters
Expand Up @@ -1561,6 +1561,7 @@
<ClCompile Include="..\..\src\gui\dialogs\surrender_quit.cpp">
<Filter>Gui\Dialogs</Filter>
</ClCompile>
<ClCompile Include="..\..\src\deprecation.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\addon\client.hpp">
Expand Down Expand Up @@ -3031,6 +3032,7 @@
<ClInclude Include="..\..\src\gui\dialogs\surrender_quit.hpp">
<Filter>Gui\Dialogs</Filter>
</ClInclude>
<ClInclude Include="..\..\src\deprecation.hpp" />
</ItemGroup>
<ItemGroup>
<CustomBuild Include="..\..\src\tests\test_sdl_utils.hpp">
Expand Down
1 change: 1 addition & 0 deletions source_lists/libwesnoth
@@ -1,6 +1,7 @@
arrow.cpp
cursor.cpp
desktop/clipboard.cpp
deprecation.cpp
display.cpp
display_context.cpp
events.cpp
Expand Down
62 changes: 62 additions & 0 deletions src/deprecation.cpp
@@ -0,0 +1,62 @@
/*
Copyright (C) 2017 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#include "deprecation.hpp"

#include "log.hpp"
#include "gettext.hpp"
#include "formula/string_utils.hpp"
#include "tstring.hpp"
#include "game_config.hpp"
#include "version.hpp"

static lg::log_domain log_wml("wml");

void 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;
if(level == 1) {
log_ptr = &lg::info();
message = VGETTEXT("$elem has been deprecated indefinitely.", msg_params);
} else if(level == 2) {
log_ptr = &lg::warn();
version_info game_version(game_config::version);
if(game_version < version) {
msg_params["version"] = version.str();
message = VGETTEXT("$elem has been deprecated and may be removed in version $version.", msg_params);
} else {
message = VGETTEXT("$elem has been deprecated and may be removed at any time.", msg_params);
}
} else if(level == 3) {
log_ptr = &lg::err();
msg_params["version"] = version.str();
message = VGETTEXT("$elem has been deprecated and may be removed at any time.", msg_params);
} else if(level == 4) {
log_ptr = &lg::err();
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);
}
// TODO: Decide when to dump to wml_error()
if(log_ptr && !log_ptr->dont_log(log_wml)) {
const lg::logger& out_log = *log_ptr;
out_log(log_wml) << message << '\n';
lg::wml_error() << message << '\n';
if(!detail.empty()) {
out_log(log_wml) << detail << '\n';
lg::wml_error() << detail << '\n';
}
}
}
19 changes: 19 additions & 0 deletions src/deprecation.hpp
@@ -0,0 +1,19 @@
/*
Copyright (C) 2017 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#include <string>

// 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 = "");

0 comments on commit 04684fe

Please sign in to comment.