Skip to content

Commit

Permalink
feat: add native logs for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
maciekstosio committed Nov 18, 2024
1 parent 922191e commit bbf3157
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
30 changes: 28 additions & 2 deletions packages/vscode-extension/src/devices/IosSimulatorDevice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from "path";
import fs from "fs";
import { OutputChannel, window } from "vscode";
import { ExecaError } from "execa";
import { getAppCachesDir, getOldAppCachesDir } from "../utilities/common";
import { getAppCachesDir, getOldAppCachesDir, watchFileContent } from "../utilities/common";
import { DeviceBase } from "./DeviceBase";
import { Preview } from "./preview";
import { Logger } from "../Logger";
Expand Down Expand Up @@ -52,6 +53,8 @@ type PrivacyServiceName =
| "siri";

export class IosSimulatorDevice extends DeviceBase {
private nativeLogsOutputChannel: OutputChannel | undefined;

constructor(private readonly deviceUDID: string, private readonly _deviceInfo: DeviceInfo) {
super();
}
Expand All @@ -72,6 +75,7 @@ export class IosSimulatorDevice extends DeviceBase {

public dispose() {
super.dispose();
this.nativeLogsOutputChannel?.dispose();
return exec("xcrun", [
"simctl",
"--set",
Expand Down Expand Up @@ -346,16 +350,38 @@ export class IosSimulatorDevice extends DeviceBase {
await Promise.all(matches.map(terminateApp));
}

async launchWithBuild(build: IOSBuildResult) {
private getNativeLogsPath() {
const deviceSetLocation = getOrCreateDeviceSet(this.deviceUDID);
const logFile = path.join(deviceSetLocation, this.deviceUDID, "data", "radon_ide_ios_process.log");

return logFile;
}

mirrorNativeLogs(logFile: string) {
this.nativeLogsOutputChannel = window.createOutputChannel("Radon IDE (iOS Native Logs)", 'log');
this.nativeLogsOutputChannel.clear();

if (fs.existsSync(logFile)) {
fs.unlinkSync(logFile);
}

watchFileContent(logFile, this.nativeLogsOutputChannel?.append);
}

async launchWithBuild(build: IOSBuildResult) {
const deviceSetLocation = getOrCreateDeviceSet(this.deviceUDID);
const logFile = this.getNativeLogsPath();

await this.terminateAnyRunningApplications();
this.mirrorNativeLogs(logFile);

await exec("xcrun", [
"simctl",
"--set",
deviceSetLocation,
"launch",
`--stdout=${logFile}`,
`--stderr=${logFile}`,
"--terminate-running-process",
this.deviceUDID,
build.bundleID,
Expand Down
29 changes: 29 additions & 0 deletions packages/vscode-extension/src/utilities/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,32 @@ export async function calculateMD5(fsPath: string, hash: Hash = createHash("md5"
}
return hash;
}

const readFileWithSize = (filePath: string, from: number): Promise<{size: number, data: string}> => new Promise((resolve, reject) => {
let size = 0;
const chunks: Uint8Array[] = [];
const stream = fs.createReadStream(filePath, {start: from});

stream.on('data', function(chunk: Uint8Array) {
size += chunk.length;
chunks.push(chunk);
});

stream.on('error', () => reject());

stream.on('close', () => resolve({size, data: Buffer.concat(chunks).toString()}));
});

export const watchFileContent = function (filePath: string, callback: (data: string) => void) {
let fileEnd = 0;

fs.watchFile(filePath, async (curr, prev) => {
if (curr.mtime > prev.mtime) {
const {data, size} = await readFileWithSize(filePath, fileEnd);
fileEnd += size;
callback(data);
}
});
};


0 comments on commit bbf3157

Please sign in to comment.