소스 검색

feature:
1)更新README
2)调整Zap模块Zap中逻辑

#Suyghur 4 년 전
부모
커밋
dfc3fc576c

+ 7 - 1
README.md

@@ -1,2 +1,8 @@
-# Med
+# Dolin
+
 an application performance monitor framework of Android
+
+## Zap
+
+a log framework of Android , [Detail](library_zap/README.md)
+

+ 1 - 1
demo/build.gradle

@@ -12,7 +12,7 @@ android {
     buildToolsVersion BUILD_TOOLS_VERSION
 
     defaultConfig {
-        applicationId "com.suyghur.dolin.simple"
+        applicationId "com.suyghur.dolin"
         minSdkVersion MIN_SDK_VERSION
         targetSdkVersion TARGET_SDK_VERSION
         versionCode 1

+ 2 - 2
demo/src/main/AndroidManifest.xml

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="com.suyghur.dolin.simple">
+    package="com.suyghur.dolin">
 
     <application
         android:name=".DemoApplication"
@@ -22,7 +22,7 @@
         </activity>
 
         <activity
-            android:name=".ZapActivity"
+            android:name="com.suyghur.dolin.ZapActivity"
             android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
             android:exported="true"
             android:launchMode="singleTask"

+ 1 - 1
demo/src/main/java/com/suyghur/dolin/simple/DemoActivity.kt → demo/src/main/java/com/suyghur/dolin/DemoActivity.kt

@@ -1,4 +1,4 @@
-package com.suyghur.dolin.simple
+package com.suyghur.dolin
 
 import android.app.Activity
 import android.app.AlertDialog

+ 1 - 3
demo/src/main/java/com/suyghur/dolin/simple/DemoApplication.kt → demo/src/main/java/com/suyghur/dolin/DemoApplication.kt

@@ -1,8 +1,7 @@
-package com.suyghur.dolin.simple
+package com.suyghur.dolin
 
 import android.app.Application
 import android.content.Context
-import androidx.lifecycle.ProcessLifecycleOwner
 import com.suyghur.dolin.zap.entity.Config
 import com.suyghur.dolin.zap.entity.Level
 import com.suyghur.dolin.zap.Zap
@@ -19,7 +18,6 @@ class DemoApplication : Application() {
 
     override fun onCreate() {
         super.onCreate()
-//        ProcessLifecycleOwner.get().lifecycle.addObserver(MyLifecycleObserver())
         val config = Config.Builder()
                 .setLogcatLevel(Level.DEBUG)
                 .setRecordEnable(true)

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

@@ -0,0 +1,3 @@
+package com.suyghur.dolin
+
+class Item constructor(val id: Int, val text: String)

+ 9 - 1
demo/src/main/java/com/suyghur/dolin/simple/ZapActivity.kt → demo/src/main/java/com/suyghur/dolin/ZapActivity.kt

@@ -1,4 +1,4 @@
-package com.suyghur.dolin.simple
+package com.suyghur.dolin
 
 import android.app.Activity
 import android.content.Intent
@@ -39,6 +39,14 @@ class ZapActivity : Activity(), View.OnClickListener {
     }
 
     override fun onClick(v: View?) {
+        v?.apply {
+            when (tag as Int) {
+                0 -> Zap.d("测试DEBUG日志")
+                1 -> Zap.i("测试INFO日志")
+                2 -> Zap.w("测试WARNING日志")
+                3 -> Zap.e("测试ERROR日志")
+            }
+        }
     }
 
     companion object {

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

@@ -1,3 +0,0 @@
-package com.suyghur.dolin.simple
-
-class Item constructor(val id: Int, val text: String)

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

@@ -1,44 +0,0 @@
-package com.suyghur.dolin.simple
-
-import androidx.lifecycle.Lifecycle
-import androidx.lifecycle.LifecycleObserver
-import androidx.lifecycle.OnLifecycleEvent
-import com.suyghur.dolin.zap.Zap
-
-/**
- * @author #Suyghur.
- * Created on 4/8/21
- */
-class MyLifecycleObserver : LifecycleObserver {
-
-    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
-    fun onCreate() {
-        Zap.d("Application onCreate")
-    }
-
-    @OnLifecycleEvent(Lifecycle.Event.ON_START)
-    fun onStart() {
-        Zap.d("Application onStart")
-    }
-
-    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
-    fun onResume() {
-        Zap.d("Application onResume")
-    }
-
-    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
-    fun onPause() {
-        Zap.d("Application onPause")
-    }
-
-
-    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
-    fun onStop() {
-        Zap.d("Application onStop")
-    }
-
-    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
-    fun onDestroy() {
-        Zap.d("Application onDestroy")
-    }
-}

+ 1 - 0
library_zap/README.md

@@ -0,0 +1 @@
+# Zap日志模块

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

@@ -2,7 +2,7 @@ package com.suyghur.dolin.zap
 
 import android.app.Application
 import com.suyghur.dolin.zap.entity.Config
-import com.suyghur.dolin.zap.impl.ZapPrintImpl
+import com.suyghur.dolin.zap.impl.ZapPrint
 
 /**
  * @author #Suyghur.
@@ -10,43 +10,57 @@ import com.suyghur.dolin.zap.impl.ZapPrintImpl
  */
 object Zap {
 
+    private lateinit var sZapPrint: ZapPrint
+    private var hasInitialized = false
+
     fun initialize(application: Application, config: Config) {
-        ZapPrintImpl.getInstance().initialize(application, config)
+        sZapPrint = ZapPrint.instance()
+        sZapPrint.initialize(application, config)
+        hasInitialized = true
     }
 
     fun recycle() {
-        ZapPrintImpl.getInstance().recycle()
+        if (hasInitialized)
+            sZapPrint.recycle()
     }
 
     fun d(any: Any?) {
-        ZapPrintImpl.getInstance().d(any)
+        if (hasInitialized)
+            sZapPrint.d(any)
     }
 
     fun d(tag: String, any: Any?) {
-        ZapPrintImpl.getInstance().d(tag, any)
+        if (hasInitialized)
+            sZapPrint.d(tag, any)
     }
 
     fun i(any: Any?) {
-        ZapPrintImpl.getInstance().i(any)
+        if (hasInitialized)
+            sZapPrint.i(any)
     }
 
     fun i(tag: String, any: Any?) {
-        ZapPrintImpl.getInstance().i(tag, any)
+        if (hasInitialized)
+            sZapPrint.i(tag, any)
     }
 
     fun w(any: Any?) {
-        ZapPrintImpl.getInstance().w(any)
+        if (hasInitialized)
+            sZapPrint.w(any)
     }
 
     fun w(tag: String, any: Any?) {
-        ZapPrintImpl.getInstance().w(tag, any)
+        if (hasInitialized)
+            sZapPrint.w(tag, any)
     }
 
     fun e(any: Any?) {
-        ZapPrintImpl.getInstance().e(any)
+        if (hasInitialized)
+            sZapPrint.e(any)
     }
 
     fun e(tag: String, any: Any?) {
-        ZapPrintImpl.getInstance().e(tag, any)
+        if (hasInitialized)
+            sZapPrint.e(tag, any)
     }
 }

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

@@ -1,7 +1,5 @@
 package com.suyghur.dolin.zap.entity
 
-import android.text.TextUtils
-
 /**
  * @author #Suyghur.
  * Created on 4/7/21
@@ -10,7 +8,7 @@ class Config private constructor(builder: Builder) {
 
     var logDir = ""
         private set
-    var defaultTag = ""
+    var tag = ""
         private set
     var logcatLevel: Level
         private set
@@ -24,7 +22,7 @@ class Config private constructor(builder: Builder) {
 
     init {
         this.logDir = builder.logDir
-        this.defaultTag = builder.defaultTag
+        this.tag = builder.tag
         this.logcatLevel = builder.logcatLevel
         this.recordLevel = builder.recordLevel
         this.recordEnable = builder.recordEnable
@@ -44,7 +42,7 @@ class Config private constructor(builder: Builder) {
 
     class Builder() {
         internal var logDir = ""
-        internal var defaultTag = "dolin_zap"
+        internal var tag = ""
         internal var logcatLevel = Level.DEBUG
         internal var recordLevel = Level.DEBUG
         internal var recordEnable = true
@@ -60,9 +58,7 @@ class Config private constructor(builder: Builder) {
         }
 
         fun setDefaultTag(tag: String): Builder {
-            if (!TextUtils.isEmpty(tag)) {
-                this.defaultTag = tag
-            }
+            this.tag = tag
             return this
         }
 

+ 0 - 2
library_zap/src/main/java/com/suyghur/dolin/zap/impl/Record2MMap.kt

@@ -1,6 +1,5 @@
 package com.suyghur.dolin.zap.impl
 
-import com.suyghur.dolin.zap.Zap
 import com.suyghur.dolin.zap.internal.IRecord
 
 /**
@@ -34,7 +33,6 @@ class Record2MMap(bufferPath: String, capacity: Int, logPath: String, compress:
     override fun asyncFlush() {
         if (ptr != 0L) {
             try {
-                Zap.d("asyncFlush")
                 asyncFlushNative(ptr)
             } catch (e: Exception) {
                 e.printStackTrace()

+ 12 - 16
library_zap/src/main/java/com/suyghur/dolin/zap/impl/ZapPrintImpl.kt → library_zap/src/main/java/com/suyghur/dolin/zap/impl/ZapPrint.kt

@@ -3,12 +3,10 @@ package com.suyghur.dolin.zap.impl
 import android.app.Application
 import android.text.TextUtils
 import android.util.Log
-import androidx.annotation.Keep
-import androidx.lifecycle.ProcessLifecycleOwner
 import com.suyghur.dolin.zap.entity.Config
 import com.suyghur.dolin.zap.entity.Level
 import com.suyghur.dolin.zap.entity.ZapData
-import com.suyghur.dolin.zap.internal.ILogger
+import com.suyghur.dolin.zap.internal.IPrint
 import com.suyghur.dolin.zap.util.FileUtils
 import java.io.File
 import java.lang.reflect.Array
@@ -19,7 +17,7 @@ import java.util.*
  * @author #Suyghur.
  * Created on 4/7/21
  */
-class ZapPrintImpl : ILogger {
+class ZapPrint : IPrint {
 
     private var hasInitialized: Boolean = false
     private var tag = ""
@@ -29,18 +27,24 @@ class ZapPrintImpl : ILogger {
     private var record2MMap: Record2MMap? = null
     private var dateFileFormatter: DateFileFormatter = DateFileFormatter()
 
+
     fun initialize(application: Application, config: Config) {
         if (hasInitialized) {
             throw IllegalArgumentException("Logger already initialize")
         }
 
-        logDir = if (TextUtils.isEmpty(config.logDir)) {
+        this.logDir = if (TextUtils.isEmpty(config.logDir)) {
             FileUtils.getLogDir(application)
         } else {
             config.logDir
         }
 
-        this.tag = config.defaultTag
+        this.tag = if (TextUtils.isEmpty(config.tag)) {
+            "dolin_zap"
+        } else {
+            config.tag
+        }
+
         this.logcatLevel = config.logcatLevel
         this.recordLevel = config.recordLevel
         val logFileName = logDir + File.separator + SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(Date()) + "-zap"
@@ -90,10 +94,6 @@ class ZapPrintImpl : ILogger {
         print(Level.ERROR, tag, any)
     }
 
-//    override fun record(level: Int, tag: String, msg: String) {
-//        TODO("Not yet implemented")
-//    }
-
     private fun interceptLogcat(level: Level): Boolean {
         return level.ordinal < logcatLevel.ordinal
     }
@@ -159,7 +159,6 @@ class ZapPrintImpl : ILogger {
     }
 
     private fun doRecord(data: ZapData) {
-        Log.d("dolin_zap", "$data")
         record2MMap?.write(dateFileFormatter.format(data.level, data.tag, data.msg))
     }
 
@@ -167,14 +166,12 @@ class ZapPrintImpl : ILogger {
 
         const val MAX_LENGTH_OF_SINGLE_MESSAGE = 4063
 
-        @Keep
-        @JvmStatic
-        fun getInstance(): ZapPrintImpl {
+        fun instance(): ZapPrint {
             return LoggerPrintHolder.INSTANCE
         }
 
         private object LoggerPrintHolder {
-            val INSTANCE = ZapPrintImpl()
+            val INSTANCE = ZapPrint()
         }
 
         /**
@@ -185,5 +182,4 @@ class ZapPrintImpl : ILogger {
         }
     }
 
-
 }

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

@@ -4,7 +4,7 @@ package com.suyghur.dolin.zap.internal
  * @author #Suyghur.
  * Created on 4/7/21
  */
-interface ILogger {
+interface IPrint {
 
 
     fun d(any: Any?)
@@ -23,6 +23,4 @@ interface ILogger {
 
     fun e(tag: String, any: Any?)
 
-//    fun record(level: Int, tag: String, msg: String)
-
 }