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