wml_exception.cpp

Go to the documentation of this file.
00001 /* $Id: wml_exception.cpp 54233 2012-05-19 19:35:44Z mordante $ */
00002 /*
00003    Copyright (C) 2007 - 2012 by Mark de Wever <koraq@xs4all.nl>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /**
00017  *  @file
00018  *  Implementation for wml_exception.hpp.
00019  */
00020 
00021 #define GETTEXT_DOMAIN "wesnoth-lib"
00022 
00023 #include "global.hpp"
00024 #include "wml_exception.hpp"
00025 
00026 #include "display.hpp"
00027 #include "gettext.hpp"
00028 #include "gui/dialogs/message.hpp"
00029 #include "formula_string_utils.hpp"
00030 #include "log.hpp"
00031 
00032 static lg::log_domain log_engine("engine");
00033 #define WRN_NG LOG_STREAM(warn, log_engine)
00034 
00035 void wml_exception(
00036           const char* cond
00037         , const char* file
00038         , const int line
00039         , const char *function
00040         , const std::string& message
00041         , const std::string& dev_message)
00042 {
00043     std::ostringstream sstr;
00044     if(cond) {
00045         sstr << "Condition '" << cond << "' failed at ";
00046     } else {
00047         sstr << "Unconditional failure at ";
00048     }
00049 
00050     sstr << file << ":" << line << " in function '" << function << "'.";
00051 
00052     if(!dev_message.empty()) {
00053         sstr << " Extra development information: " << dev_message;
00054     }
00055 
00056     throw twml_exception(message, sstr.str());
00057 }
00058 
00059 void twml_exception::show(display &disp)
00060 {
00061     std::ostringstream sstr;
00062 
00063     // The extra spaces between the \n are needed, otherwise the dialog doesn't show
00064     // an empty line.
00065     sstr << _("An error due to possibly invalid WML occurred\nThe error message is :")
00066         << "\n" << user_message << "\n \n"
00067         << _("When reporting the bug please include the following error message :")
00068         << "\n" << dev_message;
00069 
00070     gui2::show_error_message(disp.video(), sstr.str());
00071 }
00072 
00073 std::string missing_mandatory_wml_key(
00074           const std::string &section
00075         , const std::string &key
00076         , const std::string& primary_key
00077         , const std::string& primary_value)
00078 {
00079     utils::string_map symbols;
00080     if(!section.empty()) {
00081         if(section[0] == '[') {
00082             symbols["section"] = section;
00083         } else {
00084             WRN_NG << __func__
00085                     << " parameter 'section' should contain brackets."
00086                     << " Added them.\n";
00087             symbols["section"] = "[" + section + "]";
00088         }
00089     }
00090     symbols["key"] = key;
00091     if(!primary_key.empty()) {
00092         assert(!primary_value.empty());
00093 
00094         symbols["primary_key"] = primary_key;
00095         symbols["primary_value"] = primary_value;
00096 
00097         return vgettext("In section '[$section|]' where '$primary_key| = "
00098             "$primary_value' the mandatory key '$key|' isn't set.", symbols);
00099     } else {
00100         return vgettext("In section '[$section|]' the "
00101             "mandatory key '$key|' isn't set.", symbols);
00102     }
00103 }
00104 
00105 std::string deprecate_wml_key_warning(
00106           const std::string& key
00107         , const std::string& removal_version)
00108 {
00109     assert(!key.empty());
00110     assert(!removal_version.empty());
00111 
00112     utils::string_map symbols;
00113     symbols["key"] = key;
00114     symbols["removal_version"] = removal_version;
00115 
00116     return vgettext("The key '$key' is deprecated and support "
00117             "will be removed in version $removal_version.", symbols);
00118 }
00119 
00120 std::string deprecated_renamed_wml_key_warning(
00121           const std::string& deprecated_key
00122         , const std::string& key
00123         , const std::string& removal_version)
00124 {
00125     assert(!deprecated_key.empty());
00126     assert(!key.empty());
00127     assert(!removal_version.empty());
00128 
00129     utils::string_map symbols;
00130     symbols["deprecated_key"] = deprecated_key;
00131     symbols["key"] = key;
00132     symbols["removal_version"] = removal_version;
00133 
00134     return vgettext(
00135               "The key '$deprecated_key' has been renamed to '$key'. "
00136                 "Support for '$deprecated_key' will be removed in version "
00137                 "$removal_version."
00138             , symbols);
00139 }
00140 
00141 const config::attribute_value& get_renamed_config_attribute(
00142           const config& cfg
00143         , const std::string& deprecated_key
00144         , const std::string& key
00145         , const std::string& removal_version)
00146 {
00147 
00148     const config::attribute_value* result = cfg.get(key);
00149     if(result) {
00150         return *result;
00151     }
00152 
00153     result = cfg.get(deprecated_key);
00154     if(result) {
00155         lg::wml_error
00156             << deprecated_renamed_wml_key_warning(
00157                   deprecated_key
00158                 , key
00159                 , removal_version)
00160             << '\n';
00161 
00162         return *result;
00163     }
00164 
00165     static const config::attribute_value empty_attribute;
00166     return empty_attribute;
00167 }
00168 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:15 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs