public class LogUtil {
private static final boolean DEBUG = true;
private LogUtil() {
throw new UnsupportedOperationException("Cannot initialize " + getClass().getCanonicalName() + " class");
}
private static String addCallerInformation() {
int i, lio; //lio = lastIndexOf
StackTraceElement stack[] = Thread.currentThread().getStackTrace();
for (i = 0; !stack[i].getClassName().equals(LogUtil.class.getName()); i++) {
}
for (; stack[i].getClassName().equals(LogUtil.class.getName()); i++) {
}
lio = stack[i].getFileName().lastIndexOf('.');
if (lio == -1) {
return " (" + stack[i].getFileName() + ":" + stack[i].getLineNumber() + ")";
} else {
return " (" + stack[i].getFileName().substring(0, lio) + ":" + stack[i].getLineNumber() + ")";
}
}
public static void i(Object obj, Object message){
if(DEBUG) {
if (obj != null && message != null) {
Log.i(obj instanceof CharSequence ? obj.toString() : obj.getClass().getSimpleName(), message.toString().trim() + addCallerInformation());
}
}
}
public static void e(Object obj, Object message) {
if(DEBUG) {
if (obj != null && message != null) {
Log.e(obj instanceof CharSequence ? obj.toString() : obj.getClass().getSimpleName(), message.toString().trim() + addCallerInformation());
}
}
}
public static void d(Object obj, Object message) {
if(DEBUG) {
if (obj != null && message != null) {
Log.d(obj instanceof CharSequence ? obj.toString() : obj.getClass().getSimpleName(), message.toString().trim() + addCallerInformation());
}
}
}
public static void v(Object obj, Object message) {
if(DEBUG) {
if (obj != null && message != null) {
Log.v(obj instanceof CharSequence ? obj.toString() : obj.getClass().getSimpleName(), message.toString().trim() + addCallerInformation());
}
}
}
public static void w(Object obj, Object message) {
if(DEBUG) {
if (obj != null && message != null) {
Log.w(obj instanceof CharSequence ? obj.toString() : obj.getClass().getSimpleName(), message.toString().trim() + addCallerInformation());
}
}
}
public static void wtf(Object obj, Object message, Throwable t) {
if(DEBUG) {
if (obj != null && message != null) {
Log.wtf(obj instanceof CharSequence ? obj.toString() : obj.getClass().getSimpleName(), message.toString().trim() + addCallerInformation(), t);
}
}
}
}
【Android Util】全局控制Log打印日志
最新推荐文章于 2023-03-17 15:30:57 发布