1 /* 2 * Copyright (C) 2006 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 17 package android.util; 18 19 import java.io.PrintWriter; 20 import java.io.StringWriter; 21 import java.net.UnknownHostException; 22 23 /** 24 * Mock Log implementation for testing on non android host. 25 */ 26 public final class Log { 27 28 /** 29 * Priority constant for the println method; use Log.v. 30 */ 31 public static final int VERBOSE = 2; 32 33 /** 34 * Priority constant for the println method; use Log.d. 35 */ 36 public static final int DEBUG = 3; 37 38 /** 39 * Priority constant for the println method; use Log.i. 40 */ 41 public static final int INFO = 4; 42 43 /** 44 * Priority constant for the println method; use Log.w. 45 */ 46 public static final int WARN = 5; 47 48 /** 49 * Priority constant for the println method; use Log.e. 50 */ 51 public static final int ERROR = 6; 52 53 /** 54 * Priority constant for the println method. 55 */ 56 public static final int ASSERT = 7; 57 Log()58 private Log() { 59 } 60 61 /** 62 * Send a {@link #VERBOSE} log message. 63 * @param tag Used to identify the source of a log message. It usually identifies 64 * the class or activity where the log call occurs. 65 * @param msg The message you would like logged. 66 */ v(String tag, String msg)67 public static int v(String tag, String msg) { 68 return println(LOG_ID_MAIN, VERBOSE, tag, msg); 69 } 70 71 /** 72 * Send a {@link #VERBOSE} log message and log the exception. 73 * @param tag Used to identify the source of a log message. It usually identifies 74 * the class or activity where the log call occurs. 75 * @param msg The message you would like logged. 76 * @param tr An exception to log 77 */ v(String tag, String msg, Throwable tr)78 public static int v(String tag, String msg, Throwable tr) { 79 return println(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(tr)); 80 } 81 82 /** 83 * Send a {@link #DEBUG} log message. 84 * @param tag Used to identify the source of a log message. It usually identifies 85 * the class or activity where the log call occurs. 86 * @param msg The message you would like logged. 87 */ d(String tag, String msg)88 public static int d(String tag, String msg) { 89 return println(LOG_ID_MAIN, DEBUG, tag, msg); 90 } 91 92 /** 93 * Send a {@link #DEBUG} log message and log the exception. 94 * @param tag Used to identify the source of a log message. It usually identifies 95 * the class or activity where the log call occurs. 96 * @param msg The message you would like logged. 97 * @param tr An exception to log 98 */ d(String tag, String msg, Throwable tr)99 public static int d(String tag, String msg, Throwable tr) { 100 return println(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr)); 101 } 102 103 /** 104 * Send an {@link #INFO} log message. 105 * @param tag Used to identify the source of a log message. It usually identifies 106 * the class or activity where the log call occurs. 107 * @param msg The message you would like logged. 108 */ i(String tag, String msg)109 public static int i(String tag, String msg) { 110 return println(LOG_ID_MAIN, INFO, tag, msg); 111 } 112 113 /** 114 * Send a {@link #INFO} log message and log the exception. 115 * @param tag Used to identify the source of a log message. It usually identifies 116 * the class or activity where the log call occurs. 117 * @param msg The message you would like logged. 118 * @param tr An exception to log 119 */ i(String tag, String msg, Throwable tr)120 public static int i(String tag, String msg, Throwable tr) { 121 return println(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr)); 122 } 123 124 /** 125 * Send a {@link #WARN} log message. 126 * @param tag Used to identify the source of a log message. It usually identifies 127 * the class or activity where the log call occurs. 128 * @param msg The message you would like logged. 129 */ w(String tag, String msg)130 public static int w(String tag, String msg) { 131 return println(LOG_ID_MAIN, WARN, tag, msg); 132 } 133 134 /** 135 * Send a {@link #WARN} log message and log the exception. 136 * @param tag Used to identify the source of a log message. It usually identifies 137 * the class or activity where the log call occurs. 138 * @param msg The message you would like logged. 139 * @param tr An exception to log 140 */ w(String tag, String msg, Throwable tr)141 public static int w(String tag, String msg, Throwable tr) { 142 return println(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(tr)); 143 } 144 145 /* 146 * Send a {@link #WARN} log message and log the exception. 147 * @param tag Used to identify the source of a log message. It usually identifies 148 * the class or activity where the log call occurs. 149 * @param tr An exception to log 150 */ w(String tag, Throwable tr)151 public static int w(String tag, Throwable tr) { 152 return println(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr)); 153 } 154 155 /** 156 * Send an {@link #ERROR} log message. 157 * @param tag Used to identify the source of a log message. It usually identifies 158 * the class or activity where the log call occurs. 159 * @param msg The message you would like logged. 160 */ e(String tag, String msg)161 public static int e(String tag, String msg) { 162 return println(LOG_ID_MAIN, ERROR, tag, msg); 163 } 164 165 /** 166 * Send a {@link #ERROR} log message and log the exception. 167 * @param tag Used to identify the source of a log message. It usually identifies 168 * the class or activity where the log call occurs. 169 * @param msg The message you would like logged. 170 * @param tr An exception to log 171 */ e(String tag, String msg, Throwable tr)172 public static int e(String tag, String msg, Throwable tr) { 173 return println(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr)); 174 } 175 176 /** 177 * Handy function to get a loggable stack trace from a Throwable 178 * @param tr An exception to log 179 */ getStackTraceString(Throwable tr)180 public static String getStackTraceString(Throwable tr) { 181 if (tr == null) { 182 return ""; 183 } 184 185 // This is to reduce the amount of log spew that apps do in the non-error 186 // condition of the network being unavailable. 187 Throwable t = tr; 188 while (t != null) { 189 if (t instanceof UnknownHostException) { 190 return ""; 191 } 192 t = t.getCause(); 193 } 194 195 StringWriter sw = new StringWriter(); 196 PrintWriter pw = new PrintWriter(sw); 197 tr.printStackTrace(pw); 198 pw.flush(); 199 return sw.toString(); 200 } 201 202 /** 203 * Low-level logging call. 204 * @param priority The priority/type of this log message 205 * @param tag Used to identify the source of a log message. It usually identifies 206 * the class or activity where the log call occurs. 207 * @param msg The message you would like logged. 208 * @return The number of bytes written. 209 */ println(int priority, String tag, String msg)210 public static int println(int priority, String tag, String msg) { 211 return println(LOG_ID_MAIN, priority, tag, msg); 212 } 213 214 /** @hide */ public static final int LOG_ID_MAIN = 0; 215 /** @hide */ public static final int LOG_ID_RADIO = 1; 216 /** @hide */ public static final int LOG_ID_EVENTS = 2; 217 /** @hide */ public static final int LOG_ID_SYSTEM = 3; 218 /** @hide */ public static final int LOG_ID_CRASH = 4; 219 220 /** @hide */ @SuppressWarnings("unused") println(int bufID, int priority, String tag, String msg)221 public static int println(int bufID, 222 int priority, String tag, String msg) { 223 return 0; 224 } 225 } 226