浏览代码

日志打印方式更改

kth@mac 3 年之前
父节点
当前提交
c623bf0765

+ 7 - 0
app/build.gradle

@@ -1,4 +1,5 @@
 apply plugin: 'com.android.application'
+apply plugin: 'kotlin-android'
 
 android {
     compileSdkVersion 29
@@ -31,6 +32,10 @@ android {
             signingConfig signingConfigs.release
         }
     }
+//    compileOptions {
+//        sourceCompatibility 1.8
+//        targetCompatibility 1.8
+//    }
 }
 
 dependencies {
@@ -41,6 +46,8 @@ dependencies {
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
     implementation project(':loglib')
     implementation 'com.yanyi.benyanyi:PermissionHelper:1.1.4'
+    implementation "androidx.core:core-ktx:1.3.2"
+    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.30"
 }
 repositories {
     mavenCentral()

+ 0 - 21
app/src/main/java/com/mylove/logger/AppContext.java

@@ -1,21 +0,0 @@
-package com.mylove.logger;
-
-import android.app.Application;
-
-import com.benyanyi.loglib.Jlog;
-
-/**
- * @author myLove
- * @time 2017/11/16 19:11
- * @e-mail mylove.520.y@gmail.com
- * @overview
- */
-
-public class AppContext extends Application {
-    @Override
-    public void onCreate() {
-        super.onCreate();
-//        Jlog.init(BuildConfig.LOG_DEBUG);
-        Jlog.init(false, true);
-    }
-}

+ 19 - 0
app/src/main/java/com/mylove/logger/AppContext.kt

@@ -0,0 +1,19 @@
+package com.mylove.logger
+
+import android.app.Application
+import com.benyanyi.loglib.Jlog
+
+/**
+ * @author myLove
+ * @time 2017/11/16 19:11
+ * @e-mail mylove.520.y@gmail.com
+ * @overview
+ */
+class AppContext : Application() {
+    override fun onCreate() {
+        super.onCreate()
+        //        Jlog.init(BuildConfig.LOG_DEBUG);
+        Jlog.init(true, true)
+    }
+
+}

+ 0 - 67
app/src/main/java/com/mylove/logger/MainActivity.java

@@ -1,67 +0,0 @@
-package com.mylove.logger;
-
-import android.Manifest;
-import android.annotation.SuppressLint;
-import android.app.Application;
-import android.content.pm.ApplicationInfo;
-import android.os.Bundle;
-import android.widget.TextView;
-
-import androidx.annotation.Nullable;
-import androidx.appcompat.app.AppCompatActivity;
-
-import com.benyanyi.loglib.Jlog;
-import com.benyanyi.permissionlib.PermissionCallBack;
-import com.benyanyi.permissionlib.PermissionHelper;
-import com.benyanyi.permissionlib.msg.FailureMsg;
-
-/**
- * @author myLove
- * @time 2017/11/16 19:18
- * @e-mail mylove.520.y@gmail.com
- * @overview
- */
-
-public class MainActivity extends AppCompatActivity {
-    private TextView tv;
-
-    @Override
-    public void onCreate(@Nullable Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        setContentView(R.layout.activity_main);
-        tv = findViewById(R.id.text);
-//        tv.setText(isApkInDebug() + "");
-//        Jlog.v("测试" + isApkInDebug());
-        Jlog.d("测试");
-        Jlog.json("{\"name\":\"BeJson\",\"url\":\"http://www.bejson.com\",\"page\":88,\"isNonProfit\":true,\"address\":{\"street\":\"科技园路.\",\"city\":\"江苏苏州\",\"country\":\"中国\"},\"links\":[{\"name\":\"Google\",\"url\":\"http://www.google.com\"},{\"name\":\"Baidu\",\"url\":\"http://www.baidu.com\"},{\"name\":\"SoSo\",\"url\":\"http://www.SoSo.com\"}]}");
-        PermissionHelper.getInstance(this).hasPermission(0x10, new PermissionCallBack() {
-            @Override
-            public void onPermissionSuccess(int i) {
-                Jlog.writeFile("啊哈");
-                Jlog.errorWriteFile(new NullPointerException("异常"));
-            }
-
-            @Override
-            public void onPermissionFailure(FailureMsg failureMsg) {
-
-            }
-
-            @Override
-            public void onPermissionComplete(int i) {
-
-            }
-        }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
-    }
-
-    private boolean isApkInDebug() {
-        try {
-            @SuppressLint("PrivateApi")
-            Application application = (Application) Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null, (Object[]) null);
-            ApplicationInfo info = application.getApplicationInfo();
-            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) == 0;
-        } catch (Exception e) {
-            Jlog.e(e.getMessage());
-            return true;
-        }
-    }
-}

+ 51 - 0
app/src/main/java/com/mylove/logger/MainActivity.kt

@@ -0,0 +1,51 @@
+package com.mylove.logger
+
+import android.annotation.SuppressLint
+import android.app.Application
+import android.content.pm.ApplicationInfo
+import android.os.Bundle
+import android.widget.TextView
+import androidx.appcompat.app.AppCompatActivity
+import com.benyanyi.loglib.Jlog
+import com.benyanyi.loglib.v
+
+/**
+ * @author myLove
+ * @time 2017/11/16 19:18
+ * @e-mail mylove.520.y@gmail.com
+ * @overview
+ */
+class MainActivity : AppCompatActivity() {
+    private var tv: TextView? = null
+    public override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        setContentView(R.layout.activity_main)
+        tv = findViewById(R.id.text)
+        //        tv.setText(isApkInDebug() + "");
+//        Jlog.v("测试" + isApkInDebug());
+//        Jlog.d("测试")
+        "测试".v()
+        Jlog.json("{\"name\":\"BeJson\",\"url\":\"http://www.bejson.com\",\"page\":88,\"isNonProfit\":true,\"address\":{\"street\":\"科技园路.\",\"city\":\"江苏苏州\",\"country\":\"中国\"},\"links\":[{\"name\":\"Google\",\"url\":\"http://www.google.com\"},{\"name\":\"Baidu\",\"url\":\"http://www.baidu.com\"},{\"name\":\"SoSo\",\"url\":\"http://www.SoSo.com\"}]}")
+//        PermissionHelper.getInstance(this).hasPermission(0x10, object : PermissionCallBack {
+//            override fun onPermissionSuccess(i: Int) {
+//                Jlog.writeFile("啊哈")
+//                Jlog.errorWriteFile(NullPointerException("异常"))
+//            }
+//
+//            override fun onPermissionFailure(failureMsg: FailureMsg) {}
+//            override fun onPermissionComplete(i: Int) {}
+//        }, Manifest.permission.WRITE_EXTERNAL_STORAGE)
+    }
+
+    private val isApkInDebug: Boolean
+        get() = try {
+            @SuppressLint("PrivateApi") val application =
+                Class.forName("android.app.ActivityThread").getMethod("currentApplication")
+                    .invoke(null) as Application
+            val info = application.applicationInfo
+            info.flags and ApplicationInfo.FLAG_DEBUGGABLE == 0
+        } catch (e: Exception) {
+            Jlog.e(e.message)
+            true
+        }
+}

