parameters: (base_dir, adb_cmd='adb')
docstring: Monitor adb forever.  Expects a SIGINT (Ctrl-C) to kill.
docstring_summary: Monitor adb forever.  Expects a SIGINT (Ctrl-C) to kill.
docstring_tokens: Monitor adb forever . Expects a SIGINT ( Ctrl - C ) to kill .
function: def main(base_dir, adb_cmd='adb'):
    """Monitor adb forever.  Expects a SIGINT (Ctrl-C) to kill."""
    # We create the directory to ensure 'run once' semantics
    if os.path.exists(base_dir):
      print('adb_logcat_monitor: %s already exists? Cleaning' % base_dir)
      shutil.rmtree(base_dir, ignore_errors=True)
  
    os.makedirs(base_dir)
    logging.basicConfig(filename=os.path.join(base_dir, 'eventlog'),
                        level=logging.INFO,
                        format='%(asctime)-2s %(levelname)-8s %(message)s')
  
    # Set up the alarm for calling 'adb devices'. This is to ensure
    # our script doesn't get stuck waiting for a process response
    def TimeoutHandler(_signum, _unused_frame):
      raise TimeoutException()
    signal.signal(signal.SIGALRM, TimeoutHandler)
  
    # Handle SIGTERMs to ensure clean shutdown
    def SigtermHandler(_signum, _unused_frame):
      raise SigtermError()
    signal.signal(signal.SIGTERM, SigtermHandler)
  
    logging.info('Started with pid %d', os.getpid())
    pid_file_path = os.path.join(base_dir, 'LOGCAT_MONITOR_PID')
  
    try:
      with open(pid_file_path, 'w') as f:
        f.write(str(os.getpid()))
      while True:
        for device_id in GetAttachedDevices(adb_cmd):
          if not device_id in devices:
            subprocess.call([adb_cmd, '-s', device_id, 'logcat', '-c'])
            devices[device_id] = (None, 0)
  
        for device in devices:
          # This will spawn logcat watchers for any device ever detected
          StartLogcatIfNecessary(device, adb_cmd, base_dir)
  
        time.sleep(5)
    except SigtermError:
      logging.info('Received SIGTERM, shutting down')
    except: # pylint: disable=bare-except
      logging.exception('Unexpected exception in main.')
    finally:
      for process, _ in devices.values():
        if process:
          try:
            process.terminate()
          except OSError:
            pass
      os.remove(pid_file_path)
function_tokens: def main ( base_dir , adb_cmd = 'adb' ) : # We create the directory to ensure 'run once' semantics if os . path . exists ( base_dir ) : print ( 'adb_logcat_monitor: %s already exists? Cleaning' % base_dir ) shutil . rmtree ( base_dir , ignore_errors = True ) os . makedirs ( base_dir ) logging . basicConfig ( filename = os . path . join ( base_dir , 'eventlog' ) , level = logging . INFO , format = '%(asctime)-2s %(levelname)-8s %(message)s' ) # Set up the alarm for calling 'adb devices'. This is to ensure # our script doesn't get stuck waiting for a process response def TimeoutHandler ( _signum , _unused_frame ) : raise TimeoutException ( ) signal . signal ( signal . SIGALRM , TimeoutHandler ) # Handle SIGTERMs to ensure clean shutdown def SigtermHandler ( _signum , _unused_frame ) : raise SigtermError ( ) signal . signal ( signal . SIGTERM , SigtermHandler ) logging . info ( 'Started with pid %d' , os . getpid ( ) ) pid_file_path = os . path . join ( base_dir , 'LOGCAT_MONITOR_PID' ) try : with open ( pid_file_path , 'w' ) as f : f . write ( str ( os . getpid ( ) ) ) while True : for device_id in GetAttachedDevices ( adb_cmd ) : if not device_id in devices : subprocess . call ( [ adb_cmd , '-s' , device_id , 'logcat' , '-c' ] ) devices [ device_id ] = ( None , 0 ) for device in devices : # This will spawn logcat watchers for any device ever detected StartLogcatIfNecessary ( device , adb_cmd , base_dir ) time . sleep ( 5 ) except SigtermError : logging . info ( 'Received SIGTERM, shutting down' ) except : # pylint: disable=bare-except logging . exception ( 'Unexpected exception in main.' ) finally : for process , _ in devices . values ( ) : if process : try : process . terminate ( ) except OSError : pass os . remove ( pid_file_path )