The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
wml_exception.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 /**
16  * @file
17  * Implementation for wml_exception.hpp.
18  */
19 
20 #define GETTEXT_DOMAIN "wesnoth-lib"
21 
22 #include "global.hpp"
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 
34  const char* cond
35  , const char* file
36  , const int line
37  , const char *function
38  , const std::string& message
39  , const std::string& dev_message)
40 {
41  std::ostringstream sstr;
42  if(cond) {
43  sstr << "Condition '" << cond << "' failed at ";
44  } else {
45  sstr << "Unconditional failure at ";
46  }
47 
48  sstr << file << ":" << line << " in function '" << function << "'.";
49 
50  if(!dev_message.empty()) {
51  sstr << " Extra development information: " << dev_message;
52  }
53 
54  throw twml_exception(message, sstr.str());
55 }
56 
58 {
59  std::ostringstream sstr;
60 
61  // The extra spaces between the \n are needed, otherwise the dialog doesn't show
62  // an empty line.
63  sstr << _("An error due to possibly invalid WML occurred\nThe error message is :")
64  << "\n" << user_message << "\n \n"
65  << _("When reporting the bug please include the following error message :")
66  << "\n" << dev_message;
67 
68  gui2::show_error_message(video, sstr.str());
69 }
70 
72  const std::string &section
73  , const std::string &key
74  , const std::string& primary_key
75  , const std::string& primary_value)
76 {
77  utils::string_map symbols;
78  if(!section.empty()) {
79  if(section[0] == '[') {
80  symbols["section"] = section;
81  } else {
82  WRN_NG << __func__
83  << " parameter 'section' should contain brackets."
84  << " Added them.\n";
85  symbols["section"] = "[" + section + "]";
86  }
87  }
88  symbols["key"] = key;
89  if(!primary_key.empty()) {
90  assert(!primary_value.empty());
91 
92  symbols["primary_key"] = primary_key;
93  symbols["primary_value"] = primary_value;
94 
95  return vgettext("In section '[$section|]' where '$primary_key| = "
96  "$primary_value' the mandatory key '$key|' isn't set.", symbols);
97  } else {
98  return vgettext("In section '[$section|]' the "
99  "mandatory key '$key|' isn't set.", symbols);
100  }
101 }
102 
104  const std::string& key
105  , const std::string& removal_version)
106 {
107  assert(!key.empty());
108  assert(!removal_version.empty());
109 
110  utils::string_map symbols;
111  symbols["key"] = key;
112  symbols["removal_version"] = removal_version;
113 
114  return vgettext("The key '$key' is deprecated and support "
115  "will be removed in version $removal_version.", symbols);
116 }
117 
119  const std::string& deprecated_key
120  , const std::string& key
121  , const std::string& removal_version)
122 {
123  assert(!deprecated_key.empty());
124  assert(!key.empty());
125  assert(!removal_version.empty());
126 
127  utils::string_map symbols;
128  symbols["deprecated_key"] = deprecated_key;
129  symbols["key"] = key;
130  symbols["removal_version"] = removal_version;
131 
132  return vgettext(
133  "The key '$deprecated_key' has been renamed to '$key'. "
134  "Support for '$deprecated_key' will be removed in version "
135  "$removal_version."
136  , symbols);
137 }
138 
140  const config& cfg
141  , const std::string& deprecated_key
142  , const std::string& key
143  , const std::string& removal_version)
144 {
145 
146  const config::attribute_value* result = cfg.get(key);
147  if(result) {
148  return *result;
149  }
150 
151  result = cfg.get(deprecated_key);
152  if(result) {
153  lg::wml_error()
155  deprecated_key
156  , key
157  , removal_version)
158  << '\n';
159 
160  return *result;
161  }
162 
163  static const config::attribute_value empty_attribute;
164  return empty_attribute;
165 }
166 
void show_error_message(CVideo &video, const std::string &message, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:198
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
Definition: video.hpp:58
void show(CVideo &video)
Shows the error in a dialog.
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.
Variant for storing WML attributes.
Definition: config.hpp:223
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.
std::string user_message
The message for the user explaining what went wrong.
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
GLuint64EXT * result
Definition: glew.h:10727
std::map< std::string, t_string > string_map
std::string dev_message
The message for developers telling which problem was triggered, this shouldn't be translated...
#define WRN_NG
std::stringstream & wml_error()
Use this logger to send errors due to deprecated WML.
Definition: log.cpp:262
static lg::log_domain log_engine("engine")
std::string vgettext(const char *msgid, const utils::string_map &symbols)
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.
const attribute_value * get(const std::string &key) const
Returns a pointer to the attribute with the given key or nullptr if it does not exist.
Definition: config.cpp:935
static int cond(LexState *ls)
Definition: lparser.cpp:1168
Standard logging facilities (interface).
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
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 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't call this directly.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
Helper class, don't construct this directly.
GLsizei const GLcharARB ** string
Definition: glew.h:4503