|
|
@@ -30,18 +30,15 @@ public final class Jlog {
|
|
|
|
|
|
static final int JSON_INDENT = 4;
|
|
|
|
|
|
- private static final int STACK_TRACE_INDEX_5 = 5;
|
|
|
- private static final int STACK_TRACE_INDEX_4 = 4;
|
|
|
-
|
|
|
private static String mGlobalTag;
|
|
|
private static boolean mIsGlobalTagEmpty = true;
|
|
|
- private static boolean IS_SHOW_LOG = true;
|
|
|
- private static boolean IS_INIT = false;
|
|
|
- static boolean IS_WRITE = true;
|
|
|
+ static boolean IS_SHOW_LOG = false;
|
|
|
+ static boolean IS_WRITE = false;
|
|
|
+ static boolean IS_INIT = true;
|
|
|
|
|
|
public static void init(boolean isShowLog) {
|
|
|
+ IS_INIT = isShowLog;
|
|
|
IS_SHOW_LOG = isShowLog;
|
|
|
- IS_INIT = true;
|
|
|
}
|
|
|
|
|
|
public static void init(boolean isShowLog, @Nullable String tag) {
|
|
|
@@ -68,6 +65,10 @@ public final class Jlog {
|
|
|
printLog(LogType.V, null, msg);
|
|
|
}
|
|
|
|
|
|
+ static void v(int length, Object msg) {
|
|
|
+ printLog(LogType.V, null, msg);
|
|
|
+ }
|
|
|
+
|
|
|
public static void v(Object tag, @NonNull Object... objects) {
|
|
|
printLog(LogType.V, tag, objects);
|
|
|
}
|
|
|
@@ -176,14 +177,6 @@ public final class Jlog {
|
|
|
printStackTrace();
|
|
|
}
|
|
|
|
|
|
- public static void writeFile(Object msg) {
|
|
|
- printLog(LogType.WRITE, null, msg);
|
|
|
- }
|
|
|
-
|
|
|
- public static void writeFile(Object tag, Object msg) {
|
|
|
- printLog(LogType.WRITE, tag, msg);
|
|
|
- }
|
|
|
-
|
|
|
public static void errorWriteFile(Throwable throwable) {
|
|
|
if (IS_WRITE) {
|
|
|
BaseLog.writeError(null, throwable);
|
|
|
@@ -191,14 +184,16 @@ public final class Jlog {
|
|
|
}
|
|
|
|
|
|
public static void errorWriteFile(String tag, Throwable throwable) {
|
|
|
- BaseLog.writeError(tag, throwable);
|
|
|
+ if (IS_WRITE) {
|
|
|
+ BaseLog.writeError(tag, throwable);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private static void printStackTrace() {
|
|
|
- if (!IS_SHOW_LOG) {
|
|
|
+ if (!IS_INIT && !IS_WRITE) {
|
|
|
return;
|
|
|
}
|
|
|
- if (!IS_INIT && isApkInDebug()) {
|
|
|
+ if (!IS_SHOW_LOG && isApkInDebug() && !IS_WRITE) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -218,7 +213,10 @@ public final class Jlog {
|
|
|
}
|
|
|
sb.append(trace).append("\n");
|
|
|
}
|
|
|
- String[] contents = wrapperContent(STACK_TRACE_INDEX_4, null, sb.toString());
|
|
|
+ String[] contents = wrapperContent(null, sb.toString());
|
|
|
+ if (contents == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
String tag = contents[0];
|
|
|
String msg = contents[1];
|
|
|
String headString = contents[2];
|
|
|
@@ -226,38 +224,47 @@ public final class Jlog {
|
|
|
}
|
|
|
|
|
|
private static void printLog(LogType type, Object tagStr, Object... objects) {
|
|
|
- if (IS_SHOW_LOG || (type == LogType.WRITE && IS_WRITE)) {
|
|
|
- if ((!IS_INIT && isApkInDebug()) || (type == LogType.WRITE && !IS_WRITE)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- String[] contents = wrapperContent(STACK_TRACE_INDEX_5, tagStr, objects);
|
|
|
- String tag = contents[0];
|
|
|
- String msg = contents[1];
|
|
|
- String headString = contents[2];
|
|
|
-
|
|
|
- switch (type) {
|
|
|
- default:
|
|
|
- case WRITE:
|
|
|
- case V:
|
|
|
- case D:
|
|
|
- case I:
|
|
|
- case W:
|
|
|
- case E:
|
|
|
- case A:
|
|
|
- BaseLog.printDefault(type, tag, headString + msg);
|
|
|
- break;
|
|
|
- case JSON:
|
|
|
- JsonLog.printJson(tag, msg, headString);
|
|
|
- break;
|
|
|
- case XML:
|
|
|
- XmlLog.printXml(tag, msg, headString);
|
|
|
- break;
|
|
|
- }
|
|
|
+ if (!IS_INIT && !IS_WRITE) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!IS_SHOW_LOG && isApkInDebug() && !IS_WRITE) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String[] contents = wrapperContent(tagStr, objects);
|
|
|
+ if (contents == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String tag = contents[0];
|
|
|
+ String msg = contents[1];
|
|
|
+ String headString = contents[2];
|
|
|
+
|
|
|
+ switch (type) {
|
|
|
+ default:
|
|
|
+ case V:
|
|
|
+ case D:
|
|
|
+ case I:
|
|
|
+ case W:
|
|
|
+ case E:
|
|
|
+ case A:
|
|
|
+ BaseLog.printDefault(type, tag, headString + msg);
|
|
|
+ break;
|
|
|
+ case JSON:
|
|
|
+ JsonLog.printJson(tag, msg, headString);
|
|
|
+ break;
|
|
|
+ case XML:
|
|
|
+ XmlLog.printXml(tag, msg, headString);
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private static void printDebug(Object tagStr, Object... objects) {
|
|
|
- String[] contents = wrapperContent(STACK_TRACE_INDEX_5, tagStr, objects);
|
|
|
+ if (!IS_INIT || !IS_SHOW_LOG) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String[] contents = wrapperContent(tagStr, objects);
|
|
|
+ if (contents == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
String tag = contents[0];
|
|
|
String msg = contents[1];
|
|
|
String headString = contents[2];
|
|
|
@@ -265,14 +272,16 @@ public final class Jlog {
|
|
|
}
|
|
|
|
|
|
private static void printFile(Object tagStr, File targetDirectory, String fileName, Object objectMsg) {
|
|
|
-
|
|
|
- if (!IS_SHOW_LOG) {
|
|
|
+ if (!IS_INIT && !IS_WRITE) {
|
|
|
return;
|
|
|
}
|
|
|
- if (!IS_INIT && isApkInDebug()) {
|
|
|
+ if (!IS_SHOW_LOG && isApkInDebug()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String[] contents = wrapperContent(tagStr, objectMsg);
|
|
|
+ if (contents == null) {
|
|
|
return;
|
|
|
}
|
|
|
- String[] contents = wrapperContent(STACK_TRACE_INDEX_5, tagStr, objectMsg);
|
|
|
String tag = contents[0];
|
|
|
String msg = contents[1];
|
|
|
String headString = contents[2];
|
|
|
@@ -280,12 +289,22 @@ public final class Jlog {
|
|
|
FileLog.printFile(tag, targetDirectory, fileName, headString, msg);
|
|
|
}
|
|
|
|
|
|
- private static String[] wrapperContent(int stackTraceIndex, Object tagStr, Object... objects) {
|
|
|
+ private static String[] wrapperContent(Object tagStr, Object... objects) {
|
|
|
|
|
|
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
|
|
-
|
|
|
- StackTraceElement targetElement = stackTrace[stackTraceIndex];
|
|
|
- String fileName = targetElement.getFileName();
|
|
|
+ String fileName = "";
|
|
|
+ StackTraceElement targetElement = null;
|
|
|
+ for (StackTraceElement element : stackTrace) {
|
|
|
+ String name = element.getFileName();
|
|
|
+ if (!"VMStack.java".equals(name) && !"Thread.java".equals(name) && !"Jlog.java".equals(name) && !"KtUtils.kt".equals(name)) {
|
|
|
+ fileName = name;
|
|
|
+ targetElement = element;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (targetElement == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
String str = "$";
|
|
|
if (fileName.contains(str)) {
|
|
|
fileName = fileName.split("\\$")[0];
|
|
|
@@ -334,7 +353,15 @@ public final class Jlog {
|
|
|
private static boolean isApkInDebug() {
|
|
|
try {
|
|
|
@SuppressLint("PrivateApi")
|
|
|
- Application application = (Application) Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null, (Object[]) null);
|
|
|
+ Class<?> activityThread = Class.forName("android.app.ActivityThread");
|
|
|
+ Object thread = activityThread.getMethod("currentActivityThread").invoke(null);
|
|
|
+ Object app = activityThread.getMethod("getApplication").invoke(thread);
|
|
|
+ Application application;
|
|
|
+ if (app == null) {
|
|
|
+ application = null;
|
|
|
+ } else {
|
|
|
+ application = (Application) app;
|
|
|
+ }
|
|
|
ApplicationInfo info = application.getApplicationInfo();
|
|
|
return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) == 0;
|
|
|
} catch (Exception e) {
|