The Battle for Wesnoth  1.17.0-dev
wml_exception.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  * Implementation for wml_exception.hpp.
19  */
20 
21 #define GETTEXT_DOMAIN "wesnoth-lib"
22 
23 #include "wml_exception.hpp"
24 
25 #include "gettext.hpp"
26 #include "gui/dialogs/message.hpp"
27 #include "formula/string_utils.hpp"
28 #include "log.hpp"
29 
30 static lg::log_domain log_engine("engine");
31 #define WRN_NG LOG_STREAM(warn, log_engine)
32 
33 static lg::log_domain log_wml("wml");
34 #define ERR_WML LOG_STREAM(err, log_wml)
35 
37  const char* cond
38  , const char* file
39  , const int line
40  , const char *function
41  , const std::string& message
42  , const std::string& dev_message)
43 {
44  std::ostringstream sstr;
45  if(cond) {
46  sstr << "Condition '" << cond << "' failed at ";
47  } else {
48  sstr << "Unconditional failure at ";
49  }
50 
51  sstr << file << ":" << line << " in function '" << function << "'.";
52 
53  if(!dev_message.empty()) {
54  sstr << " Extra development information: " << dev_message;
55  }
56 
57  throw wml_exception(message, sstr.str());
58 }
59 
60 void wml_exception::show() const
61 {
62  std::ostringstream sstr;
63 
64  // The extra spaces between the \n are needed, otherwise the dialog doesn't show
65  // an empty line.
66  sstr << _("An error due to possibly invalid WML occurred\nThe error message is :")
67  << "\n" << user_message << "\n \n"
68  << _("When reporting the bug please include the following error message :")
69  << "\n" << dev_message;
70 
71  gui2::show_error_message(sstr.str());
72 }
73 
75  const std::string &section
76  , const std::string &key
77  , const std::string& primary_key
78  , const std::string& primary_value)
79 {
80  utils::string_map symbols;
81  if(!section.empty()) {
82  if(section[0] == '[') {
83  symbols["section"] = section;
84  } else {
85  WRN_NG << __func__
86  << " parameter 'section' should contain brackets."
87  << " Added them.\n";
88  symbols["section"] = "[" + section + "]";
89  }
90  }
91  symbols["key"] = key;
92  if(!primary_key.empty()) {
93  assert(!primary_value.empty());
94 
95  symbols["primary_key"] = primary_key;
96  symbols["primary_value"] = primary_value;
97 
98  return VGETTEXT("In section '[$section|]' where '$primary_key| = "
99  "$primary_value' the mandatory key '$key|' isn't set.", symbols);
100  } else {
101  return VGETTEXT("In section '[$section|]' the "
102  "mandatory key '$key|' isn't set.", symbols);
103  }
104 }
105 
107  const std::string& key
108  , const std::string& removal_version)
109 {
110  assert(!key.empty());
111  assert(!removal_version.empty());
112 
113  utils::string_map symbols;
114  symbols["key"] = key;
115  symbols["removal_version"] = removal_version;
116 
117  return VGETTEXT("The key '$key' is deprecated and support "
118  "will be removed in version $removal_version.", symbols);
119 }
120 
122  const std::string& deprecated_key
123  , const std::string& key
124  , const std::string& removal_version)
125 {
126  assert(!deprecated_key.empty());
127  assert(!key.empty());
128  assert(!removal_version.empty());
129 
130  utils::string_map symbols;
131  symbols["deprecated_key"] = deprecated_key;
132  symbols["key"] = key;
133  symbols["removal_version"] = removal_version;
134 
135  return VGETTEXT(
136  "The key '$deprecated_key' has been renamed to '$key'. "
137  "Support for '$deprecated_key' will be removed in version "
138  "$removal_version."
139  , symbols);
140 }
141 
143  const config& cfg
144  , const std::string& deprecated_key
145  , const std::string& key
146  , const std::string& removal_version)
147 {
148 
149  const config::attribute_value* result = cfg.get(key);
150  if(result) {
151  return *result;
152  }
153 
154  result = cfg.get(deprecated_key);
155  if(result) {
156  std::string msg = deprecated_renamed_wml_key_warning(deprecated_key, key, removal_version);
157  lg::log_to_chat() << msg << '\n';
158  ERR_WML << msg;
159 
160  return *result;
161  }
162 
163  static const config::attribute_value empty_attribute;
164  return empty_attribute;
165 }
std::map< std::string, t_string > string_map
void throw_wml_exception(const char *cond, const char *file, const int line, const char *function, const std::string &message, const std::string &dev_message)
Helper function, don&#39;t call this directly.
Variant for storing WML attributes.
Add a special kind of assert to validate whether the input from WML doesn&#39;t contain any problems that...
#define ERR_WML
std::string user_message
The message for the user explaining what went wrong.
const attribute_value * get(config_key_type key) const
Returns a pointer to the attribute with the given key or nullptr if it does not exist.
Definition: config.cpp:780
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
const config::attribute_value & get_renamed_config_attribute(const config &cfg, const std::string &deprecated_key, const std::string &key, const std::string &removal_version)
Returns a config attribute, using either the old name or the new one.
static std::string _(const char *str)
Definition: gettext.hpp:93
std::string missing_mandatory_wml_key(const std::string &section, const std::string &key, const std::string &primary_key, const std::string &primary_value)
Returns a standard message for a missing wml key.
void show() const
Shows the error in a dialog.
#define WRN_NG
Helper class, don&#39;t construct this directly.
std::string dev_message
The message for developers telling which problem was triggered, this shouldn&#39;t be translated...
static lg::log_domain log_engine("engine")
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
std::string deprecated_renamed_wml_key_warning(const std::string &deprecated_key, const std::string &key, const std::string &removal_version)
Returns a standard warning message for using a deprecated renamed wml key.
static int cond(LexState *ls)
Definition: lparser.cpp:1394
Standard logging facilities (interface).
std::string deprecate_wml_key_warning(const std::string &key, const std::string &removal_version)
Returns a standard warning message for using a deprecated wml key.
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:206
static lg::log_domain log_wml("wml")
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
std::stringstream & log_to_chat()
Use this to show WML errors in the ingame chat.
Definition: log.cpp:289