format import type { Client as ADBClient } from 'adbkit' ; import { Priority } from 'adbkit-logcat' ; import { DeviceLogEntry , DeviceLogLevel } from 'flipper-common' ; import { DeviceListener } from '../../utils/DeviceListener' ; export class AndroidLogListener extends DeviceListener { constructor ( isDeviceConnected , private onNewLogEntry , private readonly adb , private readonly serial ) { super ( isDeviceConnected ) ; } protected async startListener ( ) { const reader = await this . adb . openLogcat ( this . serial , { clear : true , } ) ; let gracefulShutdown = false ; let lastKnownError ; reader . on ( 'entry' , ( entry ) => { let type = 'unknown' ; if ( entry . priority === Priority . VERBOSE ) { type = 'verbose' ; } if ( entry . priority === Priority . DEBUG ) { type = 'debug' ; } if ( entry . priority === Priority . INFO ) { type = 'info' ; } if ( entry . priority === Priority . WARN ) { type = 'warn' ; } if ( entry . priority === Priority . ERROR ) { type = 'error' ; } if ( entry . priority === Priority . FATAL ) { type = 'fatal' ; } this . onNewLogEntry ( { tag : entry . tag , pid : entry . pid , tid : entry . tid , message : entry . message , date : entry . date , type , } ) ; } ) . on ( 'end' , ( ) => { if ( ! gracefulShutdown ) { console . error ( 'Unexpected shutdown of adb logcat' ) ; this . _state . set ( 'fatal' , lastKnownError ?? new Error ( 'Unexpected shutdown of adb logcat' ) ) ; } } ) . on ( 'error' , ( e ) => { console . warn ( 'Failed to read from adb logcat: ' , e ) ; lastKnownError = e ; } ) ; return ( ) => { gracefulShutdown = true ; reader . end ( ) ; } ; } }