Skip to content

Instantly share code, notes, and snippets.

@shiraji
Last active May 19, 2018 00:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiraji/2be2ebe43e1fac24f86b to your computer and use it in GitHub Desktop.
Save shiraji/2be2ebe43e1fac24f86b to your computer and use it in GitHub Desktop.
ExtDebugTree
/*
* Copyright (C) 2015 Yoshinori Isogai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;
import timber.log.Timber;
public class ExtDebugTree extends Timber.DebugTree {
private static final int MAX_LOG_LENGTH = 4000;
private static final String CALLER_INFO_FORMAT = " at %s(%s:%s)";
private boolean mShowLink = true;
private String mCallerInfo;
@Override
protected void log(int priority, String tag, String message, Throwable t) {
if(mShowLink) {
mCallerInfo = getCallerInfo(new Throwable().getStackTrace());
}
if (message.length() < MAX_LOG_LENGTH) {
printSingleLine(priority, tag, message + mCallerInfo);
} else {
printMultipleLines(priority, tag, message);
}
}
private void printMultipleLines(int priority, String tag, String message) {
// 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);
printSingleLine(priority, tag, part);
i = end;
} while (i < newline);
}
if(mShowLink && !TextUtils.isEmpty(mCallerInfo)) {
printSingleLine(priority, tag, mCallerInfo);
}
}
@SuppressLint("LogNotTimber")
private void printSingleLine(int priority, String tag, String message) {
if (priority == Log.ASSERT) {
Log.wtf(tag, message);
} else {
Log.println(priority, tag, message);
}
}
private String getCallerInfo(StackTraceElement[] stacks) {
if (stacks == null || stacks.length < 5) {
// are you using proguard???
return "";
}
return formatForLogCat(stacks[5]);
}
private static String formatForLogCat(StackTraceElement stack) {
String className = stack.getClassName();
String packageName = className.substring(0, className.lastIndexOf("."));
return String.format(CALLER_INFO_FORMAT, packageName,
stack.getFileName(), stack.getLineNumber());
}
}
@shiraji
Copy link
Author

shiraji commented Jun 6, 2015

This add a jumping function to Timber's DebugTree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment