def test_print_list_default(self): extracted = stacktrace.extract_stack(get_frame()) original_stderr = sys.__stderr__ stderr = sys.stderr = StringIO.StringIO() try: stacktrace.print_list(extracted) finally: sys.stderr = original_stderr self.assertEndsWith(stderr.getvalue(), 'return sys._getframe()\n')
def connection_raw_execute(self, connection, raw_cursor, statement, params): statement_to_log = statement if params: statement_to_log = raw_cursor.mogrify( statement, tuple(connection.to_database(params))) if isinstance(statement_to_log, bytes): statement_to_log = statement_to_log.decode('UTF-8', errors='replace') # Record traceback to log, if requested. print_traceback = self._debug_sql_extra log_sql = getattr(_local, 'sql_logging', None) log_traceback = False if log_sql is not None: log_sql.append(dict(stack=None, sql=None, exception=None)) conditional = getattr(_local, 'sql_logging_tracebacks_if', None) if callable(conditional): try: log_traceback = conditional( self._normalize_whitespace( statement_to_log.strip()).upper()) except (MemoryError, SystemExit, KeyboardInterrupt): raise except: exc_type, exc_value, tb = sys.exc_info() log_sql[-1]['exception'] = (exc_type, exc_value) log_sql[-1]['stack'] = extract_tb(tb) else: log_traceback = bool(conditional) if print_traceback or log_traceback: stack = extract_stack() if log_traceback: log_sql[-1]['stack'] = stack if print_traceback: print_list(stack) sys.stderr.write("." * 70 + "\n") # store the last executed statement as an attribute on the current # thread threading.currentThread().lp_last_sql_statement = statement request_starttime = getattr(_local, 'request_start_time', None) if request_starttime is None: if print_traceback or self._debug_sql or log_sql is not None: # Stash some information for logging at the end of the # SQL execution. connection._lp_statement_info = (int( time() * 1000), u'SQL-%s' % connection._database.name, statement_to_log) return action = get_request_timeline(get_current_browser_request()).start( u'SQL-%s' % connection._database.name, statement_to_log) connection._lp_statement_action = action
def connection_raw_execute(self, connection, raw_cursor, statement, params): statement_to_log = statement if params: statement_to_log = raw_cursor.mogrify( statement, tuple(connection.to_database(params))) # Record traceback to log, if requested. print_traceback = self._debug_sql_extra log_sql = getattr(_local, 'sql_logging', None) log_traceback = False if log_sql is not None: log_sql.append(dict(stack=None, sql=None, exception=None)) conditional = getattr(_local, 'sql_logging_tracebacks_if', None) if callable(conditional): try: log_traceback = conditional( self._normalize_whitespace( statement_to_log.strip()).upper()) except (MemoryError, SystemExit, KeyboardInterrupt): raise except: exc_type, exc_value, tb = sys.exc_info() log_sql[-1]['exception'] = (exc_type, exc_value) log_sql[-1]['stack'] = extract_tb(tb) else: log_traceback = bool(conditional) if print_traceback or log_traceback: stack = extract_stack() if log_traceback: log_sql[-1]['stack'] = stack if print_traceback: print_list(stack) sys.stderr.write("." * 70 + "\n") # store the last executed statement as an attribute on the current # thread threading.currentThread().lp_last_sql_statement = statement request_starttime = getattr(_local, 'request_start_time', None) if request_starttime is None: if print_traceback or self._debug_sql or log_sql is not None: # Stash some information for logging at the end of the # SQL execution. connection._lp_statement_info = ( int(time() * 1000), 'SQL-%s' % connection._database.name, statement_to_log) return action = get_request_timeline(get_current_browser_request()).start( 'SQL-%s' % connection._database.name, statement_to_log) connection._lp_statement_action = action
def print_queries(queries, file=None): if file is None: file = sys.stdout for query in queries: # Note: this could use the sql tb if it exists. stack = query['stack'] if stack is not None: exception = query['exception'] if exception is not None: file.write('Error when determining whether to generate a ' 'stacktrace.\n') file.write('Traceback (most recent call last):\n') print_list(stack, file) if exception is not None: lines = traceback.format_exception_only(*exception) file.write(' '.join(lines)) file.write("." * 70 + "\n") sql = query['sql'] if sql is not None: file.write('%d-%d@%s %s\n' % sql[:4]) else: file.write('(no SQL recorded)\n') file.write("-" * 70 + "\n")
def print_queries(queries, file=None): if file is None: file = sys.stdout for query in queries: # Note: this could use the sql tb if it exists. stack = query['stack'] if stack is not None: exception = query['exception'] if exception is not None: file.write( 'Error when determining whether to generate a ' 'stacktrace.\n') file.write('Traceback (most recent call last):\n') print_list(stack, file) if exception is not None: lines = traceback.format_exception_only(*exception) file.write(' '.join(lines)) file.write("." * 70 + "\n") sql = query['sql'] if sql is not None: file.write('%d-%d@%s %s\n' % sql[:4]) else: file.write('(no SQL recorded)\n') file.write("-" * 70 + "\n")
def test_print_list_file(self): extracted = stacktrace.extract_stack(get_frame()) f = StringIO.StringIO() stacktrace.print_list(extracted, file=f) self.assertEndsWith(f.getvalue(), 'return sys._getframe()\n')