Jelajahi Sumber

v1.0.1开发:调整zap模块record的逻辑

#Suyghur 3 tahun lalu
induk
melakukan
a642bff021

+ 3 - 0
demo/build.gradle

@@ -78,4 +78,7 @@ dependencies {
     implementation project(':library_comm')
     implementation project(':library_zap')
     implementation project(':library_crashlytics')
+
+    debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
+
 }

+ 1 - 1
library_comm/src/main/cpp/r4log/r4log_handler.cpp

@@ -57,7 +57,7 @@ static char *OpenMMap(int buffer_fd, size_t buffer_size, FileFlush *fileFlush) {
     return map_ptr;
 }
 
-static jlong InitNative(JNIEnv *env, jclass thiz, jstring buffer_path, jstring log_path, jint capacity, jint limit_size, jboolean compress) {
+static jlong InitNative(JNIEnv *env, jobject thiz, jstring buffer_path, jstring log_path, jint capacity, jint limit_size, jboolean compress) {
 
     const char *_buffer_path = env->GetStringUTFChars(buffer_path, JNI_FALSE);
     const char *_log_path = env->GetStringUTFChars(log_path, JNI_FALSE);

+ 7 - 86
library_comm/src/main/java/com/dolin/comm/impl/R4LogHandler.java

@@ -1,105 +1,26 @@
 package com.dolin.comm.impl;
 
 
-import com.dolin.comm.internal.IRecord;
-import com.dolin.comm.util.LogFileUtils;
-
-import java.io.File;
-
 /**
  * @author #Suyghur.
  * Created on 2021/05/19
  */
-public class R4LogHandler implements IRecord {
-
-    private long ptr = 0L;
-    private String logPath = "";
-    private String logDate = "";
-    private String logFilePath = "";
-    private int limitSize = 0;
-    private int num = 1;
+public class R4LogHandler{
 
     static {
         System.loadLibrary("dolin-r4log");
     }
 
-    public R4LogHandler(String bufferPath, String logPath, String logDate, int capacity, int limitSize, boolean compress) {
-        this.num = LogFileUtils.getLogFileNumByDate(logPath, logDate);
-        this.logPath = logPath;
-        this.logDate = logDate;
-        this.limitSize = limitSize;
-        if (num > 1) {
-            this.logFilePath = logPath + File.separator + logDate + "-p" + num + ".zap";
-        } else {
-            this.logFilePath = logPath + File.separator + logDate + ".zap";
-        }
-        try {
-            this.ptr = initNative(bufferPath, logFilePath, capacity, limitSize, compress);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    @Override
-    public void write(String msg) {
-        if (ptr != 0L) {
-            try {
-                writeNative(ptr, msg);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
-    }
-
-    @Override
-    public void asyncFlush() {
-        if (ptr != 0L) {
-            try {
-                //自动扩容
-                if (isLogFileOverSizeNative(ptr)) {
-                    this.num += 1;
-                    this.logFilePath = logPath + File.separator + logDate + "-p" + num + ".zap";
-                    expLogFileNative(ptr, logFilePath, limitSize);
-                }
-                asyncFlushNative(ptr);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
-    }
-
-    @Override
-    public void expLogFile(String path, int limitSize) {
-        if (ptr != 0L) {
-            try {
-                expLogFileNative(ptr, path, limitSize);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
-    }
-
-    @Override
-    public void release() {
-        if (ptr != 0L) {
-            try {
-                releaseNative(ptr);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
-    }
-
-    private static native long initNative(String bufferPath, String logFilePath, int capacity, int limitSize, boolean compress);
+    public native long initNative(String bufferPath, String logFilePath, int capacity, int limitSize, boolean compress);
 
-    private native void writeNative(long ptr, String msg);
+    public native void writeNative(long ptr, String msg);
 
-    private native void asyncFlushNative(long ptr);
+    public native void asyncFlushNative(long ptr);
 
-    private native void expLogFileNative(long ptr, String path, int limitSize);
+    public native void expLogFileNative(long ptr, String path, int limitSize);
 
-    private native void releaseNative(long ptr);
+    public native void releaseNative(long ptr);
 
-    private native boolean isLogFileOverSizeNative(long ptr);
+    public native boolean isLogFileOverSizeNative(long ptr);
 
 }

+ 0 - 1
library_crashlytics/src/main/java/com/dolin/crashlytics/Crashlytics.java

@@ -3,7 +3,6 @@ package com.dolin.crashlytics;
 import android.app.Application;
 
 import com.dolin.crashlytics.handler.JavaCrashHandler;
-import com.dolin.crashlytics.impl.NativeBridge;
 import com.dolin.crashlytics.internal.ICrashHandler;
 import com.dolin.crashlytics.utils.FileUtils;
 

+ 0 - 1
library_crashlytics/src/main/java/com/dolin/crashlytics/handler/JavaCrashHandler.java

@@ -5,7 +5,6 @@ import android.os.Process;
 import android.util.Log;
 
 import com.dolin.comm.util.AppInfoUtils;
-import com.dolin.crashlytics.impl.NativeBridge;
 import com.dolin.crashlytics.internal.ICrashHandler;
 import com.dolin.crashlytics.monitor.ActivityMonitor;
 import com.dolin.crashlytics.utils.LogUtils;

+ 39 - 0
library_crashlytics/src/main/java/com/dolin/crashlytics/impl/CrashRecord.java

@@ -0,0 +1,39 @@
+package com.dolin.crashlytics.impl;
+
+import android.app.Application;
+
+import com.dolin.comm.impl.R4LogHandler;
+import com.dolin.crashlytics.utils.FileUtils;
+
+import java.io.File;
+
+/**
+ * @author #Suyghur.
+ * Created on 2021/07/30
+ */
+public class CrashRecord {
+
+    private R4LogHandler handler=null;
+
+    private CrashRecord() {
+
+    }
+
+    private CrashRecord getInstance() {
+        return CrashRecordHolder.INSTANCE;
+    }
+
+    public void init(Application application) {
+        String logFolderDir = FileUtils.getLogFolderDir(application);
+
+        String bufferPath = logFolderDir + File.separator + "crashlytics.cache";
+//        String logPath = FileUtils.getLogDir(logFolderDir, date);
+        String logPath = logFolderDir + File.separator + "tmp.crashlytics";
+//        handler=R4LogHandler(logFolderDir)
+    }
+
+
+    private static class CrashRecordHolder {
+        private static final CrashRecord INSTANCE = new CrashRecord();
+    }
+}

+ 0 - 49
library_crashlytics/src/main/java/com/dolin/crashlytics/impl/NativeBridge.java

@@ -1,49 +0,0 @@
-package com.dolin.crashlytics.impl;
-
-/**
- * @author #Suyghur.
- * Created on 2021/07/30
- */
-public class NativeBridge {
-
-    static {
-        System.loadLibrary("dolin-crashlytics");
-    }
-
-    private long ptr = 0L;
-
-    private NativeBridge() {
-
-    }
-
-
-    public static NativeBridge getBridge() {
-        return NativeBridgeHolder.INSTANCE;
-    }
-
-    public void init(String bufferPath, String logFilePath, int capacity, int limitSize, boolean compress) {
-        this.ptr = initNative(bufferPath, logFilePath, capacity, limitSize, compress);
-    }
-
-    public void write(String msg) {
-        writeNative(ptr, msg);
-    }
-
-
-    private native long initNative(String bufferPath, String logFilePath, int capacity, int limitSize, boolean compress);
-
-    private native void writeNative(long ptr, String msg);
-
-    private native void asyncFlushNative(long ptr);
-
-    private native void releaseNative(long ptr);
-
-    private static native void onNativeCrash();
-
-    private static native void nativeTestCrash(int runInNewThread);
-
-
-    private static class NativeBridgeHolder {
-        private static final NativeBridge INSTANCE = new NativeBridge();
-    }
-}

+ 16 - 14
library_zap/src/main/java/com/dolin/zap/impl/ZapPrint.java

@@ -4,7 +4,6 @@ import android.app.Application;
 import android.text.TextUtils;
 import android.util.Log;
 
-import com.dolin.comm.impl.R4LogHandler;
 import com.dolin.zap.entity.Config;
 import com.dolin.zap.entity.Level;
 import com.dolin.zap.entity.ZapData;
@@ -27,10 +26,11 @@ public class ZapPrint implements IPrint {
 
     private static final int MAX_LENGTH_OF_SINGLE_MESSAGE = 4063;
     private boolean hasInitialized = false;
+    private boolean recordEnable = true;
     private String tag = "";
     private Level logcatLevel = Level.DEBUG;
     private Level recordLevel = Level.DEBUG;
-    private R4LogHandler r4logHandler = null;
+    //    private R4LogHandler r4logHandler = null;
     private DateFileFormatter dateFileFormatter = null;
 
     private ZapPrint() {
@@ -52,6 +52,11 @@ public class ZapPrint implements IPrint {
         if (hasInitialized) {
             return;
         }
+
+        this.logcatLevel = config.logcatLevel;
+        this.recordLevel = config.recordLevel;
+        this.recordEnable = config.recordEnable;
+
         String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
 
         String logFolderDir = "";
@@ -67,15 +72,12 @@ public class ZapPrint implements IPrint {
             this.tag = config.tag;
         }
 
-        this.logcatLevel = config.logcatLevel;
-        this.recordLevel = config.recordLevel;
-
         String bufferPath = logFolderDir + File.separator + "zap.cache";
         String logPath = LogFileUtils.getLogDir(logFolderDir, date);
 
-        if (config.recordEnable) {
-            r4logHandler = new R4LogHandler(bufferPath, logPath, date, 1024 * 400, config.fileSizeLimitDayByte, config.compressEnable);
-            ZapLifecycle.getInstance().registerZapLifeCallback(application, r4logHandler);
+        if (recordEnable) {
+            ZapRecord.getInstance().init(bufferPath, logPath, date, 1024 * 400, config.fileSizeLimitDayByte, config.compressEnable);
+            ZapLifecycle.getInstance().registerZapLifeCallback(application);
         }
 
         new Thread(new Runnable() {
@@ -205,17 +207,17 @@ public class ZapPrint implements IPrint {
     }
 
     private void doRecord(ZapData data) {
-        if (r4logHandler != null) {
-            r4logHandler.write(dateFileFormatter.format(data.level, data.tag, data.msg));
+        if (recordEnable) {
+            ZapRecord.getInstance().write(dateFileFormatter.format(data.level, data.tag, data.msg));
         }
     }
 
     public void recycle() {
-        if (r4logHandler != null) {
-            ZapLifecycle.getInstance().unregisterZapLifeCallback();
-            r4logHandler.asyncFlush();
-            r4logHandler.release();
+        ZapLifecycle.getInstance().unregisterZapLifeCallback();
+        if (recordEnable) {
+            ZapRecord.getInstance().asyncFlush();
         }
+        ZapRecord.getInstance().release();
     }
 
     private static final class ZapPrintHolder {

+ 105 - 0
library_zap/src/main/java/com/dolin/zap/impl/ZapRecord.java

@@ -0,0 +1,105 @@
+package com.dolin.zap.impl;
+
+import com.dolin.comm.impl.R4LogHandler;
+import com.dolin.comm.internal.IRecord;
+import com.dolin.comm.util.LogFileUtils;
+
+import java.io.File;
+
+/**
+ * @author #Suyghur.
+ * Created on 2021/08/05
+ */
+public class ZapRecord implements IRecord {
+
+    private long ptr = 0L;
+    private String logPath = "";
+    private String logDate = "";
+    private String logFilePath = "";
+    private int limitSize = 0;
+    private int num = 1;
+    private R4LogHandler handler = null;
+
+    private ZapRecord() {
+
+    }
+
+    public static ZapRecord getInstance() {
+        return ZapRecordHolder.INSTANCE;
+    }
+
+    public void init(String bufferPath, String logPath, String logDate, int capacity, int limitSize, boolean compress) {
+        this.num = LogFileUtils.getLogFileNumByDate(logPath, logDate);
+        this.logPath = logPath;
+        this.logDate = logDate;
+        this.limitSize = limitSize;
+        if (num > 1) {
+            this.logFilePath = logPath + File.separator + logDate + "-p" + num + ".zap";
+        } else {
+            this.logFilePath = logPath + File.separator + logDate + ".zap";
+        }
+        try {
+            this.handler = new R4LogHandler();
+            this.ptr = handler.initNative(bufferPath, logFilePath, capacity, limitSize, compress);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    @Override
+    public void write(String msg) {
+        if (ptr != 0L && handler != null) {
+            try {
+                handler.writeNative(ptr, msg);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    @Override
+    public void asyncFlush() {
+        if (ptr != 0L && handler != null) {
+            try {
+                //自动扩容
+                if (handler.isLogFileOverSizeNative(ptr)) {
+                    this.num += 1;
+                    this.logFilePath = logPath + File.separator + logDate + "-p" + num + ".zap";
+                    handler.expLogFileNative(ptr, logFilePath, limitSize);
+                }
+                handler.asyncFlushNative(ptr);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    @Override
+    public void expLogFile(String path, int limitSize) {
+        if (ptr != 0L && handler != null) {
+            try {
+                handler.expLogFileNative(ptr, path, limitSize);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    @Override
+    public void release() {
+        if (handler != null) {
+            try {
+                if (ptr != 0L) {
+                    handler.releaseNative(ptr);
+                }
+                handler = null;
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    private static class ZapRecordHolder {
+        private static final ZapRecord INSTANCE = new ZapRecord();
+    }
+}

+ 3 - 10
library_zap/src/main/java/com/dolin/zap/lifecycle/ZapLifecycle.java

@@ -4,7 +4,7 @@ import android.app.Activity;
 import android.app.Application;
 import android.os.Bundle;
 
-import com.dolin.comm.impl.R4LogHandler;
+import com.dolin.zap.impl.ZapRecord;
 
 /**
  * @author #Suyghur.
@@ -15,7 +15,6 @@ public class ZapLifecycle {
     private static volatile ZapLifecycle mInstance = null;
 
     private Application application = null;
-    private R4LogHandler r4LogHandler = null;
 
 
     private final Application.ActivityLifecycleCallbacks callback = new Application.ActivityLifecycleCallbacks() {
@@ -36,9 +35,7 @@ public class ZapLifecycle {
 
         @Override
         public void onActivityPaused(Activity activity) {
-            if (r4LogHandler != null) {
-                r4LogHandler.asyncFlush();
-            }
+            ZapRecord.getInstance().asyncFlush();
         }
 
         @Override
@@ -73,9 +70,8 @@ public class ZapLifecycle {
         return mInstance;
     }
 
-    public void registerZapLifeCallback(Application application, R4LogHandler r4LogHandler) {
+    public void registerZapLifeCallback(Application application) {
         this.application = application;
-        this.r4LogHandler = r4LogHandler;
         application.registerActivityLifecycleCallbacks(callback);
     }
 
@@ -84,8 +80,5 @@ public class ZapLifecycle {
             application.unregisterActivityLifecycleCallbacks(callback);
             this.application = null;
         }
-        if (r4LogHandler != null) {
-            this.r4LogHandler = null;
-        }
     }
 }