+ 2 - 0
build.gradle

@@ -1,6 +1,7 @@
 // Top-level build file where you can add configuration options common to all sub-projects/modules.
 
 buildscript {
+    ext.kotlin_version = '1.5.30'
 
     repositories {
         google()
@@ -10,6 +11,7 @@ buildscript {
     }
     dependencies {
         classpath 'com.android.tools.build:gradle:4.1.3'
+        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
 //        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
 //        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.2'
 

+ 7 - 1
loglib/build.gradle

@@ -1,4 +1,5 @@
 apply plugin: 'com.android.library'
+apply plugin: 'kotlin-android'
 //apply plugin: 'com.github.dcendents.android-maven'
 
 android {
@@ -32,5 +33,10 @@ dependencies {
     testImplementation 'junit:junit:4.13'
     androidTestImplementation 'androidx.test.ext:junit:1.1.2'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
+    implementation "androidx.core:core-ktx:1.3.2"
+    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.30"
 }
-apply from: 'bintrayUpload.gradle'
+apply from: 'bintrayUpload.gradle'
+repositories {
+    mavenCentral()
+}

+ 29 - 28
loglib/src/main/java/com/benyanyi/loglib/BaseLog.java

@@ -46,30 +46,30 @@ class BaseLog {
     }
 
     private static void printSub(LogType type, String tag, String sub) {
-        switch (type) {
-            case V:
-                Log.v(tag, sub);
-                break;
-            case D:
-                Log.d(tag, sub);
-                break;
-            case I:
-                Log.i(tag, sub);
-                break;
-            case W:
-                Log.w(tag, sub);
-                break;
-            case E:
-                Log.e(tag, sub);
-                break;
-            case A:
-                Log.wtf(tag, sub);
-                break;
-            case WRITE:
-                writeLog(tag, sub);
-                break;
+        if (Jlog.IS_SHOW_LOG || Jlog.IS_INIT) {
+            switch (type) {
+                case V:
+                    Log.v(tag, sub);
+                    break;
+                case D:
+                    Log.d(tag, sub);
+                    break;
+                case I:
+                    Log.i(tag, sub);
+                    break;
+                case W:
+                    Log.w(tag, sub);
+                    break;
+                case E:
+                    Log.e(tag, sub);
+                    break;
+                case A:
+                    Log.wtf(tag, sub);
+                    break;
+            }
         }
         if (Jlog.IS_WRITE) {
+
             writeLog(tag, sub);
         }
     }
@@ -127,17 +127,17 @@ class BaseLog {
             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);// HH:mm:ss
             //获取当前时间
             Date date = new Date(System.currentTimeMillis());
-            File logFile = new File(logPath.toString() + "/" + simpleDateFormat.format(date) + ".txt");
-            long size = getFileSizes(logFile);
-            if (size > 1024 * 1024 * 2) {
-                logFile.delete();
-            }
+            File logFile = new File(logPath + "/" + simpleDateFormat.format(date) + ".txt");
+//            long size = getFileSizes(logFile);
+//            if (size > 1024 * 1024 * 2) {
+//                logFile.delete();
+//            }
             try {
                 if (logPath.exists() && !logFile.exists()) {
                     logFile.createNewFile();
                 }
             } catch (Exception e) {
-                e.printStackTrace();
+//                e.printStackTrace();
             }
             //String size=FileUtils.getFileSize(logFile);
             //if(){};
@@ -169,6 +169,7 @@ class BaseLog {
                 e.printStackTrace();
             }
             try {
+                assert fis != null;
                 s = fis.available();
             } catch (Exception e) {
                 e.printStackTrace();

+ 83 - 56
loglib/src/main/java/com/benyanyi/loglib/Jlog.java

@@ -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) {

+ 0 - 3
loglib/src/main/java/com/benyanyi/loglib/JsonLog.java

@@ -40,8 +40,5 @@ class JsonLog {
             Log.d(tag, "║ " + line);
         }
         LogUtil.printLine(tag, false);
-        if (Jlog.IS_WRITE) {
-            Jlog.writeFile(tag, msg);
-        }
     }
 }

+ 15 - 0
loglib/src/main/java/com/benyanyi/loglib/KtUtils.kt

@@ -0,0 +1,15 @@
+package com.benyanyi.loglib
+
+/**
+ * @author kth
+ * @date 2022/7/27 11:16 上午
+ * @user yanyi
+ * @overview
+ */
+fun Any.v() {
+    Jlog.v(this)
+}
+
+fun Any.d() {
+    Jlog.d(this)
+}

+ 1 - 1
loglib/src/main/java/com/benyanyi/loglib/LogType.java

@@ -7,5 +7,5 @@ package com.benyanyi.loglib;
  * @overview
  */
 enum LogType {
-    V, D, I, W, E, A, JSON, XML, WRITE;
+    V, D, I, W, E, A, JSON, XML;
 }

+ 0 - 3
loglib/src/main/java/com/benyanyi/loglib/XmlLog.java

@@ -36,9 +36,6 @@ class XmlLog {
             }
         }
         LogUtil.printLine(tag, false);
-        if (Jlog.IS_WRITE) {
-            Jlog.writeFile(tag, xml);
-        }
     }
 
     private static String formatXML(String inputXML) {