Console.java revision f7bca430d9356c26d6df222d2c90bc7668262f6b
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.systemui.recents; 18 19 20import android.content.ComponentCallbacks2; 21import android.content.Context; 22import android.util.Log; 23import android.view.MotionEvent; 24import android.widget.Toast; 25 26import java.util.HashMap; 27import java.util.Map; 28 29 30public class Console { 31 // Timer 32 public static final Map<Object, Long> mTimeLogs = new HashMap<Object, Long>(); 33 34 // Colors 35 public static final String AnsiReset = "\u001B[0m"; 36 public static final String AnsiBlack = "\u001B[30m"; 37 public static final String AnsiRed = "\u001B[31m"; // SystemUIHandshake 38 public static final String AnsiGreen = "\u001B[32m"; // MeasureAndLayout 39 public static final String AnsiYellow = "\u001B[33m"; // SynchronizeViewsWithModel 40 public static final String AnsiBlue = "\u001B[34m"; // TouchEvents, Search 41 public static final String AnsiPurple = "\u001B[35m"; // Draw 42 public static final String AnsiCyan = "\u001B[36m"; // ClickEvents 43 public static final String AnsiWhite = "\u001B[37m"; 44 45 /** Logs a key */ 46 public static void log(String key) { 47 log(true, key, "", AnsiReset); 48 } 49 50 /** Logs a conditioned key */ 51 public static void log(boolean condition, String key) { 52 if (condition) { 53 log(condition, key, "", AnsiReset); 54 } 55 } 56 57 /** Logs a key in a specific color */ 58 public static void log(boolean condition, String key, Object data) { 59 if (condition) { 60 log(condition, key, data, AnsiReset); 61 } 62 } 63 64 /** Logs a key with data in a specific color */ 65 public static void log(boolean condition, String key, Object data, String color) { 66 if (condition) { 67 System.out.println(color + key + AnsiReset + " " + data.toString()); 68 } 69 } 70 71 /** Logs an error */ 72 public static void logError(Context context, String msg) { 73 Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); 74 Log.e("Recents", msg); 75 } 76 77 /** Logs a raw error */ 78 public static void logRawError(String msg, Exception e) { 79 Log.e("Recents", msg, e); 80 } 81 82 /** Logs a divider bar */ 83 public static void logDivider(boolean condition) { 84 if (condition) { 85 System.out.println("==== [" + System.currentTimeMillis() + 86 "] ============================================================"); 87 } 88 } 89 90 /** Starts a time trace */ 91 public static void logStartTracingTime(boolean condition, String key) { 92 if (condition) { 93 long curTime = System.currentTimeMillis(); 94 mTimeLogs.put(key, curTime); 95 Console.log(condition, "[Recents|" + key + "]", 96 "started @ " + curTime); 97 } 98 } 99 100 /** Continues a time trace */ 101 public static void logTraceTime(boolean condition, String key, String desc) { 102 if (condition) { 103 long timeDiff = System.currentTimeMillis() - mTimeLogs.get(key); 104 Console.log(condition, "[Recents|" + key + "|" + desc + "]", 105 "+" + timeDiff + "ms"); 106 } 107 } 108 109 /** Logs a stack trace */ 110 public static void logStackTrace() { 111 logStackTrace("", 99); 112 } 113 114 /** Logs a stack trace to a certain depth */ 115 public static void logStackTrace(int depth) { 116 logStackTrace("", depth); 117 } 118 119 /** Logs a stack trace to a certain depth with a key */ 120 public static void logStackTrace(String key, int depth) { 121 int offset = 0; 122 StackTraceElement[] callStack = Thread.currentThread().getStackTrace(); 123 String tinyStackTrace = ""; 124 // Skip over the known stack trace classes 125 for (int i = 0; i < callStack.length; i++) { 126 StackTraceElement el = callStack[i]; 127 String className = el.getClassName(); 128 if (className.indexOf("dalvik.system.VMStack") == -1 && 129 className.indexOf("java.lang.Thread") == -1 && 130 className.indexOf("recents.Console") == -1) { 131 break; 132 } else { 133 offset++; 134 } 135 } 136 // Build the pretty stack trace 137 int start = Math.min(offset + depth, callStack.length); 138 int end = offset; 139 String indent = ""; 140 for (int i = start - 1; i >= end; i--) { 141 StackTraceElement el = callStack[i]; 142 tinyStackTrace += indent + " -> " + el.getClassName() + 143 "[" + el.getLineNumber() + "]." + el.getMethodName(); 144 if (i > end) { 145 tinyStackTrace += "\n"; 146 indent += " "; 147 } 148 } 149 log(true, key, tinyStackTrace, AnsiRed); 150 } 151 152 153 /** Returns the stringified MotionEvent action */ 154 public static String motionEventActionToString(int action) { 155 switch (action) { 156 case MotionEvent.ACTION_DOWN: 157 return "Down"; 158 case MotionEvent.ACTION_UP: 159 return "Up"; 160 case MotionEvent.ACTION_MOVE: 161 return "Move"; 162 case MotionEvent.ACTION_CANCEL: 163 return "Cancel"; 164 case MotionEvent.ACTION_POINTER_DOWN: 165 return "Pointer Down"; 166 case MotionEvent.ACTION_POINTER_UP: 167 return "Pointer Up"; 168 default: 169 return "" + action; 170 } 171 } 172 173 public static String trimMemoryLevelToString(int level) { 174 switch (level) { 175 case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN: 176 return "UI Hidden"; 177 case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE: 178 return "Running Moderate"; 179 case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND: 180 return "Background"; 181 case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW: 182 return "Running Low"; 183 case ComponentCallbacks2.TRIM_MEMORY_MODERATE: 184 return "Moderate"; 185 case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL: 186 return "Critical"; 187 case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: 188 return "Complete"; 189 default: 190 return "" + level; 191 } 192 } 193} 194