Last active
April 25, 2016 19:17
-
-
Save Tagakov/302ab893919272b9786e to your computer and use it in GitHub Desktop.
For those who cant wait for merge of https://github.com/JakeWharton/timber/pull/106
This file contains 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
import timber.log.Timber; | |
public class DebugTreeWithCodeLink extends Timber.DebugTree { | |
private final ThreadLocal<String> codeLink = new ThreadLocal<>(); | |
private static final int CALL_STACK_INDEX = 5; | |
@Override | |
protected String createStackElementTag(StackTraceElement element) { | |
codeLink.set(createCodeLink(element)); | |
return super.createStackElementTag(element); | |
} | |
private String createCodeLink(StackTraceElement element) { | |
int lineNumber = element.getLineNumber(); | |
if (lineNumber > 0) { | |
return String.format(" (%s:%s)", element.getFileName(), lineNumber); | |
} | |
return ""; | |
} | |
private String createCodeLink(StackTraceElement[] stackTrace) { | |
if (stackTrace.length <= CALL_STACK_INDEX) { | |
throw new IllegalStateException( | |
"Synthetic stacktrace didn't have enough elements: are you using proguard?"); | |
} | |
return createCodeLink(stackTrace[CALL_STACK_INDEX]); | |
} | |
@Override | |
protected void log(int priority, String tag, String message, Throwable t) { | |
String codeLinkStr = codeLink.get(); | |
if (codeLinkStr == null) { | |
codeLinkStr = createCodeLink(new Throwable().getStackTrace()); | |
} else { | |
codeLink.set(null); | |
} | |
super.log(priority, tag, message + codeLinkStr, t); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment