diff options
-rwxr-xr-x | tools/ffdb.py | 90 |
1 files changed, 69 insertions, 21 deletions
diff --git a/tools/ffdb.py b/tools/ffdb.py index 143fea8e..a948c700 100755 --- a/tools/ffdb.py +++ b/tools/ffdb.py @@ -2,6 +2,12 @@ import socket, json, sys, uuid, datetime, time, logging, cgi, zipfile, os, tempfile, atexit, subprocess, re, base64, struct, imghdr +WINDOWS = sys.platform == 'win32' +if WINDOWS: + import ctypes + stdout_handle = ctypes.windll.kernel32.GetStdHandle(-11) + +LOG_FORMAT = 'short' # Either 'bare', 'short', or 'long' ADB = 'adb' # Path to the adb executable LOG_VERBOSE = False # Verbose printing enabled with --verbose HOST = 'localhost' # The remote host to connect to the B2G device @@ -63,21 +69,27 @@ def logv(msg): # Returns a JSON dictionary of the received message. def read_b2g_response(print_errors_to_console = True): global read_queue, b2g_socket - try: - if len(read_queue) == 0: - read_queue += b2g_socket.recv(65536*2) - except Exception, e: - if e[0] == 57: # Socket is not connected - print 'Error! Failed to receive data from the device: socket is not connected!' - sys.exit(1) - else: - raise payload = '' - while ':' in read_queue: - semicolon = read_queue.index(':') - payload_len = int(read_queue[:semicolon]) + while True: + semicolon = float('Inf') + payload_len = float('Inf') + try: + semicolon = read_queue.index(':') + payload_len = int(read_queue[:semicolon]) + except: + pass if semicolon+1+payload_len > len(read_queue): - read_queue += b2g_socket.recv(65536*2) + try: + read_queue += b2g_socket.recv(4096) + except socket.timeout, e: + pass # We simulate blocking sockets with looping over reads that time out, since on Windows, the user cannot press Ctrl-C to break on blocking sockets. + except Exception, e: + if e[0] == 57: # Socket is not connected + print 'Error! Failed to receive data from the device: socket is not connected!' + sys.exit(1) + else: + print 'Got exception ' + str(e) + raise continue payload = read_queue[semicolon+1:semicolon+1+payload_len] read_queue = read_queue[semicolon+1+payload_len:] @@ -313,6 +325,7 @@ def b2g_memory(app_name): print k + ': ' + str(v) def b2g_log(app_name, clear=False): + global LOG_FORMAT apps = b2g_get_appslist() appActor = '' for app in apps: @@ -329,11 +342,30 @@ def b2g_log(app_name, clear=False): msgs = send_b2g_cmd(consoleActor, 'startListeners', { 'listeners': ['PageError','ConsoleAPI','NetworkActivity','FileActivity'] }) - def log_b2g_message(msg): + if WINDOWS: + WARNING = 14 # FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY + FAIL = 12 # FOREGROUND_RED | FOREGROUND_INTENSITY + INFO = 7 # FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE + ENDC = '' + BOLD = '' + else: WARNING = '\033[93m' FAIL = '\033[91m' - ENDC = '\033[0m' + INFO = ENDC = '\033[0m' BOLD = "\033[1m" + + def set_color(string, color): + if WINDOWS: + ctypes.windll.kernel32.SetConsoleTextAttribute(stdout_handle, color) + return string + else: + return color + string + ENDC + + def reset_color(): + if WINDOWS: + ctypes.windll.kernel32.SetConsoleTextAttribute(stdout_handle, INFO) + + def log_b2g_message(msg): msgs = [] if 'type' in msg and msg['type'] == 'consoleAPICall': msgs = [msg['message']] @@ -344,16 +376,29 @@ def b2g_log(app_name, clear=False): args = m['arguments'] for arg in args: + if LOG_FORMAT == 'long': + text = str(m['functionName']) + '@' + str(m['filename']) + ':' + str(m['lineNumber']) + ': ' + str(arg) + elif LOG_FORMAT == 'bare': + text = str(arg) + else: # Default to 'short' + text = str(m['functionName']) + '@' + os.path.basename(str(m['filename'])) + ':' + str(m['lineNumber']) + ': ' + str(arg) + if m['level'] == 'log': - color = 'I/' + color = INFO + channel = 'I/' elif m['level'] == 'warn': - color = WARNING + 'W/' + color = WARNING + channel = 'W/' elif m['level'] == 'error': - color = FAIL + 'E/' + color = FAIL + channel = 'E/' else: - color = m['level'] + '/' + color = INFO + channel = m['level'] + '/' - print color + str(m['functionName']) + '@' + str(m['filename']) + ':' + str(m['lineNumber']) + ': ' + str(arg) + ENDC + text = set_color(channel + text, color) + print text + reset_color() msgs = send_b2g_cmd(consoleActor, 'getCachedMessages', { 'messageTypes': ['PageError', 'ConsoleAPI'] }) log_b2g_message(msgs) @@ -511,6 +556,9 @@ def main(): print ' 4) When launching ffdb, remember to acknowledge the "incoming debug connection" dialog if it pops up on the device.' sys.exit(1) b2g_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + if WINDOWS: + # Python Windows issue: user cannot press Ctrl-C to abort from a socket .recv() Therefore simulate blocking sockets with looping over reads that time out. + b2g_socket.settimeout(0.5) try: b2g_socket.connect((HOST, PORT)) except Exception, e: @@ -662,5 +710,5 @@ if __name__ == '__main__': logv('ffdb.py quitting with process exit code ' + str(returncode)) sys.exit(returncode) except KeyboardInterrupt: - print ' Aborted by user' + print ('^C' if WINDOWS else '') + ' Aborted by user' sys.exit(1) |