Bladeren bron

feature:zap模块采集日志开发

#Suyghur 4 jaren geleden
bovenliggende
commit
eee7284b7a

+ 2 - 0
demo/build.gradle

@@ -74,4 +74,6 @@ android {
 dependencies {
 //    implementation project(':library_caps')
     implementation project(':library_zap')
+    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
+
 }

+ 3 - 0
demo/src/main/java/com/suyghur/dolin/simple/DemoActivity.kt

@@ -5,6 +5,9 @@ import android.os.Bundle
 import android.view.View
 import android.widget.Button
 import android.widget.LinearLayout
+import com.suyghur.dolin.zap.Zap
+import com.suyghur.dolin.zap.entity.Level
+import com.suyghur.dolin.zap.util.LevelUtils
 
 /**
  * @author #Suyghur.

+ 10 - 0
demo/src/main/java/com/suyghur/dolin/simple/MyLifecycleObserver.kt

@@ -0,0 +1,10 @@
+package com.suyghur.dolin.simple
+
+import androidx.lifecycle.LifecycleObserver
+
+/**
+ * @author #Suyghur.
+ * Created on 4/8/21
+ */
+class MyLifecycleObserver: LifecycleObserver {
+}

+ 3 - 0
library_zap/build.gradle

@@ -55,4 +55,7 @@ android {
 dependencies {
     implementation "org.jetbrains.kotlin:kotlin-stdlib:$KOTLIN_VERSION"
     implementation 'androidx.core:core-ktx:1.3.2'
+//    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
+    implementation 'androidx.fragment:fragment-ktx:1.3.2'
+
 }

+ 3 - 3
library_zap/src/main/cpp/includes/buffer.h

@@ -2,8 +2,8 @@
 // Created by #Suyghur, on 4/7/21.
 //
 
-#ifndef DOLIN_BUFFER_H
-#define DOLIN_BUFFER_H
+#ifndef DOLIN_ZAP_BUFFER_H
+#define DOLIN_ZAP_BUFFER_H
 
 #include <string>
 #include <mutex>
@@ -71,4 +71,4 @@ private:
 };
 
 
-#endif //DOLIN_BUFFER_H
+#endif //DOLIN_ZAP_BUFFER_H

+ 3 - 3
library_zap/src/main/cpp/includes/buffer_flush.h

@@ -2,8 +2,8 @@
 // Created by #Suyghur, on 4/7/21.
 //
 
-#ifndef DOLIN_BUFFER_FLUSH_H
-#define DOLIN_BUFFER_FLUSH_H
+#ifndef DOLIN_ZAP_BUFFER_FLUSH_H
+#define DOLIN_ZAP_BUFFER_FLUSH_H
 
 
 #include <cstdio>
@@ -38,4 +38,4 @@ private:
 };
 
 
-#endif //DOLIN_BUFFER_FLUSH_H
+#endif //DOLIN_ZAP_BUFFER_FLUSH_H

+ 3 - 3
library_zap/src/main/cpp/includes/buffer_header.h

@@ -2,8 +2,8 @@
 // Created by #Suyghur, on 4/7/21.
 //
 
-#ifndef DOLIN_BUFFER_HEADER_H
-#define DOLIN_BUFFER_HEADER_H
+#ifndef DOLIN_ZAP_BUFFER_HEADER_H
+#define DOLIN_ZAP_BUFFER_HEADER_H
 
 #include <string>
 
@@ -57,4 +57,4 @@ namespace buffer_header {
 }
 
 
-#endif //DOLIN_BUFFER_HEADER_H
+#endif //DOLIN_ZAP_BUFFER_HEADER_H

+ 3 - 3
library_zap/src/main/cpp/includes/file_flush.h

@@ -2,8 +2,8 @@
 // Created by #Suyghur, on 4/7/21.
 //
 
-#ifndef DOLIN_FILE_FLUSH_H
-#define DOLIN_FILE_FLUSH_H
+#ifndef DOLIN_ZAP_FILE_FLUSH_H
+#define DOLIN_ZAP_FILE_FLUSH_H
 
 
 #include "buffer_flush.h"
