Last active
May 19, 2018 00:33
-
-
Save shiraji/2be2ebe43e1fac24f86b to your computer and use it in GitHub Desktop.
ExtDebugTree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This add a jumping function to Timber's DebugTree.