|
| 1 | +package org.wordpress.android.util; |
| 2 | + |
| 3 | +import android.support.annotation.NonNull; |
| 4 | + |
| 5 | +/** |
| 6 | + * simple wrapper for Android log calls, enables recording and displaying log |
| 7 | + * |
| 8 | + * Testing version: |
| 9 | + * - replaces Log calls with System.out, since the unit tests don't have access to Android Framework classes. |
| 10 | + * - removes methods with Context parameter |
| 11 | + */ |
| 12 | +public class AppLog { |
| 13 | + // T for Tag |
| 14 | + public enum T { |
| 15 | + READER, |
| 16 | + EDITOR, |
| 17 | + MEDIA, |
| 18 | + NUX, |
| 19 | + API, |
| 20 | + STATS, |
| 21 | + UTILS, |
| 22 | + NOTIFS, |
| 23 | + DB, |
| 24 | + POSTS, |
| 25 | + PAGES, |
| 26 | + COMMENTS, |
| 27 | + THEMES, |
| 28 | + TESTS, |
| 29 | + PROFILING, |
| 30 | + SIMPERIUM, |
| 31 | + SUGGESTION, |
| 32 | + MAIN, |
| 33 | + SETTINGS, |
| 34 | + PLANS, |
| 35 | + PEOPLE, |
| 36 | + SHARING, |
| 37 | + PLUGINS, |
| 38 | + ACTIVITY_LOG, |
| 39 | + JETPACK_REMOTE_INSTALL, |
| 40 | + SUPPORT, |
| 41 | + SITE_CREATION |
| 42 | + } |
| 43 | + |
| 44 | + public static final String TAG = "WordPress"; |
| 45 | + |
| 46 | + private AppLog() { |
| 47 | + throw new AssertionError(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Capture log so it can be displayed by AppLogViewerActivity |
| 52 | + * |
| 53 | + * @param enable A boolean flag to capture log. Default is false, pass true to enable recording |
| 54 | + */ |
| 55 | + public static void enableRecording(boolean enable) { |
| 56 | + } |
| 57 | + |
| 58 | + public static void addListener(@NonNull AppLogListener listener) { |
| 59 | + } |
| 60 | + |
| 61 | + public static void removeListeners() { |
| 62 | + } |
| 63 | + |
| 64 | + public interface AppLogListener { |
| 65 | + void onLog(T tag, LogLevel logLevel, String message); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Sends a VERBOSE log message |
| 70 | + * |
| 71 | + * @param tag Used to identify the source of a log message. |
| 72 | + * It usually identifies the class or activity where the log call occurs. |
| 73 | + * @param message The message you would like logged. |
| 74 | + */ |
| 75 | + public static void v(T tag, String message) { |
| 76 | + message = StringUtils.notNullStr(message); |
| 77 | + System.out.println("v - " + TAG + " - " + tag.toString() + " - " + message); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Sends a DEBUG log message |
| 82 | + * |
| 83 | + * @param tag Used to identify the source of a log message. |
| 84 | + * It usually identifies the class or activity where the log call occurs. |
| 85 | + * @param message The message you would like logged. |
| 86 | + */ |
| 87 | + public static void d(T tag, String message) { |
| 88 | + message = StringUtils.notNullStr(message); |
| 89 | + System.out.println("d - " + TAG + " - " + tag.toString() + " - " + message); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Sends a INFO log message |
| 94 | + * |
| 95 | + * @param tag Used to identify the source of a log message. |
| 96 | + * It usually identifies the class or activity where the log call occurs. |
| 97 | + * @param message The message you would like logged. |
| 98 | + */ |
| 99 | + public static void i(T tag, String message) { |
| 100 | + message = StringUtils.notNullStr(message); |
| 101 | + System.out.println("i - " + TAG + " - " + tag.toString() + " - " + message); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Sends a WARN log message |
| 106 | + * |
| 107 | + * @param tag Used to identify the source of a log message. |
| 108 | + * It usually identifies the class or activity where the log call occurs. |
| 109 | + * @param message The message you would like logged. |
| 110 | + */ |
| 111 | + public static void w(T tag, String message) { |
| 112 | + message = StringUtils.notNullStr(message); |
| 113 | + System.out.println("w - " + TAG + " - " + tag.toString() + " - " + message); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Sends a ERROR log message |
| 118 | + * |
| 119 | + * @param tag Used to identify the source of a log message. |
| 120 | + * It usually identifies the class or activity where the log call occurs. |
| 121 | + * @param message The message you would like logged. |
| 122 | + */ |
| 123 | + public static void e(T tag, String message) { |
| 124 | + message = StringUtils.notNullStr(message); |
| 125 | + System.out.println("e - " + TAG + " - " + tag.toString() + " - " + message); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Send a ERROR log message and log the exception. |
| 130 | + * |
| 131 | + * @param tag Used to identify the source of a log message. |
| 132 | + * It usually identifies the class or activity where the log call occurs. |
| 133 | + * @param message The message you would like logged. |
| 134 | + * @param tr An exception to log |
| 135 | + */ |
| 136 | + public static void e(T tag, String message, Throwable tr) { |
| 137 | + message = StringUtils.notNullStr(message); |
| 138 | + System.out.println("e - " + TAG + " - " + tag.toString() + " - " + message); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Sends a ERROR log message and the exception with StackTrace |
| 143 | + * |
| 144 | + * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the |
| 145 | + * log call occurs. |
| 146 | + * @param tr An exception to log to get StackTrace |
| 147 | + */ |
| 148 | + public static void e(T tag, Throwable tr) { |
| 149 | + System.out.println("e - " + TAG + " - " + tag.toString() + " - " + tr.getMessage()); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Sends a ERROR log message |
| 154 | + * |
| 155 | + * @param tag Used to identify the source of a log message. It usually identifies the class or |
| 156 | + * activity where the |
| 157 | + * log call occurs. |
| 158 | + */ |
| 159 | + public static void e(T tag, String volleyErrorMsg, int statusCode) { |
| 160 | + if (volleyErrorMsg == null || "".equals(volleyErrorMsg)) { |
| 161 | + return; |
| 162 | + } |
| 163 | + String logText; |
| 164 | + if (statusCode == -1) { |
| 165 | + logText = volleyErrorMsg; |
| 166 | + } else { |
| 167 | + logText = volleyErrorMsg + ", status " + statusCode; |
| 168 | + } |
| 169 | + System.out.println("e - " + TAG + " - " + tag.toString() + " - " + logText); |
| 170 | + } |
| 171 | + |
| 172 | + // -------------------------------------------------------------------------------------------------------- |
| 173 | + |
| 174 | + |
| 175 | + public enum LogLevel { |
| 176 | + v, d, i, w, e |
| 177 | + } |
| 178 | +} |
0 commit comments