@@ -33,4 +33,4 @@ private:
 };
 
 
-#endif //DOLIN_FILE_FLUSH_H
+#endif //DOLIN_ZAP_FILE_FLUSH_H

+ 12 - 11
library_zap/src/main/java/com/suyghur/dolin/zap/Zap.kt

@@ -1,7 +1,8 @@
 package com.suyghur.dolin.zap
 
+import android.app.Application
 import com.suyghur.dolin.zap.entity.Config
-import com.suyghur.dolin.zap.impl.LoggerPrint
+import com.suyghur.dolin.zap.impl.ZapPrintImpl
 
 /**
  * @author #Suyghur.
@@ -9,39 +10,39 @@ import com.suyghur.dolin.zap.impl.LoggerPrint
  */
 object Zap {
 
-    fun initialize(config: Config) {
-        LoggerPrint.getInstance().initialize(config)
+    fun initialize(application: Application,config: Config) {
+        ZapPrintImpl.getInstance().initialize(application,config)
     }
 
     fun d(any: Any?) {
-        LoggerPrint.getInstance().d(any)
+        ZapPrintImpl.getInstance().d(any)
     }
 
     fun d(tag: String, any: Any?) {
-        LoggerPrint.getInstance().d(tag, any)
+        ZapPrintImpl.getInstance().d(tag, any)
     }
 
     fun i(any: Any?) {
-        LoggerPrint.getInstance().i(any)
+        ZapPrintImpl.getInstance().i(any)
     }
 
     fun i(tag: String, any: Any?) {
-        LoggerPrint.getInstance().i(tag, any)
+        ZapPrintImpl.getInstance().i(tag, any)
     }
 
     fun w(any: Any?) {
-        LoggerPrint.getInstance().w(any)
+        ZapPrintImpl.getInstance().w(any)
     }
 
     fun w(tag: String, any: Any?) {
-        LoggerPrint.getInstance().w(tag, any)
+        ZapPrintImpl.getInstance().w(tag, any)
     }
 
     fun e(any: Any?) {
-        LoggerPrint.getInstance().e(any)
+        ZapPrintImpl.getInstance().e(any)
     }
 
     fun e(tag: String, any: Any?) {
-        LoggerPrint.getInstance().e(tag, any)
+        ZapPrintImpl.getInstance().e(tag, any)
     }
 }

+ 4 - 7
library_zap/src/main/java/com/suyghur/dolin/zap/entity/Config.kt

@@ -1,6 +1,5 @@
 package com.suyghur.dolin.zap.entity
 
-import android.content.Context
 import android.text.TextUtils
 
 /**
@@ -43,9 +42,9 @@ class Config private constructor(builder: Builder) {
                 '}'
     }
 
-    class Builder(context: Context) {
-        internal var logDir = context.getExternalFilesDir("dolin")!!.absolutePath
-        internal var defaultTag = "dolin_logger"
+    class Builder() {
+        internal var logDir = ""
+        internal var defaultTag = "dolin_zap"
         internal var logcatLevel = Level.DEBUG
         internal var recordLevel = Level.DEBUG
         internal var recordEnable = true
@@ -56,9 +55,7 @@ class Config private constructor(builder: Builder) {
          * 日志存储路径,默认是应用的私有目录下dolin文件夹
          */
         fun setLogDir(logDir: String): Builder {
-            if (!TextUtils.isEmpty(logDir)) {
-                this.logDir = logDir
-            }
+            this.logDir = logDir
             return this
         }
 

+ 50 - 0
library_zap/src/main/java/com/suyghur/dolin/zap/impl/DateFileFormatter.kt

@@ -0,0 +1,50 @@
+package com.suyghur.dolin.zap.impl
+
+import android.text.TextUtils
+import com.suyghur.dolin.zap.entity.Level
+import com.suyghur.dolin.zap.internal.IFormatter
+import com.suyghur.dolin.zap.util.LevelUtils
+import java.text.SimpleDateFormat
+import java.util.*
+
+/**
+ * @author #Suyghur.
+ * Created on 4/8/21
+ */
+class DateFileFormatter : IFormatter {
+
+    private val pattern = "yyy-MM-dd HH:mm:ss"
+    private var simpleDateFormat: SimpleDateFormat = SimpleDateFormat(pattern, Locale.getDefault())
+    private var date: Date
+    private var lastDateFormatted = ""
+    private var stringBuilder: StringBuilder = StringBuilder()
+    private var timeLength = 0
+
+    init {
+        Calendar.getInstance()[Calendar.SECOND] = 0
+        date = Calendar.getInstance().time
+    }
+
+    private fun resetTimePrefix() {
+        if (stringBuilder.isNotEmpty()) {
+            stringBuilder.delete(0, stringBuilder.length)
+        }
+        timeLength = stringBuilder.append(lastDateFormatted).append(" ").length
+    }
+
+    private fun formatString(level: Level, tag: String, msg: String): String {
+        if (stringBuilder.length > timeLength) {
+            stringBuilder.delete(timeLength, stringBuilder.length)
+        }
+        return stringBuilder.append("[${LevelUtils.getShortLevelName(level)}").append(tag).append(": ").append(msg).append("\n").toString()
+    }
+
+    override fun format(level: Level, tag: String, msg: String): String {
+        if ((System.currentTimeMillis() - date.time) > 1000 || TextUtils.isEmpty(lastDateFormatted)) {
+            date.time = System.currentTimeMillis()
+            lastDateFormatted = simpleDateFormat.format(date)
+            resetTimePrefix()
+        }
+        return formatString(level, tag, msg)
+    }
+}

+ 54 - 0
library_zap/src/main/java/com/suyghur/dolin/zap/impl/ZapLifecycle.kt

@@ -0,0 +1,54 @@
+package com.suyghur.dolin.zap.impl
+
+import android.app.Activity
+import android.app.Application
+import android.os.Bundle
+
+/**
+ * @author #Suyghur.
+ * Created on 4/8/21
+ */
+object ZapLifecycle {
+
+    fun registerZapLifeCallback(application: Application, record2MMap: Record2MMap) {
+        application.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
+            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
+            }
+
+            override fun onActivityStarted(activity: Activity) {
+            }
+
+            override fun onActivityResumed(activity: Activity) {
+            }
+
+            override fun onActivityPaused(activity: Activity) {
+                record2MMap.asyncFlush()
+            }
+
+            override fun onActivityStopped(activity: Activity) {
+            }
+
+            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
+                record2MMap.asyncFlush()
+            }
+
+            override fun onActivityDestroyed(activity: Activity) {
+                record2MMap.asyncFlush()
+            }
+
+        })
+    }
+
+//    private fun initFragmentLifeCallback(): FragmentManager.FragmentLifecycleCallbacks {
+//        return object : FragmentManager.FragmentLifecycleCallbacks() {
+//            override fun onFragmentCreated(fm: FragmentManager, f: Fragment, savedInstanceState: Bundle?) {
+//                super.onFragmentCreated(fm, f, savedInstanceState)
+//            }
+//
+//            override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
+//                super.onFragmentResumed(fm, f)
+//            }
+//
+//        }
+//    }
+}

