( deviceId: string, appBundleId: string, stream: fs.WriteStream, redactionPatterns: RegExp[], pidPath?: string, )
| 71 | } |
| 72 | |
| 73 | export async function startAndroidAppLog( |
| 74 | deviceId: string, |
| 75 | appBundleId: string, |
| 76 | stream: fs.WriteStream, |
| 77 | redactionPatterns: RegExp[], |
| 78 | pidPath?: string, |
| 79 | ): Promise<AppLogResult> { |
| 80 | let state: AppLogState = 'recovering'; |
| 81 | let stopped = false; |
| 82 | let activeChild: AndroidAdbProcess | undefined; |
| 83 | let activeWait: ReturnType<typeof attachChildToStream> | undefined; |
| 84 | |
| 85 | const wait = (async () => { |
| 86 | try { |
| 87 | while (!stopped) { |
| 88 | const pid = await resolveAndroidPid(deviceId, appBundleId); |
| 89 | if (!pid) { |
| 90 | state = 'recovering'; |
| 91 | await sleep(1_000); |
| 92 | continue; |
| 93 | } |
| 94 | const provider = resolveAndroidAdbProvider(androidDeviceForSerial(deviceId)); |
| 95 | const child = streamAndroidLogcatWithAdb(provider, { pid }); |
| 96 | activeChild = child; |
| 97 | const writer = createLineWriter(stream, { redactionPatterns }); |
| 98 | activeWait = attachChildToStream(child, stream, { endStreamOnClose: false, writer }); |
| 99 | if (typeof child.pid === 'number') { |
| 100 | writePidFile(pidPath, child.pid); |
| 101 | } |
| 102 | state = 'active'; |
| 103 | await activeWait; |
| 104 | clearPidFile(pidPath); |
| 105 | activeChild = undefined; |
| 106 | activeWait = undefined; |
| 107 | if (stopped) return { stdout: '', stderr: '', exitCode: 0 }; |
| 108 | state = 'recovering'; |
| 109 | await sleep(500); |
| 110 | } |
| 111 | return { stdout: '', stderr: '', exitCode: 0 }; |
| 112 | } finally { |
| 113 | stream.end(); |
| 114 | clearPidFile(pidPath); |
| 115 | } |
| 116 | })(); |
| 117 | |
| 118 | return { |
| 119 | backend: 'android', |
| 120 | getState: () => state, |
| 121 | startedAt: Date.now(), |
| 122 | wait, |
| 123 | stop: async () => { |
| 124 | stopped = true; |
| 125 | if (activeChild && !activeChild.killed) { |
| 126 | activeChild.kill('SIGINT'); |
| 127 | } |
| 128 | if (activeWait) await waitForChildExit(activeWait); |
| 129 | if (activeChild && !activeChild.killed) { |
| 130 | activeChild.kill('SIGKILL'); |
no test coverage detected