The Battle for Wesnoth  1.15.2+dev
deprecation.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #include "deprecation.hpp"
15 
16 #include "formula/string_utils.hpp"
17 #include "gettext.hpp"
18 #include "log.hpp"
19 #include "preferences/general.hpp"
20 #include "game_version.hpp"
21 
22 // Set the default severity with the second parameter.
23 // -1 means the default is to never log on this domain.
24 // 0 would mean log errors only.
25 // 1 would mean log errors and warnings.
26 // and so on and so on.
27 static lg::log_domain log_deprecate("deprecation", 0);
28 
29 std::string deprecated_message(
30  const std::string& elem_name, DEP_LEVEL level, const version_info& version, const std::string& detail)
31 {
32  utils::string_map msg_params {{"elem", elem_name}};
33  lg::logger* log_ptr = nullptr;
34  std::string message;
35 
36  switch(level) {
38  log_ptr = &lg::info();
39  message = VGETTEXT("$elem has been deprecated indefinitely.", msg_params);
40  break;
41 
43  log_ptr = &lg::warn();
44  if(game_config::wesnoth_version < version) {
45  msg_params["version"] = version.str();
46  message = VGETTEXT("$elem has been deprecated and may be removed in version $version.", msg_params);
47  } else {
48  message = VGETTEXT("$elem has been deprecated and may be removed at any time.", msg_params);
49  }
50  break;
51 
53  log_ptr = &lg::err();
54  msg_params["version"] = version.str();
55  message = VGETTEXT("$elem has been deprecated and will be removed in version $version.", msg_params);
56  break;
57 
58  case DEP_LEVEL::REMOVED:
59  log_ptr = &lg::err();
60  message = VGETTEXT("$elem has been deprecated and removed.", msg_params);
61  break;
62 
63  default: // Not impossible, in case level was given an invalid value from a cast.
64  utils::string_map err_params {{"level", std::to_string(static_cast<int>(level))}};
65 
66  // Note: This message is duplicated in data/lua/core.lua
67  // Any changes should be mirrorred there.
68  std::string msg = VGETTEXT("Invalid deprecation level $level (should be 1-4)", err_params);
69  LOG_STREAM(err, "general") << msg;
70  return msg;
71  }
72 
73  if(!detail.empty()) {
74  message += "\n ";
75  message += detail;
76  }
77 
78  if(log_ptr && !log_ptr->dont_log(log_deprecate)) {
79  const lg::logger& out_log = *log_ptr;
80  out_log(log_deprecate) << message << '\n';
81 
82  if(preferences::get("show_deprecation", false)) {
83  lg::wml_error() << message << '\n';
84  }
85  }
86 
87  return message;
88 }
std::map< std::string, t_string > string_map
Interfaces for manipulating version numbers of engine, add-ons, etc.
static lg::log_domain log_deprecate("deprecation", 0)
logger & info()
Definition: log.cpp:90
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
#define LOG_STREAM(level, domain)
Definition: log.hpp:189
std::string get(const std::string &key)
Definition: general.cpp:228
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Definition: deprecation.cpp:29
bool dont_log(const log_domain &domain) const
Definition: log.hpp:123
std::stringstream & wml_error()
Use this logger to send errors due to deprecated WML.
Definition: log.cpp:269
logger & err()
Definition: log.cpp:78
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
const version_info wesnoth_version(VERSION)
Represents version numbers.
logger & warn()
Definition: log.cpp:84
DEP_LEVEL
See https://wiki.wesnoth.org/CompatibilityStandards for more info.
Definition: deprecation.hpp:19
Standard logging facilities (interface).
std::string str() const
Serializes the version number into string form.