| 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 """Shutdown adb_logcat_monitor and print accumulated logs. | 7 """Shutdown adb_logcat_monitor and print accumulated logs. |
| 8 | 8 |
| 9 To test, call './adb_logcat_printer.py <base_dir>' where | 9 To test, call './adb_logcat_printer.py <base_dir>' where |
| 10 <base_dir> contains 'adb logcat -v threadtime' files named as | 10 <base_dir> contains 'adb logcat -v threadtime' files named as |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 152 |
| 153 | 153 |
| 154 def main(argv): | 154 def main(argv): |
| 155 parser = optparse.OptionParser(usage='Usage: %prog [options] <log dir>') | 155 parser = optparse.OptionParser(usage='Usage: %prog [options] <log dir>') |
| 156 parser.add_option('--output-path', | 156 parser.add_option('--output-path', |
| 157 help='Output file path (if unspecified, prints to stdout)') | 157 help='Output file path (if unspecified, prints to stdout)') |
| 158 options, args = parser.parse_args(argv) | 158 options, args = parser.parse_args(argv) |
| 159 if len(args) != 1: | 159 if len(args) != 1: |
| 160 parser.error('Wrong number of unparsed args') | 160 parser.error('Wrong number of unparsed args') |
| 161 base_dir = args[0] | 161 base_dir = args[0] |
| 162 if options.output_path: | |
| 163 output_file = open(options.output_path, 'w') | |
| 164 else: | |
| 165 output_file = sys.stdout | |
| 166 | 162 |
| 167 log_stringio = cStringIO.StringIO() | 163 log_stringio = cStringIO.StringIO() |
| 168 logger = logging.getLogger('LogcatPrinter') | 164 logger = logging.getLogger('LogcatPrinter') |
| 169 logger.setLevel(LOG_LEVEL) | 165 logger.setLevel(LOG_LEVEL) |
| 170 sh = logging.StreamHandler(log_stringio) | 166 sh = logging.StreamHandler(log_stringio) |
| 171 sh.setFormatter(logging.Formatter('%(asctime)-2s %(levelname)-8s' | 167 sh.setFormatter(logging.Formatter('%(asctime)-2s %(levelname)-8s' |
| 172 ' %(message)s')) | 168 ' %(message)s')) |
| 173 logger.addHandler(sh) | 169 logger.addHandler(sh) |
| 174 | 170 |
| 171 if options.output_path: |
| 172 if not os.path.exists(os.path.dirname(options.output_path)): |
| 173 logger.warning('Output dir %s doesn\'t exist. Creating it.', |
| 174 os.path.dirname(options.output_path)) |
| 175 os.makedirs(os.path.dirname(options.output_path)) |
| 176 output_file = open(options.output_path, 'w') |
| 177 logger.info('Dumping logcat to local file %s. If running in a build, ' |
| 178 'this file will likely will be uploaded to google storage ' |
| 179 'in a later step. It can be downloaded from there.', |
| 180 options.output_path) |
| 181 else: |
| 182 output_file = sys.stdout |
| 183 |
| 175 try: | 184 try: |
| 176 # Wait at least 5 seconds after base_dir is created before printing. | 185 # Wait at least 5 seconds after base_dir is created before printing. |
| 177 # | 186 # |
| 178 # The idea is that 'adb logcat > file' output consists of 2 phases: | 187 # The idea is that 'adb logcat > file' output consists of 2 phases: |
| 179 # 1 Dump all the saved logs to the file | 188 # 1 Dump all the saved logs to the file |
| 180 # 2 Stream log messages as they are generated | 189 # 2 Stream log messages as they are generated |
| 181 # | 190 # |
| 182 # We want to give enough time for phase 1 to complete. There's no | 191 # We want to give enough time for phase 1 to complete. There's no |
| 183 # good method to tell how long to wait, but it usually only takes a | 192 # good method to tell how long to wait, but it usually only takes a |
| 184 # second. On most bots, this code path won't occur at all, since | 193 # second. On most bots, this code path won't occur at all, since |
| (...skipping 19 matching lines...) Expand all Loading... |
| 204 except: | 213 except: |
| 205 logger.exception('Unexpected exception') | 214 logger.exception('Unexpected exception') |
| 206 | 215 |
| 207 logger.info('Done.') | 216 logger.info('Done.') |
| 208 sh.flush() | 217 sh.flush() |
| 209 output_file.write('\nLogcat Printer Event Log\n') | 218 output_file.write('\nLogcat Printer Event Log\n') |
| 210 output_file.write(log_stringio.getvalue()) | 219 output_file.write(log_stringio.getvalue()) |
| 211 | 220 |
| 212 if __name__ == '__main__': | 221 if __name__ == '__main__': |
| 213 sys.exit(main(sys.argv[1:])) | 222 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |