[log] Escape newline with \n

Change-Id: I456b3456351860e3e5e7e9dcb800d42d543a7c47
Reviewed-on: https://chromium-review.googlesource.com/753681
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Reviewed-by: Peter Marshall <petermarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49110}
This commit is contained in:
Camillo Bruni 2017-11-03 17:14:44 +01:00 committed by Commit Bot
parent cc5574799c
commit e4a97a2e6f
2 changed files with 18 additions and 11 deletions

View File

@ -170,17 +170,19 @@ void Log::MessageBuilder::AppendStringPart(const char* str, int len) {
void Log::MessageBuilder::AppendCharacter(char c) {
OFStream& os = log_->os_;
// A log entry (separate by commas) cannot contain commas or line-brakes.
// A log entry (separate by commas) cannot contain commas or line-breaks.
if (c >= 32 && c <= 126) {
if (c == ',') {
// Escape commas directly.
// Escape commas (log field separator) directly.
os << "\x2c";
} else {
// Directly append any printable ascii character.
os << c;
}
} else if (c == '\n') {
os << "\\n";
} else {
// Escape any non-printable haracters.
// Escape any non-printable characters.
Append("\\x%02x", c);
}
}

View File

@ -46,16 +46,21 @@ class CsvParser {
while (nextPos !== -1) {
let escapeIdentifier = string.charAt(nextPos + 1);
pos = nextPos + 2;
if (escapeIdentifier == 'x') {
// \x00 ascii range escapes consume 2 chars.
nextPos = pos + 2;
if (escapeIdentifier == 'n') {
result += '\n';
nextPos = pos;
} else {
// \u0000 unicode range escapes consume 4 chars.
nextPos = pos + 4;
if (escapeIdentifier == 'x') {
// \x00 ascii range escapes consume 2 chars.
nextPos = pos + 2;
} else {
// \u0000 unicode range escapes consume 4 chars.
nextPos = pos + 4;
}
// Convert the selected escape sequence to a single character.
let escapeChars = string.substring(pos, nextPos);
result += String.fromCharCode(parseInt(escapeChars, 16));
}
// Convert the selected escape sequence to a single character.
let escapeChars = string.substring(pos, nextPos);
result += String.fromCharCode(parseInt(escapeChars, 16));
// Continue looking for the next escape sequence.
pos = nextPos;