AK: Add LogStream overload for ReadonlyBytes.

This is extremely useful for debugging.
This commit is contained in:
asynts 2020-09-06 15:02:46 +02:00 committed by Andreas Kling
parent bc48181939
commit cd2815ed87
Notes: sideshowbarker 2024-07-19 02:52:29 +09:00
2 changed files with 42 additions and 0 deletions

View File

@ -209,4 +209,45 @@ const LogStream& operator<<(const LogStream& stream, float value)
#endif
const LogStream& operator<<(const LogStream& stream, ReadonlyBytes bytes)
{
StringBuilder builder;
u8 buffered_byte = 0;
size_t nrepeat = 0;
const char* prefix = "";
auto flush = [&]() {
if (nrepeat > 0) {
if (nrepeat == 1)
builder.appendf("%s0x%02x", prefix, static_cast<int>(buffered_byte));
else
builder.appendf("%s%zu * 0x%02x", prefix, nrepeat, static_cast<int>(buffered_byte));
nrepeat = 0;
prefix = ", ";
}
};
builder.append("{ ");
for (auto byte : bytes) {
if (nrepeat > 0) {
if (byte != buffered_byte)
flush();
buffered_byte = byte;
nrepeat++;
} else {
buffered_byte = byte;
nrepeat = 1;
}
}
flush();
builder.append(" }");
return stream << builder.to_string();
}
}

View File

@ -184,6 +184,7 @@ const LogStream& operator<<(const LogStream& stream, Span<T> span)
}
const LogStream& operator<<(const LogStream&, const void*);
const LogStream& operator<<(const LogStream&, ReadonlyBytes);
inline const LogStream& operator<<(const LogStream& stream, char value)
{