+ 25 - 9
library_zap/src/main/java/com/suyghur/dolin/zap/impl/LoggerPrint.kt → library_zap/src/main/java/com/suyghur/dolin/zap/impl/ZapPrintImpl.kt

@@ -1,5 +1,7 @@
 package com.suyghur.dolin.zap.impl
 
+import android.app.Application
+import android.text.TextUtils
 import android.util.Log
 import androidx.annotation.Keep
 import com.suyghur.dolin.zap.entity.Config
@@ -12,23 +14,33 @@ import java.lang.reflect.Array
  * @author #Suyghur.
  * Created on 4/7/21
  */
-class LoggerPrint : ILogger {
+class ZapPrintImpl : ILogger {
 
     private var hasInitialized: Boolean = false
     private var tag = ""
+    private var logDir = ""
     private var logcatLevel = Level.NONE
     private var recordLevel = Level.NONE
     private var record2MMap: Record2MMap? = null
+    private var dateFileFormatter: DateFileFormatter = DateFileFormatter()
 
-    fun initialize(config: Config) {
+    fun initialize(application: Application, config: Config) {
         if (hasInitialized) {
             throw IllegalArgumentException("Logger already initialize")
         }
+
+        logDir = if (TextUtils.isEmpty(config.logDir)) {
+            application.getExternalFilesDir("dolin/zap")!!.absolutePath
+        } else {
+            config.logDir
+        }
+
         this.tag = config.defaultTag
         this.logcatLevel = config.logcatLevel
         this.recordLevel = config.recordLevel
         if (config.recordEnable) {
-//            record2MMap = Record2MMap(config.logDir)
+            record2MMap = Record2MMap(config.logDir, 1, "", false)
+            ZapLifecycle.registerZapLifeCallback(application, record2MMap!!)
         }
     }
 
@@ -64,9 +76,9 @@ class LoggerPrint : ILogger {
         print(Level.ERROR, tag, any)
     }
 
-    override fun record(level: Int, tag: String, msg: String) {
-        TODO("Not yet implemented")
-    }
+//    override fun record(level: Int, tag: String, msg: String) {
+//        TODO("Not yet implemented")
+//    }
 
     private fun interceptLogcat(level: Level): Boolean {
         return level.ordinal < logcatLevel.ordinal
@@ -102,7 +114,7 @@ class LoggerPrint : ILogger {
             printInner(data)
         }
         if (!interceptRecord(level)) {
-            recordLevel
+            doRecord(data)
         }
         data.recycle()
     }
@@ -132,18 +144,22 @@ class LoggerPrint : ILogger {
         }
     }
 
+    private fun doRecord(data: ZapData) {
+        record2MMap?.write(dateFileFormatter.format(data.level, data.tag, data.msg))
+    }
+
     companion object {
 
         const val MAX_LENGTH_OF_SINGLE_MESSAGE = 4063
 
         @Keep
         @JvmStatic
-        fun getInstance(): LoggerPrint {
+        fun getInstance(): ZapPrintImpl {
             return LoggerPrintHolder.INSTANCE
         }
 
         private object LoggerPrintHolder {
-            val INSTANCE = LoggerPrint()
+            val INSTANCE = ZapPrintImpl()
         }
 
         /**

+ 11 - 0
library_zap/src/main/java/com/suyghur/dolin/zap/internal/IFormatter.kt

@@ -0,0 +1,11 @@
+package com.suyghur.dolin.zap.internal
+
+import com.suyghur.dolin.zap.entity.Level
+
+/**
+ * @author #Suyghur.
+ * Created on 4/8/21
+ */
+interface IFormatter {
+    fun format(level: Level, tag: String, msg: String): String
+}

+ 1 - 1
library_zap/src/main/java/com/suyghur/dolin/zap/internal/ILogger.kt

@@ -23,6 +23,6 @@ interface ILogger {
 
     fun e(tag: String, any: Any?)
 
-    fun record(level: Int, tag: String, msg: String)
+//    fun record(level: Int, tag: String, msg: String)
 
 }

+ 29 - 0
library_zap/src/main/java/com/suyghur/dolin/zap/util/LevelUtils.kt

@@ -0,0 +1,29 @@
+package com.suyghur.dolin.zap.util
+
+import com.suyghur.dolin.zap.entity.Level
+
+/**
+ * @author #Suyghur.
+ * Created on 4/8/21
+ */
+object LevelUtils {
+
+    fun getLevelName(level: Level): String {
+        return level.name
+    }
+
+    fun getShortLevelName(level: Level): String {
+        return when (level) {
+            Level.DEBUG -> "D"
+            Level.INFO -> "I"
+            Level.WARNING -> "W"
+            Level.ERROR -> "E"
+            else ->
+                if (level.ordinal < Level.DEBUG.ordinal) {
+                    "D"
+                } else {
+                    "E"
+                }
+        }
+    }
+}