|
| 1 | +import { EventEmitter } from 'events'; |
| 2 | +import { ChildProcess, spawn, execFile } from 'child_process'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as path from 'path'; |
| 5 | +import * as os from 'os'; |
| 6 | +import { LogcatConfig, LogcatDeviceInfo, LogcatStatus } from '../shared/types'; |
| 7 | +import { wallClockPrefix } from './serialHandler'; |
| 8 | + |
| 9 | +export class LogcatHandler extends EventEmitter { |
| 10 | + private process: ChildProcess | null = null; |
| 11 | + private tempFilePath: string | null = null; |
| 12 | + private fd: number | null = null; |
| 13 | + private lineBuffer: string = ''; |
| 14 | + private linesReceived: number = 0; |
| 15 | + private connectedSince: number | null = null; |
| 16 | + private config: LogcatConfig | null = null; |
| 17 | + |
| 18 | + async listDevices(): Promise<LogcatDeviceInfo[]> { |
| 19 | + return new Promise((resolve, reject) => { |
| 20 | + execFile('adb', ['devices', '-l'], { timeout: 5000 }, (err, stdout, stderr) => { |
| 21 | + if (err) { |
| 22 | + reject(new Error(`adb not found or failed: ${err.message}`)); |
| 23 | + return; |
| 24 | + } |
| 25 | + resolve(parseAdbDevices(stdout)); |
| 26 | + }); |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + async connect(config: LogcatConfig): Promise<string> { |
| 31 | + if (this.process) { |
| 32 | + throw new Error('Already connected. Disconnect first.'); |
| 33 | + } |
| 34 | + |
| 35 | + // Create temp directory |
| 36 | + const tempDir = path.join(os.tmpdir(), 'logan-logcat'); |
| 37 | + if (!fs.existsSync(tempDir)) { |
| 38 | + fs.mkdirSync(tempDir, { recursive: true }); |
| 39 | + } |
| 40 | + |
| 41 | + // Create temp file |
| 42 | + const safeDevice = (config.device || 'default').replace(/[^a-zA-Z0-9]/g, '_'); |
| 43 | + const timestamp = Date.now(); |
| 44 | + this.tempFilePath = path.join(tempDir, `logcat_${safeDevice}_${timestamp}.log`); |
| 45 | + fs.writeFileSync(this.tempFilePath, ''); |
| 46 | + this.fd = fs.openSync(this.tempFilePath, 'a'); |
| 47 | + |
| 48 | + this.config = config; |
| 49 | + this.lineBuffer = ''; |
| 50 | + this.linesReceived = 0; |
| 51 | + this.connectedSince = Date.now(); |
| 52 | + |
| 53 | + // Build adb logcat command args |
| 54 | + const args: string[] = []; |
| 55 | + if (config.device) { |
| 56 | + args.push('-s', config.device); |
| 57 | + } |
| 58 | + args.push('logcat'); |
| 59 | + if (config.filter) { |
| 60 | + args.push(config.filter); |
| 61 | + } |
| 62 | + |
| 63 | + this.process = spawn('adb', args, { stdio: ['ignore', 'pipe', 'pipe'] }); |
| 64 | + |
| 65 | + this.process.stdout?.on('data', (chunk: Buffer) => { |
| 66 | + this.handleData(chunk); |
| 67 | + }); |
| 68 | + |
| 69 | + this.process.stderr?.on('data', (chunk: Buffer) => { |
| 70 | + const msg = chunk.toString('utf-8').trim(); |
| 71 | + if (msg) { |
| 72 | + this.emit('error', msg); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + this.process.on('error', (err: Error) => { |
| 77 | + this.emit('error', err.message); |
| 78 | + this.cleanup(); |
| 79 | + this.emit('disconnected'); |
| 80 | + }); |
| 81 | + |
| 82 | + this.process.on('close', (code) => { |
| 83 | + this.process = null; |
| 84 | + this.config = null; |
| 85 | + this.connectedSince = null; |
| 86 | + if (this.fd !== null) { |
| 87 | + // Flush partial line |
| 88 | + if (this.lineBuffer.length > 0) { |
| 89 | + fs.writeSync(this.fd, wallClockPrefix() + ' ' + this.lineBuffer + '\n'); |
| 90 | + this.linesReceived++; |
| 91 | + this.lineBuffer = ''; |
| 92 | + this.emit('lines-added', 1); |
| 93 | + } |
| 94 | + fs.closeSync(this.fd); |
| 95 | + this.fd = null; |
| 96 | + } |
| 97 | + this.emit('disconnected'); |
| 98 | + }); |
| 99 | + |
| 100 | + return this.tempFilePath; |
| 101 | + } |
| 102 | + |
| 103 | + private handleData(chunk: Buffer): void { |
| 104 | + if (!this.fd) return; |
| 105 | + |
| 106 | + const text = chunk.toString('utf-8'); |
| 107 | + this.lineBuffer += text; |
| 108 | + |
| 109 | + // Split on any line ending: \r\n, \n, \r |
| 110 | + const lines: string[] = []; |
| 111 | + let i = 0; |
| 112 | + let lineStart = 0; |
| 113 | + |
| 114 | + while (i < this.lineBuffer.length) { |
| 115 | + const ch = this.lineBuffer[i]; |
| 116 | + if (ch === '\n') { |
| 117 | + lines.push(this.lineBuffer.substring(lineStart, i)); |
| 118 | + lineStart = i + 1; |
| 119 | + } else if (ch === '\r') { |
| 120 | + lines.push(this.lineBuffer.substring(lineStart, i)); |
| 121 | + if (i + 1 < this.lineBuffer.length && this.lineBuffer[i + 1] === '\n') { |
| 122 | + i++; |
| 123 | + } |
| 124 | + lineStart = i + 1; |
| 125 | + } |
| 126 | + i++; |
| 127 | + } |
| 128 | + |
| 129 | + // Keep remainder in buffer (partial line) |
| 130 | + this.lineBuffer = this.lineBuffer.substring(lineStart); |
| 131 | + |
| 132 | + if (lines.length > 0) { |
| 133 | + // Write complete lines to temp file with wall clock timestamp |
| 134 | + const ts = wallClockPrefix(); |
| 135 | + const data = lines.map(l => ts + ' ' + l + '\n').join(''); |
| 136 | + fs.writeSync(this.fd, data); |
| 137 | + this.linesReceived += lines.length; |
| 138 | + this.emit('lines-added', lines.length); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + disconnect(): void { |
| 143 | + if (!this.process) return; |
| 144 | + |
| 145 | + try { |
| 146 | + this.process.kill('SIGTERM'); |
| 147 | + } catch { |
| 148 | + // Process may already be dead |
| 149 | + } |
| 150 | + // The 'close' event handler will do the rest |
| 151 | + } |
| 152 | + |
| 153 | + getStatus(): LogcatStatus { |
| 154 | + return { |
| 155 | + connected: this.process !== null && !this.process.killed, |
| 156 | + deviceId: this.config?.device || null, |
| 157 | + filter: this.config?.filter || null, |
| 158 | + linesReceived: this.linesReceived, |
| 159 | + connectedSince: this.connectedSince, |
| 160 | + tempFilePath: this.tempFilePath, |
| 161 | + }; |
| 162 | + } |
| 163 | + |
| 164 | + getTempFilePath(): string | null { |
| 165 | + return this.tempFilePath; |
| 166 | + } |
| 167 | + |
| 168 | + cleanupTempFile(): void { |
| 169 | + this.disconnect(); |
| 170 | + // Wait a tick for the close handler to flush |
| 171 | + if (this.fd !== null) { |
| 172 | + try { fs.closeSync(this.fd); } catch {} |
| 173 | + this.fd = null; |
| 174 | + } |
| 175 | + if (this.tempFilePath && fs.existsSync(this.tempFilePath)) { |
| 176 | + try { |
| 177 | + fs.unlinkSync(this.tempFilePath); |
| 178 | + } catch { |
| 179 | + // Best effort |
| 180 | + } |
| 181 | + } |
| 182 | + this.tempFilePath = null; |
| 183 | + } |
| 184 | + |
| 185 | + private cleanup(): void { |
| 186 | + if (this.fd !== null) { |
| 187 | + fs.closeSync(this.fd); |
| 188 | + this.fd = null; |
| 189 | + } |
| 190 | + if (this.tempFilePath && fs.existsSync(this.tempFilePath)) { |
| 191 | + try { |
| 192 | + fs.unlinkSync(this.tempFilePath); |
| 193 | + } catch { |
| 194 | + // Best effort |
| 195 | + } |
| 196 | + } |
| 197 | + this.tempFilePath = null; |
| 198 | + this.config = null; |
| 199 | + this.connectedSince = null; |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +/** Parse `adb devices -l` output into LogcatDeviceInfo[] */ |
| 204 | +export function parseAdbDevices(output: string): LogcatDeviceInfo[] { |
| 205 | + const devices: LogcatDeviceInfo[] = []; |
| 206 | + const lines = output.split('\n'); |
| 207 | + |
| 208 | + for (const line of lines) { |
| 209 | + // Skip header and empty lines |
| 210 | + const trimmed = line.trim(); |
| 211 | + if (!trimmed || trimmed.startsWith('List of devices') || trimmed.startsWith('*')) { |
| 212 | + continue; |
| 213 | + } |
| 214 | + |
| 215 | + // Format: <serial> <state> [usb:<...>] [product:<...>] [model:<...>] [device:<...>] [transport_id:<...>] |
| 216 | + const parts = trimmed.split(/\s+/); |
| 217 | + if (parts.length < 2) continue; |
| 218 | + |
| 219 | + const id = parts[0]; |
| 220 | + const state = parts[1]; |
| 221 | + |
| 222 | + // Extract model from key:value pairs |
| 223 | + let model: string | undefined; |
| 224 | + for (const part of parts.slice(2)) { |
| 225 | + if (part.startsWith('model:')) { |
| 226 | + model = part.substring(6).replace(/_/g, ' '); |
| 227 | + break; |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + devices.push({ id, state, model }); |
| 232 | + } |
| 233 | + |
| 234 | + return devices; |
| 235 | +} |
0 commit comments