Skip to content

Commit

Permalink
Improved messages generated by asserts & dumps (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadult authored Oct 5, 2024
1 parent 3661aea commit 79b3202
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
5 changes: 4 additions & 1 deletion include/fwk/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,10 @@ template <c_formattible... T> void print(const char *str, T &&...);
template <c_formattible... T> void printPlain(const char *str, T &&...);

#define FWK_DUMP(...) \
fwk::print(fwk::detail::autoPrintFormat({FWK_STRINGIZE_MANY(__VA_ARGS__)}).c_str(), __VA_ARGS__)
fwk::print( \
("Dump at %:%:\n" + fwk::detail::autoPrintFormat({FWK_STRINGIZE_MANY(__VA_ARGS__)})) \
.c_str(), \
__FILE__, __LINE__, __VA_ARGS__)
}

#endif
21 changes: 12 additions & 9 deletions src/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace detail {
};

bool back_trim = false;
int arg_id = 0;
for(auto &arg : args) {
if(!arg)
break;
Expand All @@ -36,17 +37,19 @@ namespace detail {

DASSERT(elem);
back_trim = true;
if(elem[0] == '"' && elem[elem.size() - 1] == '"') {
out << "%";
back_trim = false;
} else if(anyOf(elem, isspace)) {
out << '"';
append_elem(elem);
out << "\":% ";
} else {
out << "- arg #" << arg_id;

bool is_string_literal = elem[0] == '"' && elem[elem.size() - 1] == '"';
bool is_numeric_value = allOf(elem, [](char c) { return isdigit(c) || c == '.'; });
bool is_const_value = is_string_literal || is_numeric_value;

if(!is_const_value) {
out << " (";
append_elem(elem);
out << ":% ";
out << ')';
}
out << ": %\n";
arg_id++;
}

if(back_trim)
Expand Down

0 comments on commit 79b3202

Please sign in to comment.