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): install the log file before module-load logging

Marc Diazef4db3dc508b
diff --git a/electron/diagnostics/main-log-file.test.ts b/electron/diagnostics/main-log-file.test.ts
index 1321318d4..b714863c4 100644
--- a/electron/diagnostics/main-log-file.test.ts
+++ b/electron/diagnostics/main-log-file.test.ts
@@ -150,6 +150,20 @@ describe("MainLogFile", () => {
expect(lines()).toHaveLength(1);
});
+ it("reports a failed read and keeps writing", () => {
+ silenceConsole();
+ const log = make();
+ log.install(dir, "launch");
+ vi.spyOn(fs, "readSync").mockImplementationOnce(() => {
+ throw Object.assign(new Error("EIO: i/o error, read"), { code: "EIO" });
+ });
+
+ expect(log.readTail(1000)).toBe("");
+ expect(log.lastError).toMatch(/^EIO/);
+ console.info("after");
+ expect(lines()).toHaveLength(2);
+ });
+
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 cd3941a39..926f02f3c 100644
--- a/electron/diagnostics/main-log-file.ts
+++ b/electron/diagnostics/main-log-file.ts
@@ -110,13 +110,21 @@ export class MainLogFile {
readTail(maxBytes: number): string {
if (!this.dir) return "";
this.flush();
- const current = readLastBytes(path.join(this.dir, LOG_FILE), maxBytes);
- let data = current.data;
- let 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;
+ let data: Buffer;
+ let whole: boolean;
+ try {
+ const current = readLastBytes(path.join(this.dir, LOG_FILE), maxBytes);
+ 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;
+ }
+ } catch (error) {
+ // Reported in the bundle as `logFileError`; writing carries on.
+ this.error = describeError(error);
+ return "";
}
const text = data.toString("utf-8");
return whole ? text : text.slice(text.indexOf("\n") + 1);
@@ -216,7 +224,7 @@ export class MainLogFile {
private fail(error: unknown): void {
this.close();
- this.error = error instanceof Error ? error.message : String(error);
+ this.error = describeError(error);
}
private close(): void {
@@ -230,6 +238,10 @@ export class MainLogFile {
}
}
+function describeError(error: unknown): string {
+ return error instanceof Error ? error.message : String(error);
+}
+
function formatArgs(args: unknown[]): string {
return args
.map((arg) => {
@@ -250,8 +262,11 @@ function readLastBytes(file: string, maxBytes: number): { data: Buffer; whole: b
let fd: number;
try {
fd = fs.openSync(file, "r");
- } catch {
- return { data: Buffer.alloc(0), whole: true };
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") {
+ return { data: Buffer.alloc(0), whole: true };
+ }
+ throw error;
}
try {
const size = fs.fstatSync(fd).size;
diff --git a/electron/main.ts b/electron/main.ts
index 97c0a24e5..d650bc5b5 100644
--- a/electron/main.ts
+++ b/electron/main.ts
@@ -96,6 +96,28 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
// HUD/tray/menu. Parsed before any GUI side effects; see electron/cli/.
const cliCommand = parseCliArgs(process.argv, app.isPackaged ? 1 : 2);
+// CLI runs skip the single-instance lock so `openscreen export/record` works
+// while the GUI app is open. They share its userData, but not its Chromium
+// profile: runCli gives each run its own sessionData (see cli/sessionData.ts).
+const hasSingleInstanceLock = cliCommand ? false : app.requestSingleInstanceLock();
+
+// On every app launch, not only under OPENSCREEN_DIAGNOSTIC: a user who saves a
+// bundle from the tray started the app where no such variable can be set. Installed
+// here, before anything below logs. Only the instance holding the lock writes, so a
+// second launch never rotates the file under the running one, and never in a CLI run,
+// whose stdout is its own protocol.
+if (hasSingleInstanceLock) {
+ // The lock is per userData, but macOS's default logs folder is per app name: an
+ // instance on another userData gets its own folder, or two would share one file.
+ const userData = app.getPath("userData");
+ if (userData === path.join(app.getPath("appData"), app.getName())) {
+ app.setAppLogsPath();
+ } else {
+ app.setAppLogsPath(path.join(userData, "logs"));
+ }
+ mainLogFile.install(app.getPath("logs"), `launch ${app.getVersion()} pid ${process.pid}`);
+}
+
// Use Screen & System Audio Recording permissions instead of the CoreAudio Tap API on macOS.
// Tap needs NSAudioCaptureUsageDescription in the parent app's Info.plist, which breaks when
// running from a terminal/IDE during dev.
@@ -201,20 +223,6 @@ function showMainWindow() {
createWindow();
}
-// CLI runs skip the single-instance lock so `openscreen export/record` works
-// while the GUI app is open. They share its userData, but not its Chromium
-// profile: runCli gives each run its own sessionData (see cli/sessionData.ts).
-const hasSingleInstanceLock = cliCommand ? false : app.requestSingleInstanceLock();
-
-// On every app launch, not only under OPENSCREEN_DIAGNOSTIC: a user who saves a
-// bundle from the tray started the app where no such variable can be set. Only the
-// instance holding the lock writes, so a second launch never rotates the file under
-// the running one, and never in a CLI run, whose stdout is its own protocol.
-if (hasSingleInstanceLock) {
- app.setAppLogsPath();
- mainLogFile.install(app.getPath("logs"), `launch ${app.getVersion()} pid ${process.pid}`);
-}
-
if (cliCommand) {
runCli(cliCommand);
} else if (hasSingleInstanceLock) {
diff --git a/technical-documentation/testing/manual-e2e-checklist.md b/technical-documentation/testing/manual-e2e-checklist.md
index c907b7384..0e87378c0 100644
--- a/technical-documentation/testing/manual-e2e-checklist.md
+++ b/technical-documentation/testing/manual-e2e-checklist.md
@@ -97,7 +97,7 @@ gh run download <run-id> -R getopenscreen/openscreen -n openscreen-windows-x64
- AppImage: `chmod +x Openscreen-Linux-<version>.AppImage`, then run it; add `--no-sandbox` if Chromium's sandbox cannot start. A package: install it with the distribution's tool (`sudo apt install ./Openscreen-Linux-<version>.deb`, `sudo dnf install ./Openscreen-Linux-<version>.rpm`, `sudo pacman -U Openscreen-Linux-<version>.pacman`), then run `openscreen`.
- No flag is needed. Which name to grant is **to confirm on the first run**: a package installs a desktop entry, an AppImage has none until it is integrated.
-**Logs of a packaged build.** The main process logs to its standard output, so a launch from a terminal shows it there, `[content-protection] OFF for the HUD window` included; whether the Windows executable prints into the PowerShell that started it is **to confirm on the first run**. However it was launched, the same lines are appended to `main.log` in `~/Library/Logs/Openscreen`, `%APPDATA%\Openscreen\logs` or `~/.config/Openscreen/logs`, one `--- launch <version> pid <pid> ---` header per launch. Past 5 MiB it becomes `main.old.log` and starts over, so the folder never holds more than two files. Tray → *Save Diagnostics* writes the last 256 KiB of them to a JSON file of your choice (`mainProcessLogs`, with `logFile` and, if the file could not be written, `logFileError`), with the capture helper's recent output (`helperOutput.windows`, `.mac` or `.linux`). It leaves the open project out; a project file is in `userData/projects`. The renderer's `console` calls are stripped from packaged builds. Recordings and their `.cursor.json` and `.session.json` sidecars are in the `recordings` folder of `%APPDATA%\Openscreen`, `~/Library/Application Support/Openscreen` or `~/.config/Openscreen`.
+**Logs of a packaged build.** The main process logs to its standard output, so a launch from a terminal shows it there, `[content-protection] OFF for the HUD window` included; whether the Windows executable prints into the PowerShell that started it is **to confirm on the first run**. However it was launched, the same lines are appended to `main.log` in `~/Library/Logs/Openscreen`, `%APPDATA%\Openscreen\logs` or `~/.config/openscreen/logs`, one `--- launch <version> pid <pid> ---` header per launch. Past 5 MiB it becomes `main.old.log` and starts over, so the folder never holds more than two files. Tray → *Save Diagnostics* writes the last 256 KiB of them to a JSON file of your choice (`mainProcessLogs`, with `logFile` and, if the file could not be written, `logFileError`), with the capture helper's recent output (`helperOutput.windows`, `.mac` or `.linux`). It leaves the open project out; a project file is in `userData/projects`. The renderer's `console` calls are stripped from packaged builds. Recordings and their `.cursor.json` and `.session.json` sidecars are in the `recordings` folder of `%APPDATA%\Openscreen`, `~/Library/Application Support/Openscreen` or `~/.config/openscreen`.
## Launch and HUD