Skip to content

Instantly share code, notes, and snippets.

@kojiokb
Created September 5, 2012 14:36
Show Gist options
  • Save kojiokb/3637574 to your computer and use it in GitHub Desktop.
Save kojiokb/3637574 to your computer and use it in GitHub Desktop.
ダブルクリックでジャンプできるようにログを出す方法
import android.util.Log;
public class DebugLog {
private static final String TAG = "MyApp";
private static final int MAX_TAB_COUNT = 8;
private static final int TAB_SIZE = 4;
private static final int INDEX = 3;
/*
* DEBUG
*/
public static void d(String message) {
if (BuildConfig.DEBUG) {
Log.d(TAG, message + getPadding(message) + getAdditionalLog());
}
}
public static void d(String tag, String message) {
if (BuildConfig.DEBUG) {
Log.d(tag, message + getPadding(message) + getAdditionalLog());
}
}
public static void d(String message, Throwable throwable) {
if (BuildConfig.DEBUG) {
Log.d(TAG, message + getPadding(message) + getAdditionalLog(), throwable);
}
}
public static void d(String tag, String message, Throwable throwable) {
if (BuildConfig.DEBUG) {
Log.d(tag, message + getPadding(message) + getAdditionalLog(), throwable);
}
}
private static String getPadding(String message) {
String padding = "";
int tabCount = MAX_TAB_COUNT - (message.length() / TAB_SIZE);
if (tabCount <= 0) {
return "\t";
}
for (int i = 0; i < tabCount; i++) {
padding += "\t";
}
return padding;
}
private static String getAdditionalLog() {
String additionalLog = " at " + getClassName() + "#" + getMethodName()
+ "(" + getFileName() + ":" + getLineNumber() + ")";
return additionalLog;
}
private static String getClassName() {
String className = new Throwable().getStackTrace()[INDEX].getClassName();
className = className.substring(className.lastIndexOf(".") + 1);
return className;
}
private static String getMethodName() {
return new Throwable().getStackTrace()[INDEX].getMethodName();
}
private static String getFileName() {
return new Throwable().getStackTrace()[INDEX].getFileName();
}
private static String getLineNumber() {
return String.valueOf(new Throwable().getStackTrace()[INDEX].getLineNumber());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment