8000 Limiting the output when logging large string and binary values by jedelbo · Pull Request #6986 · realm/realm-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Limiting the output when logging large string and binary values #6986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/realm/mixed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,57 @@ StringData Mixed::get_index_data(std::array<char, 16>& buffer) const noexcept
return {};
}

std::string Mixed::to_string(size_t max_size) const noexcept
{
std::ostringstream ostr;
if (is_type(type_String)) {
std::string ret = "\"";
if (string_val.size() <= max_size) {
ret += std::string(string_val);
}
else {
ret += std::string(StringData(string_val.data(), max_size)) + " ...";
}
ret += "\"";
return ret;
}
else if (is_type(type_Binary)) {
static constexpr int size_one_hex_out = 3;
static char hex_chars[] = "0123456789ABCDEF";
auto out_hex = [&ostr](char c) {
ostr << hex_chars[c >> 4];
ostr << hex_chars[c & 0xF];
ostr << ' ';
};

auto sz = binary_val.size();
bool capped = false;
size_t out_size = 0;

ostr << '"';
for (size_t n = 0; n < sz; n++) {
out_size += size_one_hex_out;
if (out_size > max_size) {
capped = true;
break;
}
out_hex(binary_val[n]);
}
if (capped) {
ostr << "...";
}
ostr << '"';
}
else if (is_type(type_Timestamp)) {
char buffer[32];
return date_val.to_string(buffer);
}
else {
ostr << *this;
}
return ostr.str();
}

void Mixed::use_buffer(std::string& buf) noexcept
{
if (is_null()) {
Expand Down
4 changes: 4 additions & 0 deletions src/realm/mixed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ class Mixed {
Mixed operator/(const Mixed&) const noexcept;

size_t hash() const;
// Used when inserting values into index
StringData get_index_data(std::array<char, 16>&) const noexcept;
// Used when logging values
std::string to_string(size_t max_size) const noexcept;
// Used when you need a backup buffer for string or binary value
void use_buffer(std::string& buf) noexcept;

void to_json(std::ostream& out, JSONOutputMode output_mode) const noexcept;
Expand Down
6 changes: 4 additions & 2 deletions src/realm/replication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ void Replication::set(const Table* t, ColKey col_key, ObjKey key, Mixed value, _
}
}
else {
logger->log(util::Logger::Level::trace, " Set '%1' to %2", t->get_column_name(col_key), value);
logger->log(util::Logger::Level::trace, " Set '%1' to %2", t->get_column_name(col_key),
value.to_string(util::Logger::max_width_of_value));
}
}
}
Expand Down Expand Up @@ -297,7 +298,8 @@ void Replication::log_collection_operation(const char* operation, const Collecti
}
}
else {
logger->log(util::Logger::Level::trace, " %1 %2 in %3%4", operation, value, path, position);
logger->log(util::Logger::Level::trace, " %1 %2 in %3%4", operation,
value.to_string(util::Logger::max_width_of_value), path, position);
}
}
void Replication::list_insert(const CollectionBase& list, size_t list_ndx, Mixed value, size_t)
Expand Down
2 changes: 2 additions & 0 deletions src/realm/util/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class Logger {
// this is enforced in logging.cpp.
enum class Level { all = 0, trace = 1, debug = 2, detail = 3, info = 4, warn = 5, error = 6, fatal = 7, off = 8 };

static constexpr size_t max_width_of_value = 80;

template <class... Params>
void log(Level, const char* message, Params&&...);

Expand Down
17 changes: 17 additions & 0 deletions test/test_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5377,14 +5377,28 @@ TEST(Table_LoggingMutations)
auto t = wt->add_table_with_primary_key("foo", type_Int, "id");
col = t->add_column(type_Mixed, "any");
col_int = t->add_column(type_Int, "int");

auto dict =
t->create_object_with_primary_key(1).set_collection(col, CollectionType::Dictionary).get_dictionary(col);
dict.insert("hello", "world");

auto list =
t->create_object_with_primary_key(2).set_collection(col, CollectionType::List).get_list<Mixed>(col);
list.add(47.50);

auto set = t->create_object_with_primary_key(3).set_collection(col, CollectionType::Set).get_set<Mixed>(col);
set.insert(false);

std::vector<char> str_data(90);
std::iota(str_data.begin(), str_data.end(), ' ');
t->create_object_with_primary_key(5).set_any(col, StringData(str_data.data(), str_data.size()));

std::vector<char> bin_data(50);
std::iota(bin_data.begin(), bin_data.end(), 0);
t->create_object_with_primary_key(6).set_any(col, BinaryData(bin_data.data(), bin_data.size()));

t->create_object_with_primary_key(7).set_any(col, Timestamp(1695207215, 0));

wt->commit();
}
{
Expand All @@ -5397,6 +5411,9 @@ TEST(Table_LoggingMutations)

auto str = buffer.str();
// std::cout << str << std::endl;
CHECK(str.find("abcdefghijklmno ...") != std::string::npos);
CHECK(str.find("14 15 16 17 18 19 ...") != std::string::npos);
CHECK(str.find("2023-09-20 10:53:35") != std::string::npos);
CHECK(str.find("Query::get_description() failed:") != std::string::npos);
CHECK(str.find("Set 'any' to dictionary") != std::string::npos);
CHECK(str.find("Set 'any' to list") != std::string::npos);
Expand Down
0