-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
deprecation.cpp
88 lines (72 loc) · 2.73 KB
/
deprecation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Copyright (C) 2017-2018 by the Battle for Wesnoth Project https://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 "formula/string_utils.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "preferences/general.hpp"
#include "game_version.hpp"
// 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", 0);
std::string deprecated_message(
const std::string& elem_name, DEP_LEVEL 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;
switch(level) {
case DEP_LEVEL::INDEFINITE:
log_ptr = &lg::info();
message = VGETTEXT("$elem has been deprecated indefinitely.", msg_params);
break;
case DEP_LEVEL::PREEMPTIVE:
log_ptr = &lg::warn();
if(game_config::wesnoth_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);
}
break;
case DEP_LEVEL::FOR_REMOVAL:
log_ptr = &lg::err();
msg_params["version"] = version.str();
message = VGETTEXT("$elem has been deprecated and will be removed in version $version.", msg_params);
break;
case DEP_LEVEL::REMOVED:
log_ptr = &lg::err();
message = VGETTEXT("$elem has been deprecated and removed.", msg_params);
break;
default: // Not impossible, in case level was given an invalid value from a cast.
utils::string_map err_params {{"level", std::to_string(static_cast<int>(level))}};
// 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;
}
if(log_ptr && !log_ptr->dont_log(log_deprecate)) {
const lg::logger& out_log = *log_ptr;
FORCE_LOG_TO(out_log, log_deprecate) << message << '\n';
if(preferences::get("show_deprecation", false)) {
lg::wml_error() << message << '\n';
}
}
return message;
}