OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Saves logcats from all connected devices. | 7 """Saves logcats from all connected devices. |
8 | 8 |
9 Usage: adb_logcat_monitor.py <base_dir> [<adb_binary_path>] | 9 Usage: adb_logcat_monitor.py <base_dir> [<adb_binary_path>] |
10 | 10 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 return [] | 89 return [] |
90 except (IOError, OSError): | 90 except (IOError, OSError): |
91 logging.exception('Exception from "adb devices"') | 91 logging.exception('Exception from "adb devices"') |
92 return [] | 92 return [] |
93 finally: | 93 finally: |
94 signal.alarm(0) | 94 signal.alarm(0) |
95 | 95 |
96 | 96 |
97 def main(base_dir, adb_cmd='adb'): | 97 def main(base_dir, adb_cmd='adb'): |
98 """Monitor adb forever. Expects a SIGINT (Ctrl-C) to kill.""" | 98 """Monitor adb forever. Expects a SIGINT (Ctrl-C) to kill.""" |
| 99 # Spawn a detached child process. |
| 100 pid = os.fork() |
| 101 if pid > 0: |
| 102 os._exit(os.EX_OK) |
| 103 elif pid < 0: |
| 104 sys.exit('Unable to spawn a detached child process.') |
| 105 os.setsid() |
| 106 # The rest happens in the child process. |
| 107 |
99 # We create the directory to ensure 'run once' semantics | 108 # We create the directory to ensure 'run once' semantics |
100 if os.path.exists(base_dir): | 109 if os.path.exists(base_dir): |
101 print 'adb_logcat_monitor: %s already exists? Cleaning' % base_dir | 110 print 'adb_logcat_monitor: %s already exists? Cleaning' % base_dir |
102 shutil.rmtree(base_dir, ignore_errors=True) | 111 shutil.rmtree(base_dir, ignore_errors=True) |
103 | 112 |
104 os.makedirs(base_dir) | 113 os.makedirs(base_dir) |
105 logging.basicConfig(filename=os.path.join(base_dir, 'eventlog'), | 114 logging.basicConfig(filename=os.path.join(base_dir, 'eventlog'), |
106 level=logging.INFO, | 115 level=logging.INFO, |
107 format='%(asctime)-2s %(levelname)-8s %(message)s') | 116 format='%(asctime)-2s %(levelname)-8s %(message)s') |
108 | 117 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 pass | 156 pass |
148 os.remove(pid_file_path) | 157 os.remove(pid_file_path) |
149 | 158 |
150 | 159 |
151 if __name__ == '__main__': | 160 if __name__ == '__main__': |
152 if 2 <= len(sys.argv) <= 3: | 161 if 2 <= len(sys.argv) <= 3: |
153 print 'adb_logcat_monitor: Initializing' | 162 print 'adb_logcat_monitor: Initializing' |
154 sys.exit(main(*sys.argv[1:3])) | 163 sys.exit(main(*sys.argv[1:3])) |
155 | 164 |
156 print 'Usage: %s <base_dir> [<adb_binary_path>]' % sys.argv[0] | 165 print 'Usage: %s <base_dir> [<adb_binary_path>]' % sys.argv[0] |
OLD | NEW |