Changeset 786 for cpp


Ignore:
Timestamp:
05/21/18 14:01:14 (7 years ago)
Author:
Maciej Komosinski
Message:

Improved shortening of long stringified values in error messages

Location:
cpp/frams
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/param/param.h

    r784 r786  
    252252};
    253253
     254template<typename T> struct quote_in_messages { constexpr static const char* value = "'"; };
     255template<> struct quote_in_messages < SString > { constexpr static const char* value = "\""; };
     256template<typename T> struct length_in_messages { static const bool value = false; };
     257template<> struct length_in_messages < SString > { static const bool value = true; };
     258
    254259class SimpleAbstractParam : public virtual ParamInterface
    255260{
     
    298303                        SString actual = get(i);
    299304                        bool s_type = type(i)[0] == 's';
    300                         logPrintf("Param", "set", LOG_WARN, "Setting '%s.%s = %s' exceeded allowed range (too %s). %s to %s.",
    301                                 getName(), id(i), sstringShorten(svaluetoset, 30).c_str(), (setflags&PSET_HITMAX) ? (s_type ? "long" : "big") : "small", s_type ? "Truncated" : "Adjusted", sstringShorten(actual, 30).c_str());
     305                        logPrintf("Param", "set", LOG_WARN, "Setting %s.%s = %s exceeded allowed range (too %s). %s to %s.",
     306                                getName(), id(i),
     307                                ::sstringDelimitAndShorten(svaluetoset, 30, length_in_messages<T>::value, quote_in_messages<T>::value, quote_in_messages<T>::value).c_str(),
     308                                (setflags&PSET_HITMAX) ? (s_type ? "long" : "big") : "small", s_type ? "Truncated" : "Adjusted",
     309                                ::sstringDelimitAndShorten(actual, 30, length_in_messages<T>::value, quote_in_messages<T>::value, quote_in_messages<T>::value).c_str()
     310                                );
    302311                }
    303312        }
  • cpp/frams/util/extvalue.cpp

    r785 r786  
    305305{
    306306        SString msg = typeDescription();
     307        SString delimit("'");
    307308        switch (type)
    308309        {
    309         case TInt: case TDouble: case TString: case TObj:
    310                 msg += " '";
    311                 msg += sstringShorten(getString(), 30);
    312                 msg += "'";
     310        case TString:
     311                delimit = "\"";
     312        case TInt: case TDouble: case TObj:
     313                msg += " ";
     314                msg += sstringDelimitAndShorten(getString(), 50, (type == TString), delimit, delimit);
    313315        default:;
    314316        }
  • cpp/frams/util/sstringutils.cpp

    r785 r786  
    157157}
    158158
    159 SString sstringShorten(const SString &in, int maxlen)
     159SString sstringDelimitAndShorten(const SString &in, int maxlen, bool show_length, const SString& before, const SString& after)
    160160{
    161161        SString out;
    162162        if (in.len() > maxlen)
    163                 out = in.substr(0, maxlen / 2) + "..." + in.substr(in.len() - maxlen + maxlen / 2) + SString::sprintf(" (length %d)", in.len());
     163                out = in.substr(0, maxlen / 2) + "..." + in.substr(in.len() - maxlen + maxlen / 2);
    164164        else
    165                 out = in;
     165        {
     166                out = in; show_length = false;
     167        }
    166168        sstringQuote(out);
     169        out = before + out + after;
     170        if (show_length)
     171                out += SString::sprintf(" (length %d)", in.len());
    167172        return out;
    168173}
  • cpp/frams/util/sstringutils.h

    r785 r786  
    1919bool strContainsOneOf(const char* str, const char* chars);
    2020bool sstringQuote(SString& target);
    21 SString sstringShorten(const SString &in, int maxlen);
     21SString sstringDelimitAndShorten(const SString &in, int maxlen, bool show_length, const SString& before, const SString& after);
    2222const char* skipQuoteString(const char* txt, const char* limit);
    2323int sstringUnquote(SString &target);
Note: See TracChangeset for help on using the changeset viewer.