The Battle for Wesnoth  1.19.0-dev
deprecation.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
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 "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.
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);
70  return msg;
71  }
72 
73  if(!detail.empty()) {
74  message += "; ";
75  message += detail;
76  }
77 
78  if(log_ptr && !log_ptr->dont_log(log_deprecate)) {
79  const lg::logger& out_log = *log_ptr;
80  FORCE_LOG_TO(out_log, log_deprecate) << message;
81  // whether to show the error in the ingame chat area
82  if(preferences::get("show_deprecation", game_config::wesnoth_version.is_dev_version())) {
83  lg::log_to_chat() << message << '\n';
84  }
85  }
86 
87  return message;
88 }
bool dont_log(const log_domain &domain) const
Definition: log.hpp:211
Represents version numbers.
std::string str() const
Serializes the version number into string form.
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Definition: deprecation.cpp:29
static lg::log_domain log_deprecate("deprecation", lg::severity::LG_ERROR)
DEP_LEVEL
See https://wiki.wesnoth.org/CompatibilityStandards for more info.
Definition: deprecation.hpp:21
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
Interfaces for manipulating version numbers of engine, add-ons, etc.
Standard logging facilities (interface).
#define FORCE_LOG_TO(logger, domain)
Definition: log.hpp:290
#define LOG_STREAM(level, domain)
Definition: log.hpp:277
const version_info wesnoth_version(VERSION)
logger & err()
Definition: log.cpp:302
log_domain & general()
Definition: log.cpp:328
logger & warn()
Definition: log.cpp:308
std::stringstream & log_to_chat()
Use this to show WML errors in the ingame chat.
Definition: log.cpp:544
logger & info()
Definition: log.cpp:314
std::string get(const std::string &key)
Definition: general.cpp:213
std::map< std::string, t_string > string_map
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109