write logs atomically

This commit is contained in:
Florian Dold 2021-12-01 12:22:58 +01:00
parent 54d4a1efe0
commit 718595a572
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B

View File

@ -71,22 +71,23 @@ function writeNodeLog(
args: any[],
): void {
try {
process.stderr.write(`${new Date().toISOString()} ${tag} ${level} `);
process.stderr.write(`${message}`);
let msg = `${new Date().toISOString()} ${tag} ${level} ${message}`;
if (args.length != 0) {
process.stderr.write(" ");
process.stderr.write(JSON.stringify(args, undefined, 2));
msg += ` ${JSON.stringify(args, undefined, 2)}\n`;
} else {
msg += `\n`;
}
process.stderr.write("\n");
process.stderr.write(msg);
} catch (e) {
// This can happen when we're trying to log something that doesn't want to be
// converted to a string.
process.stderr.write(`${new Date().toISOString()} (logger) FATAL `);
let msg = `${new Date().toISOString()} (logger) FATAL `;
if (e instanceof Error) {
process.stderr.write("failed to write log: ");
process.stderr.write(e.message);
msg += `failed to write log: ${e.message}\n`;
} else {
msg += "failed to write log\n";
}
process.stderr.write("\n");
process.stderr.write(msg);
}
}