mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:47:45 +00:00
AK: Add LogStream overload for ReadonlyBytes.
This is extremely useful for debugging.
This commit is contained in:
parent
bc48181939
commit
cd2815ed87
2 changed files with 42 additions and 0 deletions
|
@ -209,4 +209,45 @@ const LogStream& operator<<(const LogStream& stream, float value)
|
||||||
|
|
||||||
#endif
|
#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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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&, const void*);
|
||||||
|
const LogStream& operator<<(const LogStream&, ReadonlyBytes);
|
||||||
|
|
||||||
inline const LogStream& operator<<(const LogStream& stream, char value)
|
inline const LogStream& operator<<(const LogStream& stream, char value)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue