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