Created
September 7, 2014 08:46
-
-
Save s-shin/cdcf1d80b09ad4621a27 to your computer and use it in GitHub Desktop.
Hotentorで使ったLogユーティリティ
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
package jp.hateblo.shin.hotentor.util; | |
import android.util.Log; | |
import jp.hateblo.shin.hotentor.BuildConfig; | |
/** | |
* ref: http://wada811.blogspot.com/2013/04/android-log-util.html | |
* Created by shin on 2014/08/30. | |
*/ | |
public class LogUtil { | |
public static final String TAG = "Hotentor"; | |
public static void d(String msg) { | |
if (BuildConfig.DEBUG) { | |
Log.d(TAG, getMetaInfo() + " " + null2str(msg)); | |
} | |
} | |
public static void i(String msg) { | |
if (BuildConfig.DEBUG) { | |
Log.i(TAG, getMetaInfo() + " " + null2str(msg)); | |
} | |
} | |
public static void w(String msg) { | |
if (BuildConfig.DEBUG) { | |
Log.w(TAG, getMetaInfo() + " " + null2str(msg)); | |
} | |
} | |
public static void e(String msg) { | |
if (BuildConfig.DEBUG) { | |
Log.e(TAG, getMetaInfo() + " " + null2str(msg)); | |
} | |
} | |
public static void wtf(String msg) { | |
if (BuildConfig.DEBUG) { | |
Log.wtf(TAG, getMetaInfo() + " " + null2str(msg)); | |
} | |
} | |
public static void d(Throwable t) { | |
d(getThrowableInfo(t)); | |
} | |
public static void i(Throwable t) { | |
i(getThrowableInfo(t)); | |
} | |
public static void w(Throwable t) { | |
w(getThrowableInfo(t)); | |
} | |
public static void e(Throwable t) { | |
e(getThrowableInfo(t)); | |
} | |
public static void wtf(Throwable t) { | |
wtf(getThrowableInfo(t)); | |
} | |
/** | |
* 例外のスタックトレースを文字で取得する。 | |
* @param t | |
*/ | |
public static String getThrowableInfo(Throwable t){ | |
StringBuilder builder = new StringBuilder(); | |
builder.append(t.getClass().getName()); | |
builder.append(": "); | |
builder.append(t.getMessage()); | |
for (StackTraceElement element : t.getStackTrace()) { | |
builder.append(" at " + LogUtil.getMetaInfo(element)); | |
} | |
return builder.toString(); | |
} | |
/** | |
* nullを文字に変換。 | |
* @param string | |
* @return | |
*/ | |
private static String null2str(String string){ | |
return string == null ? "(null)" : string; | |
} | |
/** | |
* ログ呼び出し元のメタ情報を取得する | |
* @return [className#methodName:line] | |
*/ | |
private static String getMetaInfo() { | |
// スタックトレースから情報を取得 // 0: VM, 1: Thread, 2: LogUtil#getMetaInfo, 3: LogUtil#d など, 4: 呼び出し元 | |
final StackTraceElement element = Thread.currentThread().getStackTrace()[4]; | |
return LogUtil.getMetaInfo(element); | |
} | |
/** | |
* スタックトレースからクラス名、メソッド名、行数を取得する | |
* @return [className#methodName:line] | |
*/ | |
public static String getMetaInfo(StackTraceElement element) { | |
// クラス名、メソッド名、行数を取得 | |
final String fullClassName = element.getClassName(); | |
final String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf(".") + 1); | |
final String methodName = element.getMethodName(); | |
final int lineNumber = element.getLineNumber(); | |
// メタ情報 | |
final String metaInfo = "[" + simpleClassName + "#" + methodName + ":" + lineNumber + "]"; | |
return metaInfo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment