下面列出了android.util.Log#println ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Prints data out to the console using Android's native log mechanism.
* @param priority Log level of the data being logged. Verbose, Error, etc.
* @param tag Tag for for the log data. Can be used to organize log statements.
* @param msg The actual message to be logged. The actual message to be logged.
* @param tr If an exception was thrown, this can be sent along for the logging facilities
* to extract and print useful information.
*/
@Override
public void println(int priority, String tag, String msg, Throwable tr) {
// There actually are log methods that don't take a msg parameter. For now,
// if that's the case, just convert null to the empty string and move on.
String useMsg = msg;
if (useMsg == null) {
useMsg = "";
}
// If an exeption was provided, convert that exception to a usable string and attach
// it to the end of the msg method.
if (tr != null) {
msg += "\n" + Log.getStackTraceString(tr);
}
// This is functionally identical to Log.x(tag, useMsg);
// For instance, if priority were Log.VERBOSE, this would be the same as Log.v(tag, useMsg)
Log.println(priority, tag, useMsg);
// If this isn't the last node in the chain, move things along.
if (mNext != null) {
mNext.println(priority, tag, msg, tr);
}
}
/**
* Prints data out to the console using Android's native log mechanism.
*
* @param priority Log level of the data being logged. Verbose, Error, etc.
* @param tag Tag for for the log data. Can be used to organize log statements.
* @param msg The actual message to be logged. The actual message to be logged.
* @param tr If an exception was thrown, this can be sent along for the logging
* facilities
* to extract and print useful information.
*/
@Override
public void println(int priority, String tag, String msg, Throwable tr) {
// There actually are log methods that don't take a msg parameter. For now,
// if that's the case, just convert null to the empty string and move on.
String useMsg = msg;
if (useMsg == null) {
useMsg = "";
}
// If an exeption was provided, convert that exception to a usable string and attach
// it to the end of the msg method.
if (tr != null) {
msg += "\n" + Log.getStackTraceString(tr);
}
// This is functionally identical to Log.x(tag, useMsg);
// For instance, if priority were Log.VERBOSE, this would be the same as Log.v(tag, useMsg)
Log.println(priority, tag, useMsg);
// If this isn't the last node in the chain, move things along.
if (mNext != null) {
mNext.println(priority, tag, msg, tr);
}
}
/**
* Prints data out to the console using Android's native log mechanism.
* @param priority Log level of the data being logged. Verbose, Error, etc.
* @param tag Tag for for the log data. Can be used to organize log statements.
* @param msg The actual message to be logged. The actual message to be logged.
* @param tr If an exception was thrown, this can be sent along for the logging facilities
* to extract and print useful information.
*/
@Override
public void println(int priority, String tag, String msg, Throwable tr) {
// There actually are log methods that don't take a msg parameter. For now,
// if that's the case, just convert null to the empty string and move on.
String useMsg = msg;
if (useMsg == null) {
useMsg = "";
}
// If an exeption was provided, convert that exception to a usable string and attach
// it to the end of the msg method.
if (tr != null) {
msg += "\n" + Log.getStackTraceString(tr);
}
// This is functionally identical to Log.x(tag, useMsg);
// For instance, if priority were Log.VERBOSE, this would be the same as Log.v(tag, useMsg)
Log.println(priority, tag, useMsg);
// If this isn't the last node in the chain, move things along.
if (mNext != null) {
mNext.println(priority, tag, msg, tr);
}
}
private static void log(String tag, int level, Throwable t, Object... messages) {
String message;
if (t == null && messages != null && messages.length == 1) {
// handle this common case without the extra cost of creating a stringBuffer:
message = messages[0].toString();
} else {
StringBuilder sb = new StringBuilder();
if (messages != null) for (Object m : messages) {
sb.append(m);
}
if (t != null) {
sb.append("\n").append(Log.getStackTraceString(t));
}
message = sb.toString();
}
Log.println(level, tag, message);
}
/**
* Prints data out to the console using Android's native log mechanism.
*
* @param priority Log level of the data being logged. Verbose, Error, etc.
* @param tag Tag for for the log data. Can be used to organize log statements.
* @param msg The actual message to be logged. The actual message to be logged.
* @param tr If an exception was thrown, this can be sent along for the logging
* facilities
* to extract and print useful information.
*/
@Override
public void println(int priority, String tag, String msg, Throwable tr) {
// There actually are log methods that don't take a msg parameter. For now,
// if that's the case, just convert null to the empty string and move on.
String useMsg = msg;
if (useMsg == null) {
useMsg = "";
}
// If an exeption was provided, convert that exception to a usable string and attach
// it to the end of the msg method.
if (tr != null) {
msg += "\n" + Log.getStackTraceString(tr);
}
// This is functionally identical to Log.x(tag, useMsg);
// For instance, if priority were Log.VERBOSE, this would be the same as Log.v(tag, useMsg)
Log.println(priority, tag, useMsg);
// If this isn't the last node in the chain, move things along.
if (mNext != null) {
mNext.println(priority, tag, msg, tr);
}
}
/**
* Prints data out to the console using Android's native log mechanism.
* @param priority Log level of the data being logged. Verbose, Error, etc.
* @param tag Tag for for the log data. Can be used to organize log statements.
* @param msg The actual message to be logged. The actual message to be logged.
* @param tr If an exception was thrown, this can be sent along for the logging facilities
* to extract and print useful information.
*/
@Override
public void println(int priority, String tag, String msg, Throwable tr) {
// There actually are log methods that don't take a msg parameter. For now,
// if that's the case, just convert null to the empty string and move on.
String useMsg = msg;
if (useMsg == null) {
useMsg = "";
}
// If an exeption was provided, convert that exception to a usable string and attach
// it to the end of the msg method.
if (tr != null) {
msg += "\n" + Log.getStackTraceString(tr);
}
// This is functionally identical to Log.x(tag, useMsg);
// For instance, if priority were Log.VERBOSE, this would be the same as Log.v(tag, useMsg)
Log.println(priority, tag, useMsg);
// If this isn't the last node in the chain, move things along.
if (mNext != null) {
mNext.println(priority, tag, msg, tr);
}
}
private static int println(int level, String tag, String msg) {
if (LOG_LEVEL <= level) {
return Log.println(level, tag, TextUtils.isEmpty(msg) ? "null" : msg);
} else {
return -1;
}
}
private void throwShade(int priority, String message, Throwable t) {
if (message == null || message.length() == 0) {
if (t != null) {
message = Log.getStackTraceString(t);
} else {
// Swallow message if it's null and there's no throwable.
return;
}
} else if (t != null) {
message += "\n" + Log.getStackTraceString(t);
}
String tag = createTag();
if (message.length() < 4000) {
/*
* Replacing the original tag with the application tag and
* using the tag for the class formatted at the beginning.
*/
String taggedMessage = String.format("%s -> %s", tag, message);
Log.println(priority, applicationTag, taggedMessage);
} else {
// It's rare that the message will be this large, so we're ok with the perf hit of splitting
// and calling Log.println N times. It's possible but unlikely that a single line will be
// longer than 4000 characters: we're explicitly ignoring this case here.
String[] lines = message.split("\n");
for (String line : lines) {
Log.println(priority, tag, line);
}
}
}
private static void printSubMsg(final int type, final String tag, final String msg) {
if (!CONFIG.isLogBorderSwitch()) {
Log.println(type, tag, msg);
return;
}
StringBuilder sb = new StringBuilder();
String[] lines = msg.split(LINE_SEP);
for (String line : lines) {
Log.println(type, tag, LEFT_BORDER + line);
}
}
public static void w(String message) {
if (!DEVELOP_MODE || TextUtils.isEmpty(message)) {
return;
}
final StackTraceElement[] stack = new Throwable().getStackTrace();
final int i = 1;
final StackTraceElement ste = stack[i];
Log.println(Log.WARN, LOG_TAG, String.format("[%s][%s][%s]%s", ste.getFileName(), ste.getMethodName(), ste.getLineNumber(), message));
}
private void log_private(int level, String tag, String message, StackTraceElement trace)
{
message = prefixMessage(trace.getMethodName(), message);
if (m_logger != null)
{
m_logger.onLogEntry(level, tag, message);
}
else
{
Log.println(level, tag, message);
}
}
public static void e(String message) {
if (!DEVELOP_MODE || TextUtils.isEmpty(message)) {
return;
}
final StackTraceElement[] stack = new Throwable().getStackTrace();
final int i = 1;
final StackTraceElement ste = stack[i];
Log.println(Log.ERROR, LOG_TAG, String.format("[%s][%s][%s]%s", ste.getFileName(), ste.getMethodName(), ste.getLineNumber(), message));
}
@Override
public boolean onException(Exception e, A model, Target<B> target, boolean isFirstResource) {
Log.println(level, "GLIDE", String.format(Locale.ROOT,
"%s.onException(%s, %s, %s, %s)\n%s",
name, e, model, strip(target), isFirst(isFirstResource), Log.getStackTraceString(e)));
return delegate.onException(e, model, target, isFirstResource);
}
private static void d(int i, String str, String str2) {
if (!d) {
str = "FlurryAgent";
}
int length = TextUtils.isEmpty(str2) ? 0 : str2.length();
int i2 = 0;
while (i2 < length) {
int i3 = a > length - i2 ? length : a + i2;
if (Log.println(i, str, str2.substring(i2, i3)) > 0) {
i2 = i3;
} else {
return;
}
}
}
/**
* Break up {@code message} into maximum-length chunks (if needed) and send to either
* {@link Log#println(int, String, String) Log.println()} or
* {@link Log#wtf(String, String) Log.wtf()} for logging.
* <p>
* {@inheritDoc}
*/
@Override
protected void log(int priority, String tag, String message, Throwable t) {
if (message.length() < MAX_LOG_LENGTH) {
if (priority == Log.ASSERT) {
Log.wtf(tag, message);
} else {
Log.println(priority, tag, message);
}
return;
}
// Split by line, then ensure each line can fit into Log's maximum length.
for (int i = 0, length = message.length(); i < length; i++) {
int newline = message.indexOf('\n', i);
newline = newline != -1 ? newline : length;
do {
int end = Math.min(newline, i + MAX_LOG_LENGTH);
String part = message.substring(i, end);
if (priority == Log.ASSERT) {
Log.wtf(tag, part);
} else {
Log.println(priority, tag, part);
}
i = end;
} while (i < newline);
}
}
private static void printHead(final int type, final String tag, final String[] head) {
if (head != null) {
for (String aHead : head) {
Log.println(type, tag, sLogBorderSwitch ? LEFT_BORDER + aHead : aHead);
}
if (sLogBorderSwitch) Log.println(type, tag, MIDDLE_BORDER);
}
}
@Override
public void print(int level, StackTraceElement element, String tag, Object msg) {
String className = element.getClassName();
className = className.substring(className.lastIndexOf(".") + 1);
String codeLine = className+'.'+element.getMethodName()+'('+element.getFileName()+':'+element.getLineNumber()+')';
Log.println(level, tag, codeLine);
String message = toString(msg);
Log.println(level, tag, "\t" + message);
}
private static void printBorder(final int type, final String tag, boolean isTop) {
if (sLogBorderSwitch) {
Log.println(type, tag, isTop ? TOP_BORDER : BOTTOM_BORDER);
}
}
private static void traceAndLog(int level, String message) {
TraceEvent.instant("TraceEvent.LooperMonitor:IdleStats", message);
Log.println(level, TAG, message);
}