Skip to content

Instantly share code, notes, and snippets.

@wajahatkarim3
Last active April 27, 2018 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wajahatkarim3/21fc299f22172c4a147ba3ce75d2d761 to your computer and use it in GitHub Desktop.
Save wajahatkarim3/21fc299f22172c4a147ba3ce75d2d761 to your computer and use it in GitHub Desktop.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import timber.log.Timber;
public class LinkingDebugTree extends Timber.DebugTree {
private static final int CALL_STACK_INDEX = 4;
private static final Pattern ANONYMOUS_CLASS = Pattern.compile("(\\$\\d+)+$");
@Override
protected void log(int priority, String tag, String message, Throwable t) {
// DO NOT switch this to Thread.getCurrentThread().getStackTrace(). The test will pass
// because Robolectric runs them on the JVM but on Android the elements are different.
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
if (stackTrace.length <= CALL_STACK_INDEX) {
throw new IllegalStateException(
"Synthetic stacktrace didn't have enough elements: are you using proguard?");
}
String clazz = extractClassName(stackTrace[CALL_STACK_INDEX]);
int lineNumber = stackTrace[CALL_STACK_INDEX].getLineNumber();
message = ".(" + clazz + ".java:" + lineNumber + ") - " + message;
super.log(priority, tag, message, t);
}
/**
* Extract the class name without any anonymous class suffixes (e.g., {@code Foo$1}
* becomes {@code Foo}).
*/
private String extractClassName(StackTraceElement element) {
String tag = element.getClassName();
Matcher m = ANONYMOUS_CLASS.matcher(tag);
if (m.find()) {
tag = m.replaceAll("");
}
return tag.substring(tag.lastIndexOf('.') + 1);
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.util.Log;
public class LogUtil {
private static final int CALL_STACK_INDEX = 1;
private static final Pattern ANONYMOUS_CLASS = Pattern.compile("(\\$\\d+)+$");
public static String prependCallLocation(String message) {
// DO NOT switch this to Thread.getCurrentThread().getStackTrace(). The test will pass
// because Robolectric runs them on the JVM but on Android the elements are different.
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
if (stackTrace.length <= CALL_STACK_INDEX) {
throw new IllegalStateException(
"Synthetic stacktrace didn't have enough elements: are you using proguard?");
}
String clazz = extractClassName(stackTrace[CALL_STACK_INDEX]);
int lineNumber = stackTrace[CALL_STACK_INDEX].getLineNumber();
message = ".(" + clazz + ".java:" + lineNumber + ") - " + message;
return message;
}
/**
* Extract the class name without any anonymous class suffixes (e.g., {@code Foo$1}
* becomes {@code Foo}).
*/
private static String extractClassName(StackTraceElement element) {
String tag = element.getClassName();
Matcher m = ANONYMOUS_CLASS.matcher(tag);
if (m.find()) {
tag = m.replaceAll("");
}
return tag.substring(tag.lastIndexOf('.') + 1);
}
public static void d(String tag, String message)
{
String newMessage = LogUtil.prependCallLocation(message);
Log.d(tag, newMessage);
}
public static void w(String tag, String message)
{
String newMessage = LogUtil.prependCallLocation(message);
Log.w(tag, newMessage);
}
public static void e(String tag, String message)
{
String newMessage = LogUtil.prependCallLocation(message);
Log.e(tag, newMessage);
}
public static void v(String tag, String message)
{
String newMessage = LogUtil.prependCallLocation(message);
Log.v(tag, newMessage);
}
public static void i(String tag, String message)
{
String newMessage = LogUtil.prependCallLocation(message);
Log.i(tag, newMessage);
}
public static void wtf(String tag, String message)
{
String newMessage = LogUtil.prependCallLocation(message);
Log.wtf(tag, newMessage);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment