|
| 1 | +package app.revanced.extension.shared; |
| 2 | + |
| 3 | +import static app.revanced.extension.shared.settings.BaseSettings.DEBUG; |
| 4 | +import static app.revanced.extension.shared.settings.BaseSettings.DEBUG_STACKTRACE; |
| 5 | +import static app.revanced.extension.shared.settings.BaseSettings.DEBUG_TOAST_ON_ERROR; |
| 6 | + |
| 7 | +import android.util.Log; |
| 8 | + |
| 9 | +import androidx.annotation.NonNull; |
| 10 | +import androidx.annotation.Nullable; |
| 11 | + |
| 12 | +import java.io.PrintWriter; |
| 13 | +import java.io.StringWriter; |
| 14 | + |
| 15 | +/** |
| 16 | + * ReVanced specific logger. Logging is done to standard device log (accessible thru ADB), |
| 17 | + * and additionally accessible thru {@link LogBufferManager}. |
| 18 | + * |
| 19 | + * All methods are thread safe, and are safe to call even |
| 20 | + * if {@link Utils#getContext()} is not available. |
| 21 | + */ |
| 22 | +public class Logger { |
| 23 | + |
| 24 | + /** |
| 25 | + * Log messages using lambdas. |
| 26 | + */ |
| 27 | + @FunctionalInterface |
| 28 | + public interface LogMessage { |
| 29 | + /** |
| 30 | + * @return Logger string message. This method is only called if logging is enabled. |
| 31 | + */ |
| 32 | + @NonNull |
| 33 | + String buildMessageString(); |
| 34 | + } |
| 35 | + |
| 36 | + private enum LogLevel { |
| 37 | + DEBUG, |
| 38 | + INFO, |
| 39 | + ERROR |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Log tag prefix. Only used for system logging. |
| 44 | + */ |
| 45 | + private static final String REVANCED_LOG_TAG_PREFIX = "revanced: "; |
| 46 | + |
| 47 | + private static final String LOGGER_CLASS_NAME = Logger.class.getName(); |
| 48 | + |
| 49 | + /** |
| 50 | + * @return For outer classes, this returns {@link Class#getSimpleName()}. |
| 51 | + * For static, inner, or anonymous classes, this returns the simple name of the enclosing class. |
| 52 | + * <br> |
| 53 | + * For example, each of these classes returns 'SomethingView': |
| 54 | + * <code> |
| 55 | + * com.company.SomethingView |
| 56 | + * com.company.SomethingView$StaticClass |
| 57 | + * com.company.SomethingView$1 |
| 58 | + * </code> |
| 59 | + */ |
| 60 | + private static String getOuterClassSimpleName(Object obj) { |
| 61 | + Class<?> logClass = obj.getClass(); |
| 62 | + String fullClassName = logClass.getName(); |
| 63 | + final int dollarSignIndex = fullClassName.indexOf('$'); |
| 64 | + if (dollarSignIndex < 0) { |
| 65 | + return logClass.getSimpleName(); // Already an outer class. |
| 66 | + } |
| 67 | + |
| 68 | + // Class is inner, static, or anonymous. |
| 69 | + // Parse the simple name full name. |
| 70 | + // A class with no package returns index of -1, but incrementing gives index zero which is correct. |
| 71 | + final int simpleClassNameStartIndex = fullClassName.lastIndexOf('.') + 1; |
| 72 | + return fullClassName.substring(simpleClassNameStartIndex, dollarSignIndex); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Internal method to handle logging to Android Log and {@link LogBufferManager}. |
| 77 | + * Appends the log message, stack trace (if enabled), and exception (if present) to logBuffer |
| 78 | + * with class name but without 'revanced:' prefix. |
| 79 | + * |
| 80 | + * @param logLevel The log level. |
| 81 | + * @param message Log message object. |
| 82 | + * @param ex Optional exception. |
| 83 | + * @param includeStackTrace If the current stack should be included. |
| 84 | + * @param showToast If a toast is to be shown. |
| 85 | + */ |
| 86 | + private static void logInternal(LogLevel logLevel, LogMessage message, @Nullable Throwable ex, |
| 87 | + boolean includeStackTrace, boolean showToast) { |
| 88 | + // It's very important that no Settings are used in this method, |
| 89 | + // as this code is used when a context is not set and thus referencing |
| 90 | + // a setting will crash the app. |
| 91 | + String messageString = message.buildMessageString(); |
| 92 | + |
| 93 | + String logText = messageString; |
| 94 | + |
| 95 | + // Append exception message if present. |
| 96 | + if (ex != null) { |
| 97 | + var exceptionMessage = ex.getMessage(); |
| 98 | + if (exceptionMessage != null) { |
| 99 | + logText += "\nException: " + exceptionMessage; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (includeStackTrace) { |
| 104 | + var sw = new StringWriter(); |
| 105 | + new Throwable().printStackTrace(new PrintWriter(sw)); |
| 106 | + String stackTrace = sw.toString(); |
| 107 | + // Remove the stacktrace elements of this class. |
| 108 | + final int loggerIndex = stackTrace.lastIndexOf(LOGGER_CLASS_NAME); |
| 109 | + final int loggerBegins = stackTrace.indexOf('\n', loggerIndex); |
| 110 | + logText += stackTrace.substring(loggerBegins); |
| 111 | + } |
| 112 | + |
| 113 | + // Do not include "revanced:" prefix in clipboard logs. |
| 114 | + String managerToastString = logText; |
| 115 | + |
| 116 | + String logTag = "revanced"; |
| 117 | + switch (logLevel) { |
| 118 | + case DEBUG: |
| 119 | + if (ex == null) Log.d(logTag, logText); |
| 120 | + else Log.d(logTag, logText, ex); |
| 121 | + break; |
| 122 | + case INFO: |
| 123 | + if (ex == null) Log.i(logTag, logText); |
| 124 | + else Log.i(logTag, logText, ex); |
| 125 | + break; |
| 126 | + case ERROR: |
| 127 | + if (ex == null) Log.e(logTag, logText); |
| 128 | + else Log.e(logTag, logText, ex); |
| 129 | + break; |
| 130 | + } |
| 131 | + |
| 132 | + if (showToast) { |
| 133 | + Utils.showToastLong(managerToastString); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static boolean shouldLogDebug() { |
| 138 | + // If the app is still starting up and the context is not yet set, |
| 139 | + // then allow debug logging regardless what the debug setting actually is. |
| 140 | + return Utils.context == null || DEBUG.get(); |
| 141 | + } |
| 142 | + |
| 143 | + private static boolean shouldShowErrorToast() { |
| 144 | + return Utils.context != null && DEBUG_TOAST_ON_ERROR.get(); |
| 145 | + } |
| 146 | + |
| 147 | + private static boolean includeStackTrace() { |
| 148 | + return Utils.context != null && DEBUG_STACKTRACE.get(); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Logs debug messages under the outer class name of the code calling this method. |
| 153 | + * <p> |
| 154 | + * Whenever possible, the log string should be constructed entirely inside |
| 155 | + * {@link LogMessage#buildMessageString()} so the performance cost of |
| 156 | + * building strings is paid only if {@link BaseSettings#DEBUG} is enabled. |
| 157 | + */ |
| 158 | + public static void printDebug(LogMessage message) { |
| 159 | + printDebug(message, null); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Logs debug messages under the outer class name of the code calling this method. |
| 164 | + * <p> |
| 165 | + * Whenever possible, the log string should be constructed entirely inside |
| 166 | + * {@link LogMessage#buildMessageString()} so the performance cost of |
| 167 | + * building strings is paid only if {@link BaseSettings#DEBUG} is enabled. |
| 168 | + */ |
| 169 | + public static void printDebug(LogMessage message, @Nullable Exception ex) { |
| 170 | + if (shouldLogDebug()) { |
| 171 | + logInternal(LogLevel.DEBUG, message, ex, includeStackTrace(), false); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Logs information messages using the outer class name of the code calling this method. |
| 177 | + */ |
| 178 | + public static void printInfo(LogMessage message) { |
| 179 | + printInfo(message, null); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Logs information messages using the outer class name of the code calling this method. |
| 184 | + */ |
| 185 | + public static void printInfo(LogMessage message, @Nullable Exception ex) { |
| 186 | + logInternal(LogLevel.INFO, message, ex, includeStackTrace(), false); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Logs exceptions under the outer class name of the code calling this method. |
| 191 | + * Appends the log message, exception (if present), and toast message (if enabled) to logBuffer. |
| 192 | + */ |
| 193 | + public static void printException(LogMessage message) { |
| 194 | + printException(message, null); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Logs exceptions under the outer class name of the code calling this method. |
| 199 | + * <p> |
| 200 | + * If the calling code is showing it's own error toast, |
| 201 | + * instead use {@link #printInfo(LogMessage, Exception)} |
| 202 | + * |
| 203 | + * @param message log message |
| 204 | + * @param ex exception (optional) |
| 205 | + */ |
| 206 | + public static void printException(LogMessage message, @Nullable Throwable ex) { |
| 207 | + logInternal(LogLevel.ERROR, message, ex, includeStackTrace(), shouldShowErrorToast()); |
| 208 | + } |
| 209 | +} |
0 commit comments