1# Copyright 2014 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5"""Print prettier and more detailed exceptions.""" 6 7import logging 8import math 9import os 10import sys 11import traceback 12 13from telemetry.core import exceptions 14 15 16def PrintFormattedException(exception_class=None, exception=None, tb=None, 17 msg=None): 18 logging.info('Try printing formatted exception: %s %s %s' % 19 (exception_class, exception, tb)) 20 assert bool(exception_class) == bool(exception) == bool(tb), ( 21 'Must specify all or none of exception_class, exception, and tb') 22 23 if not exception_class: 24 exception_class, exception, tb = sys.exc_info() 25 26 if exception_class == exceptions.IntentionalException: 27 return 28 29 def _GetFinalFrame(tb_level): 30 while tb_level.tb_next: 31 tb_level = tb_level.tb_next 32 return tb_level.tb_frame 33 34 processed_tb = traceback.extract_tb(tb) 35 frame = _GetFinalFrame(tb) 36 exception_list = traceback.format_exception_only(exception_class, exception) 37 exception_string = '\n'.join(l.strip() for l in exception_list) 38 39 if msg: 40 print >> sys.stderr 41 print >> sys.stderr, msg 42 43 _PrintFormattedTrace(processed_tb, frame, exception_string) 44 45 46def PrintFormattedFrame(frame, exception_string=None): 47 _PrintFormattedTrace(traceback.extract_stack(frame), frame, exception_string) 48 49 50def _PrintFormattedTrace(processed_tb, frame, exception_string=None): 51 """Prints an Exception in a more useful format than the default. 52 53 TODO(tonyg): Consider further enhancements. For instance: 54 - Report stacks to maintainers like depot_tools does. 55 - Add a debug flag to automatically start pdb upon exception. 56 """ 57 print >> sys.stderr 58 59 # Format the traceback. 60 print >> sys.stderr, 'Traceback (most recent call last):' 61 for filename, line, function, text in processed_tb: 62 filename = os.path.abspath(filename) 63 print >> sys.stderr, ' %s at %s:%d' % (function, filename, line) 64 print >> sys.stderr, ' %s' % text 65 66 # Format the exception. 67 if exception_string: 68 print >> sys.stderr, exception_string 69 70 # Format the locals. 71 local_variables = [(variable, value) for variable, value in 72 frame.f_locals.iteritems() if variable != 'self'] 73 print >> sys.stderr 74 print >> sys.stderr, 'Locals:' 75 if local_variables: 76 longest_variable = max(len(v) for v, _ in local_variables) 77 for variable, value in sorted(local_variables): 78 value = repr(value) 79 possibly_truncated_value = _AbbreviateMiddleOfString(value, ' ... ', 1024) 80 truncation_indication = '' 81 if len(possibly_truncated_value) != len(value): 82 truncation_indication = ' (truncated)' 83 print >> sys.stderr, ' %s: %s%s' % (variable.ljust(longest_variable + 1), 84 possibly_truncated_value, 85 truncation_indication) 86 else: 87 print >> sys.stderr, ' No locals!' 88 89 print >> sys.stderr 90 sys.stderr.flush() 91 92 93def _AbbreviateMiddleOfString(target, middle, max_length): 94 if max_length < 0: 95 raise ValueError('Must provide positive max_length') 96 if len(middle) > max_length: 97 raise ValueError('middle must not be greater than max_length') 98 99 if len(target) <= max_length: 100 return target 101 half_length = (max_length - len(middle)) / 2. 102 return (target[:int(math.floor(half_length))] + middle + 103 target[-int(math.ceil(half_length)):]) 104