mirror of
https://github.com/apple/foundationdb.git
synced 2025-06-01 10:45:56 +08:00
Emit valid json for unprintable characters
This commit is contained in:
parent
cdc7e83993
commit
4b31d209da
@ -45,14 +45,22 @@ const char* JsonTraceLogFormatter::getFooter() {
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
void escapeString(std::stringstream& ss, const std::string source) {
|
void escapeString(std::stringstream& ss, const std::string& source) {
|
||||||
for (auto c : source) {
|
for (auto c : source) {
|
||||||
if (c == '"') {
|
if (c == '"') {
|
||||||
ss << "\\\"";
|
ss << "\\\"";
|
||||||
} else if (c == '\\') {
|
} else if (c == '\\') {
|
||||||
ss << "\\\\";
|
ss << "\\\\";
|
||||||
} else {
|
} else if (c == '\n') {
|
||||||
|
ss << "\\n";
|
||||||
|
} else if (c == '\r') {
|
||||||
|
ss << "\\r";
|
||||||
|
} else if (isprint(c)) {
|
||||||
ss << c;
|
ss << c;
|
||||||
|
} else {
|
||||||
|
constexpr char hex[] = "0123456789abcdef";
|
||||||
|
int x = int{ static_cast<uint8_t>(c) };
|
||||||
|
ss << "\\x" << hex[x / 16] << hex[x % 16];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user