Alex Light | 6717e5f | 2018-03-12 11:54:48 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright 2018, The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import argparse |
| 18 | import subprocess |
| 19 | import sys |
| 20 | import shlex |
| 21 | |
| 22 | def main(): |
| 23 | parser = argparse.ArgumentParser( |
| 24 | description='Runs a command while collecting logcat in the background') |
| 25 | parser.add_argument('--output', '-o', |
| 26 | type=lambda f: open(f,'w'), |
| 27 | required=True, |
| 28 | action='store', |
| 29 | help='File to store the logcat to. Will be created if not already existing') |
| 30 | parser.add_argument('--logcat-invoke', |
| 31 | action='store', |
| 32 | default='adb logcat', |
| 33 | help="""Command to run to retrieve logcat data. Defaults to 'adb logcat'. |
| 34 | It will be run in the background and killed by SIGTERM when the 'command' |
| 35 | finishes.""") |
| 36 | parser.add_argument('command', |
| 37 | action='store', |
| 38 | help='The command to run with logcat in the background.') |
| 39 | args = parser.parse_args() |
| 40 | # Send all output from logcat to the file. |
| 41 | with subprocess.Popen(shlex.split(args.logcat_invoke), |
| 42 | stdout=args.output, |
| 43 | stderr=subprocess.STDOUT, |
| 44 | shell=False, |
| 45 | universal_newlines=True) as logcat_proc: |
| 46 | # Let the run-test-proc inherit our stdout FDs |
| 47 | with subprocess.Popen(shlex.split(args.command), |
| 48 | stdout=None, |
| 49 | stderr=None, |
| 50 | shell=False) as run_test_proc: |
| 51 | # Don't actually do anything. Just let the run-test proc finish. |
| 52 | pass |
| 53 | # Send SIGTERM to the logcat process. |
| 54 | logcat_proc.kill() |
| 55 | return 0 |
| 56 | |
| 57 | if __name__ == '__main__': |
| 58 | sys.exit(main()) |
| 59 | |