Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(diagnostics): never break a console call, and keep the current log when main.old.log cannot be read

Marc Diaz83d49300d8ce
diff --git a/electron/diagnostics/main-log-file.test.ts b/electron/diagnostics/main-log-file.test.ts
index d6f1e8425..f39531034 100644
--- a/electron/diagnostics/main-log-file.test.ts
+++ b/electron/diagnostics/main-log-file.test.ts
@@ -196,6 +196,34 @@ describe("MainLogFile", () => {
expect(lines()).toHaveLength(2);
});
+ it("keeps the current log when main.old.log cannot be read", () => {
+ silenceConsole();
+ // A directory opens but does not read, so only the older half of the tail fails.
+ fs.mkdirSync(path.join(dir, "main.old.log"));
+ const log = make();
+ log.install(dir, "launch");
+ console.info("current");
+
+ expect(log.readTail(1000)).toContain("info current");
+ expect(log.lastError).toBeTruthy();
+ });
+
+ it("still calls the console when an argument cannot be turned into text", () => {
+ silenceConsole();
+ const original = vi.fn();
+ console.info = original;
+ make().install(dir, "launch");
+ // No prototype, so no toString, and a toJSON that throws.
+ const unprintable = Object.assign(Object.create(null), {
+ toJSON: () => {
+ throw new Error("no");
+ },
+ });
+
+ expect(() => console.info(unprintable)).not.toThrow();
+ expect(original).toHaveBeenCalledWith(unprintable);
+ });
+
it("uninstall restores the console methods", () => {
const captured = silenceConsole();
const original = console.info;
diff --git a/electron/diagnostics/main-log-file.ts b/electron/diagnostics/main-log-file.ts
index da0486d4f..193454b3d 100644
--- a/electron/diagnostics/main-log-file.ts
+++ b/electron/diagnostics/main-log-file.ts
@@ -75,7 +75,12 @@ export class MainLogFile {
const original = console_[level];
this.originals[level] = original;
console_[level] = (...args: unknown[]) => {
- this.record(level, formatArgs(args));
+ // Logging to the file must never break the console call it rides on.
+ try {
+ this.record(level, formatArgs(args));
+ } catch {
+ // An argument that cannot even be turned into text: the console still gets it.
+ }
original.apply(console, args);
};
}
@@ -117,9 +122,14 @@ export class MainLogFile {
data = current.data;
whole = current.whole;
if (current.whole && data.length < maxBytes) {
- const old = readLastBytes(path.join(this.dir, OLD_LOG_FILE), maxBytes - data.length);
- data = Buffer.concat([old.data, data]);
- whole = old.whole;
+ try {
+ const old = readLastBytes(path.join(this.dir, OLD_LOG_FILE), maxBytes - data.length);
+ data = Buffer.concat([old.data, data]);
+ whole = old.whole;
+ } catch (error) {
+ // The current log was read: keep it, and report the older one.
+ this.error = describeError(error);
+ }
}
} catch (error) {
// Reported in the bundle as `logFileError`; writing carries on.