[size=1em][size=1em]/**
[size=1em] * Copyright (C) 2006 The Android Open Source Project
[size=1em] *
[size=1em] * Licensed under the Apache License, Version 2.0 (the "License");
[size=1em] * you may not use this file except in compliance with the License.
[size=1em] * You may obtain a copy of the License at
[size=1em] *
[size=1em] *
http://www.apache.org/licenses/LICENSE-2.0
[size=1em] *
[size=1em] * Unless required by applicable law or agreed to in writing, software
[size=1em] * distributed under the License is distributed on an "AS IS" BASIS,
[size=1em] * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
[size=1em] * See the License for the specific language governing permissions and
[size=1em] * limitations under the License.
[size=1em] */
[size=1em]package Android.util;
[size=1em]import com.Android.internal.os.RuntimeInit;
[size=1em]import java.io.PrintWriter;
[size=1em]import java.io.StringWriter;
[size=1em]/**
[size=1em] * API for sending log output.
[size=1em] *
[size=1em] * <p>Generally, use the Log.v() Log.d() Log.i() Log.w() and Log.e()
[size=1em] * methods.
[size=1em] *
[size=1em] * <p>The order in terms of verbosity, from least to most is
[size=1em] * ERROR, WARN, INFO, DEBUG, VERBOSE. Verbose should never be compiled
[size=1em] * into an application except during development. Debug logs are compiled
[size=1em] * in but stripped at runtime. Error, warning and info logs are always kept.
[size=1em] *
[size=1em] * <p><b>Tip:</b> A good convention is to declare a <code>TAG</code> constant
[size=1em] * in your class:
[size=1em] *
[size=1em] * <pre>private static final String TAG = "MyActivity";</pre>
[size=1em] *
[size=1em] * and use that in subsequent calls to the log methods.
[size=1em] * </p>
[size=1em] *
[size=1em] * <p><b>Tip:</b> Don't forget that when you make a call like
[size=1em] * <pre>Log.v(TAG, "index=" + i);</pre>
[size=1em] * that when you're building the string to pass into Log.d, the compiler uses a
[size=1em] * StringBuilder and at least three allocations occur: the StringBuilder
[size=1em] * itself, the buffer, and the String object. Realistically, there is also
[size=1em] * another buffer allocation and copy, and even more pressure on the gc.
[size=1em] * That means that if your log message is filtered out, you might be doing
[size=1em] * significant work and incurring significant overhead.
[size=1em] */
[size=1em]public final class Log {
[size=1em] /**
[size=1em] * Priority constant for the println method; use Log.v.
[size=1em] */
[size=1em] public static final int VERBOSE = 2;
[size=1em] /**
[size=1em] * Priority constant for the println method; use Log.d.
[size=1em] */
[size=1em] public static final int DEBUG = 3;
[size=1em] /**
[size=1em] * Priority constant for the println method; use Log.i.
[size=1em] */
[size=1em] public static final int INFO = 4;
[size=1em] /**
[size=1em] * Priority constant for the println method; use Log.w.
[size=1em] */
[size=1em] public static final int WARN = 5;
[size=1em] /**
[size=1em] * Priority constant for the println method; use Log.e.
[size=1em] */
[size=1em] public static final int ERROR = 6;
[size=1em] /**
[size=1em] * Priority constant for the println method.
[size=1em] */
[size=1em] public static final int ASSERT = 7;
[size=1em] /**
[size=1em] * Exception class used to capture a stack trace in {@link #wtf()}.
[size=1em] */
[size=1em] private static class TerribleFailure extends Exception {
[size=1em] TerribleFailure(String msg, Throwable cause) { super(msg, cause); }
[size=1em] }
[size=1em] private Log() {
[size=1em] }
[size=1em] /**
[size=1em] * Send a {@link #VERBOSE} log message.
[size=1em] * @param tag Used to identify the source of a log message. It usually identifies
[size=1em] * the class or activity where the log call occurs.
[size=1em] * @param msg The message you would like logged.
[size=1em] */
[size=1em] public static int v(String tag, String msg) {
[size=1em] return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);
[size=1em] }
[size=1em] /**
[size=1em] * Send a {@link #VERBOSE} log message and log the exception.
[size=1em] * @param tag Used to identify the source of a log message. It usually identifies
[size=1em] * the class or activity where the log call occurs.
[size=1em] * @param msg The message you would like logged.
[size=1em] * @param tr An exception to log
[size=1em] */
[size=1em] public static int v(String tag, String msg, Throwable tr) {
[size=1em] return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '/n' + getStackTraceString(tr));
[size=1em] }
[size=1em] /**
[size=1em] * Send a {@link #DEBUG} log message.
[size=1em] * @param tag Used to identify the source of a log message. It usually identifies
[size=1em] * the class or activity where the log call occurs.
[size=1em] * @param msg The message you would like logged.
[size=1em] */
[size=1em] public static int d(String tag, String msg) {
[size=1em] return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
[size=1em] }
[size=1em] /**
[size=1em] * Send a {@link #DEBUG} log message and log the exception.
[size=1em] * @param tag Used to identify the source of a log message. It usually identifies
[size=1em] * the class or activity where the log call occurs.
[size=1em] * @param msg The message you would like logged.
[size=1em] * @param tr An exception to log
[size=1em] */
[size=1em] public static int d(String tag, String msg, Throwable tr) {
[size=1em] return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '/n' + getStackTraceString(tr));
[size=1em] }
[size=1em] /**
[size=1em] * Send an {@link #INFO} log message.
[size=1em] * @param tag Used to identify the source of a log message. It usually identifies
[size=1em] * the class or activity where the log call occurs.
[size=1em] * @param msg The message you would like logged.
[size=1em] */
[size=1em] public static int i(String tag, String msg) {
[size=1em] return println_native(LOG_ID_MAIN, INFO, tag, msg);
[size=1em] }
[size=1em] /**
[size=1em] * Send a {@link #INFO} log message and log the exception.
[size=1em] * @param tag Used to identify the source of a log message. It usually identifies
[size=1em] * the class or activity where the log call occurs.
[size=1em] * @param msg The message you would like logged.
[size=1em] * @param tr An exception to log
[size=1em] */
[size=1em] public static int i(String tag, String msg, Throwable tr) {
[size=1em] return println_native(LOG_ID_MAIN, INFO, tag, msg + '/n' + getStackTraceString(tr));
[size=1em] }
[size=1em] /**
[size=1em] * Send a {@link #WARN} log message.
[size=1em] * @param tag Used to identify the source of a log message. It usually identifies
[size=1em] * the class or activity where the log call occurs.
[size=1em] * @param msg The message you would like logged.
[size=1em] */
[size=1em] public static int w(String tag, String msg) {
[size=1em] return println_native(LOG_ID_MAIN, WARN, tag, msg);
[size=1em] }
[size=1em] /**
[size=1em] * Send a {@link #WARN} log message and log the exception.
[size=1em] * @param tag Used to identify the source of a log message. It usually identifies
[size=1em] * the class or activity where the log call occurs.
[size=1em] * @param msg The message you would like logged.
[size=1em] * @param tr An exception to log
[size=1em] */
[size=1em] public static int w(String tag, String msg, Throwable tr) {
[size=1em] return println_native(LOG_ID_MAIN, WARN, tag, msg + '/n' + getStackTraceString(tr));
[size=1em] }
[size=1em] /**
[size=1em] * Checks to see whether or not a log for the specified tag is loggable at the specified level.
[size=1em] *
[size=1em] * The default level of any tag is set to INFO. This means that any level above and including
[size=1em] * INFO will be logged. Before you make any calls to a logging method you should check to see
[size=1em] * if your tag should be logged. You can change the default level by setting a system property:
[size=1em] * 'setprop log.tag.<YOUR_LOG_TAG> <LEVEL>'
[size=1em] * Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will
[size=1em] * turn off all logging for your tag. You can also create a local.prop file that with the
[size=1em] * following in it:
[size=1em] * 'log.tag.<YOUR_LOG_TAG>=<LEVEL>'
[size=1em] * and place that in /data/local.prop.
[size=1em] *
[size=1em] * @param tag The tag to check.
[size=1em] * @param level The level to check.
[size=1em] * @return Whether or not that this is allowed to be logged.
[size=1em] * @throws IllegalArgumentException is thrown if the tag.length() > 23.
[size=1em] */
[size=1em] public static native boolean isLoggable(String tag, int level);
[size=1em] /**
[size=1em] * Send a {@link #WARN} log message and log the exception.
[size=1em] * @param tag Used to identify the source of a log message. It usually identifies
[size=1em] * the class or activity where the log call occurs.
[size=1em] * @param tr An exception to log
[size=1em] */
[size=1em] public static int w(String tag, Throwable tr) {
[size=1em] return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));
[size=1em] }
[size=1em] /**
[size=1em] * Send an {@link #ERROR} log message.
[size=1em] * @param tag Used to identify the source of a log message. It usually identifies
[size=1em] * the class or activity where the log call occurs.
[size=1em] * @param msg The message you would like logged.
[size=1em] */
[size=1em] public static int e(String tag, String msg) {
[size=1em] return println_native(LOG_ID_MAIN, ERROR, tag, msg);
[size=1em] }
[size=1em] /**
[size=1em] * Send a {@link #ERROR} log message and log the exception.
[size=1em] * @param tag Used to identify the source of a log message. It usually identifies
[size=1em] * the class or activity where the log call occurs.
[size=1em] * @param msg The message you would like logged.
[size=1em] * @param tr An exception to log
[size=1em] */
[size=1em] public static int e(String tag, String msg, Throwable tr) {
[size=1em] return println_native(LOG_ID_MAIN, ERROR, tag, msg + '/n' + getStackTraceString(tr));
[size=1em] }
[size=1em] /**
[size=1em] * What a Terrible Failure: Report a condition that should never happen.
[size=1em] * The error will always be logged at level ASSERT with the call stack.
[size=1em] * Depending on system configuration, a report may be added to the
[size=1em] * {@link Android.os.DropBoxManager} and/or the process may be terminated
[size=1em] * immediately with an error dialog.
[size=1em] * @param tag Used to identify the source of a log message.
[size=1em] * @param msg The message you would like logged.
[size=1em] */
[size=1em] public static int wtf(String tag, String msg) {
[size=1em] return wtf(tag, msg, null);
[size=1em] }
[size=1em] /**
[size=1em] * What a Terrible Failure: Report an exception that should never happen.
[size=1em] * Similar to {@link #wtf(String, String)}, with an exception to log.
[size=1em] * @param tag Used to identify the source of a log message.
[size=1em] * @param tr An exception to log.
[size=1em] */
[size=1em] public static int wtf(String tag, Throwable tr) {
[size=1em] return wtf(tag, tr.getMessage(), tr);
[size=1em] }
[size=1em] /**
[size=1em] * What a Terrible Failure: Report an exception that should never happen.
[size=1em] * Similar to {@link #wtf(String, Throwable)}, with a message as well.
[size=1em] * @param tag Used to identify the source of a log message.
[size=1em] * @param msg The message you would like logged.
[size=1em] * @param tr An exception to log. May be null.
[size=1em] */
[size=1em] public static int wtf(String tag, String msg, Throwable tr) {
[size=1em] tr = new TerribleFailure(msg, tr);
[size=1em] int bytes = println_native(LOG_ID_MAIN, ASSERT, tag, getStackTraceString(tr));
[size=1em] RuntimeInit.wtf(tag, tr);
[size=1em] return bytes;
[size=1em] }
[size=1em] /**
[size=1em] * Handy function to get a loggable stack trace from a Throwable
[size=1em] * @param tr An exception to log
[size=1em] */
[size=1em] public static String getStackTraceString(Throwable tr) {
[size=1em] if (tr == null) {
[size=1em] return "";
[size=1em] }
[size=1em] StringWriter sw = new StringWriter();
[size=1em] PrintWriter pw = new PrintWriter(sw);
[size=1em] tr.printStackTrace(pw);
[size=1em] return sw.toString();
[size=1em] }
[size=1em] /**
[size=1em] * Low-level logging call.
[size=1em] * @param priority The priority/type of this log message
[size=1em] * @param tag Used to identify the source of a log message. It usually identifies
[size=1em] * the class or activity where the log call occurs.
[size=1em] * @param msg The message you would like logged.
[size=1em] * @return The number of bytes written.
[size=1em] */
[size=1em] public static int println(int priority, String tag, String msg) {
[size=1em] return println_native(LOG_ID_MAIN, priority, tag, msg);
[size=1em] }
[size=1em] /** @hide */ public static final int LOG_ID_MAIN = 0;
[size=1em] /** @hide */ public static final int LOG_ID_RADIO = 1;
[size=1em] /** @hide */ public static final int LOG_ID_EVENTS = 2;
[size=1em] /** @hide */ public static final int LOG_ID_SYSTEM = 3;
[size=1em] /** @hide */ public static native int println_native(int bufID,
[size=1em] int priority, String tag, String msg);
[size=1em]}