Procházet zdrojové kódy

feature:抽离zap模块中部分方法到common中

#Suyghur před 4 roky
rodič
revize
d417441650

+ 2 - 0
build.gradle

@@ -1,5 +1,7 @@
 // Top-level build file where you can add configuration options common to all sub-projects/modules.
 buildscript {
+
+    ext.COMMON_LIB_DEV_ENABLE = false
     // 混淆开关
     ext.MINIFY_ENABLE = false
     // ndk版本

+ 3 - 1
library_common/CMakeLists.txt

@@ -10,14 +10,16 @@ cmake_minimum_required(VERSION 3.4.1)
 # You can define multiple libraries, and CMake builds them for you.
 # Gradle automatically packages shared libraries with your APK.
 
-include_directories(src/main/cpp/include)
+include_directories(src/main/cpp/buffer)
 aux_source_directory(src/main/cpp/ DIR_SOURCE)
+aux_source_directory(src/main/cpp/buffer DIR_SOURCE_BUFFER)
 #add_subdirectory(common)
 #include_directories(src/main/cpp/common)
 add_library(
         dolin-common
         SHARED
         ${DIR_SOURCE}
+        ${DIR_SOURCE_BUFFER}
 )
 
 # Searches for a specified prebuilt library and stores the path as a

+ 15 - 11
library_common/build.gradle

@@ -11,12 +11,14 @@ android {
         minSdkVersion MIN_SDK_VERSION
         targetSdkVersion TARGET_SDK_VERSION
 
-//        externalNativeBuild {
-//            cmake {
-//                cppFlags '-std=c++11 -frtti -fexceptions -lz'
-//                abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
-//            }
-//        }
+        if (COMMON_LIB_DEV_ENABLE) {
+            externalNativeBuild {
+                cmake {
+                    cppFlags '-std=c++11 -frtti -fexceptions -lz'
+                    abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
+                }
+            }
+        }
     }
 
     buildTypes {
@@ -46,11 +48,13 @@ android {
         preDexLibraries = false
     }
 
-//    externalNativeBuild {
-//        cmake {
-//            path "CMakeLists.txt"
-//        }
-//    }
+    if (COMMON_LIB_DEV_ENABLE) {
+        externalNativeBuild {
+            cmake {
+                path "CMakeLists.txt"
+            }
+        }
+    }
 }
 
 dependencies {

+ 2 - 2
library_zap/src/main/cpp/buffer.cpp → library_common/src/main/cpp/buffer/buffer.cpp

@@ -2,7 +2,7 @@
 // Created by #Suyghur, on 4/7/21.
 //
 
-#include "include/buffer.h"
+#include "buffer.h"
 
 Buffer::Buffer(char *ptr, size_t buffer_size) : buffer_ptr(ptr), buffer_size(buffer_size), buffer_header(buffer_ptr, buffer_size) {}
 
@@ -14,7 +14,7 @@ void Buffer::InitData(char *log_path, size_t log_path_len, bool _compress) {
     std::lock_guard<std::recursive_mutex> lck_release(log_mtx);
     memset(buffer_ptr, '\0', buffer_size);
 
-    zap::Header header{};
+    dolin_common::Header header{};
     header.magic = kMagicHeader;
     header.log_path_len = log_path_len;
     header.log_path = log_path;

+ 1 - 1
library_zap/src/main/cpp/include/buffer.h → library_common/src/main/cpp/buffer/buffer.h

@@ -12,7 +12,7 @@
 #include "buffer_header.h"
 #include "file_flush.h"
 
-using namespace zap;
+using namespace dolin_common;
 
 class Buffer {
 public:

+ 1 - 1
library_zap/src/main/cpp/buffer_flush.cpp → library_common/src/main/cpp/buffer/buffer_flush.cpp

@@ -4,7 +4,7 @@
 
 #include <cmath>
 #include <cstring>
-#include "include/buffer_flush.h"
+#include "buffer_flush.h"
 
 BufferFlush::BufferFlush(FILE *log_file, size_t size) : capacity(size), log_file_ptr(log_file) {}
 

+ 0 - 0
library_zap/src/main/cpp/include/buffer_flush.h → library_common/src/main/cpp/buffer/buffer_flush.h


+ 16 - 16
library_zap/src/main/cpp/buffer_header.cpp → library_common/src/main/cpp/buffer/buffer_header.cpp

@@ -2,13 +2,13 @@
 // Created by #Suyghur, on 4/7/21.
 //
 
-#include "include/buffer_header.h"
+#include "buffer_header.h"
 
-zap::BufferHeader::BufferHeader(void *data, size_t size) : data_ptr((char *) data), data_size(size) {}
+dolin_common::BufferHeader::BufferHeader(void *data, size_t size) : data_ptr((char *) data), data_size(size) {}
 
-zap::BufferHeader::~BufferHeader() = default;
+dolin_common::BufferHeader::~BufferHeader() = default;
 
-void zap::BufferHeader::InitHeader(zap::Header &header) {
+void dolin_common::BufferHeader::InitHeader(dolin_common::Header &header) {
     if ((sizeof(char) + sizeof(size_t) + sizeof(size_t) + header.log_path_len) > data_size) {
         return;
     }
@@ -26,25 +26,25 @@ void zap::BufferHeader::InitHeader(zap::Header &header) {
 /**
  * 获取原始的锚点
  */
-void *zap::BufferHeader::GetOriginPtr() {
+void *dolin_common::BufferHeader::GetOriginPtr() {
     return data_ptr;
 }
 
 /**
  * 获取当前锚点
  */
-void *zap::BufferHeader::GetPtr() {
+void *dolin_common::BufferHeader::GetPtr() {
     return data_ptr + GetHeaderLen();
 }
 
 /**
  * 获取写入的锚点
  */
-void *zap::BufferHeader::GetWritePtr() {
+void *dolin_common::BufferHeader::GetWritePtr() {
     return data_ptr + GetHeaderLen() + GetLogLen();
 }
 
-zap::Header *zap::BufferHeader::GetHeader() {
+dolin_common::Header *dolin_common::BufferHeader::GetHeader() {
     auto *header = new Header();
     if (IsAvailable()) {
         header->magic = kMagicHeader;
@@ -68,20 +68,20 @@ zap::Header *zap::BufferHeader::GetHeader() {
     return header;
 }
 
-size_t zap::BufferHeader::GetHeaderLen() {
+size_t dolin_common::BufferHeader::GetHeaderLen() {
     if (IsAvailable()) {
         return CalculateHeaderLen(GetLogPathLen());
     }
     return 0;
 }
 
-void zap::BufferHeader::SetLogLen(size_t len) {
+void dolin_common::BufferHeader::SetLogLen(size_t len) {
     if (IsAvailable()) {
         memcpy(data_ptr + sizeof(char), &len, sizeof(size_t));
     }
 }
 
-size_t zap::BufferHeader::GetLogLen() {
+size_t dolin_common::BufferHeader::GetLogLen() {
     if (IsAvailable()) {
         size_t len = 0;
         memcpy(&len, data_ptr + sizeof(char), sizeof(size_t));
@@ -93,7 +93,7 @@ size_t zap::BufferHeader::GetLogLen() {
     return 0;
 }
 
-size_t zap::BufferHeader::GetLogPathLen() {
+size_t dolin_common::BufferHeader::GetLogPathLen() {
     if (IsAvailable()) {
         size_t len = 0;
         memcpy(&len, data_ptr + sizeof(char) + sizeof(size_t), sizeof(size_t));
@@ -105,7 +105,7 @@ size_t zap::BufferHeader::GetLogPathLen() {
     return 0;
 }
 
-char *zap::BufferHeader::GetLogPath() {
+char *dolin_common::BufferHeader::GetLogPath() {
     if (IsAvailable()) {
         size_t log_path_len = GetLogPathLen();
         if (log_path_len > 0) {
@@ -119,18 +119,18 @@ char *zap::BufferHeader::GetLogPath() {
 }
 
 
-bool zap::BufferHeader::IsCompress() {
+bool dolin_common::BufferHeader::IsCompress() {
     if (IsAvailable()) {
         return ((data_ptr + sizeof(char) + sizeof(size_t) + sizeof(size_t) + GetLogPathLen())[0]) == 1;
     }
     return false;
 }
 
-bool zap::BufferHeader::IsAvailable() {
+bool dolin_common::BufferHeader::IsAvailable() {
     return data_ptr[0] == kMagicHeader;
 }
 
-size_t zap::BufferHeader::CalculateHeaderLen(size_t path_len) {
+size_t dolin_common::BufferHeader::CalculateHeaderLen(size_t path_len) {
     return sizeof(char) + sizeof(size_t) + sizeof(size_t) + path_len + sizeof(char);
 }
 

+ 1 - 1
library_zap/src/main/cpp/include/buffer_header.h → library_common/src/main/cpp/buffer/buffer_header.h

@@ -7,7 +7,7 @@
 
 #include <string>
 
-namespace zap {
+namespace dolin_common {
     static const char kMagicHeader = '\x11';
 
     struct Header {

+ 3 - 3
library_zap/src/main/cpp/file_flush.cpp → library_common/src/main/cpp/buffer/file_flush.cpp

@@ -2,7 +2,7 @@
 // Created by #Suyghur, on 4/7/21.
 //
 
-#include "include/file_flush.h"
+#include "file_flush.h"
 
 FileFlush::FileFlush() {
     async_thread = std::thread(&FileFlush::AsyncLogThread, this);
@@ -13,7 +13,7 @@ FileFlush::~FileFlush() {
 }
 
 bool FileFlush::AsyncFlush(BufferFlush *buffer) {
-    std::unique_lock<std::mutex> lck_async_flush(async_mtx);
+    std::unique_lock <std::mutex> lck_async_flush(async_mtx);
     if (exit) {
         delete buffer;
         return false;
@@ -31,7 +31,7 @@ void FileFlush::StopFlush() {
 
 void FileFlush::AsyncLogThread() {
     while (true) {
-        std::unique_lock<std::mutex> lck_async_log_thread(async_mtx);
+        std::unique_lock <std::mutex> lck_async_log_thread(async_mtx);
         while (!async_buffer.empty()) {
             BufferFlush *data = async_buffer.back();
             async_buffer.pop_back();

+ 0 - 0
library_zap/src/main/cpp/include/file_flush.h → library_common/src/main/cpp/buffer/file_flush.h


binární
library_zap/src/main/cpp/libs/arm64-v8a/libdolin-common.so


binární
library_zap/src/main/cpp/libs/armeabi-v7a/libdolin-common.so


binární
library_zap/src/main/cpp/libs/x86/libdolin-common.so


binární
library_zap/src/main/cpp/libs/x86_64/libdolin-common.so


+ 74 - 0
library_zap/src/main/cpp/third_part/buffer/buffer.h

@@ -0,0 +1,74 @@
+//
+// Created by #Suyghur, on 4/7/21.
+//
+
+#ifndef DOLIN_ZAP_BUFFER_H
+#define DOLIN_ZAP_BUFFER_H
+
+#include <string>
+#include <mutex>
+#include <zlib.h>
+#include <sys/mman.h>
+#include "buffer_header.h"
+#include "file_flush.h"
+
+using namespace dolin_common;
+
+class Buffer {
+public:
+
+    bool map_buffer = true;
+
+    Buffer(char *ptr, size_t buffer_size);
+
+    ~Buffer();
+
+    void InitData(char *log_path, size_t log_path_len, bool _compress);
+
+    size_t GetLength();
+
+    size_t Append(const char *log, size_t len);
+
+    void Release();
+
+    size_t EmptySize();
+
+    char *GetLogPath();
+
+    void SetFileFlush(FileFlush *flush);
+
+    void CallFileFlush();
+
+    void CallFileFlush(FileFlush *flush);
+
+    void CallFileFlush(FileFlush *flush, Buffer *buffer);
+
+    void ChangeLogPath(char *path);
+
+private:
+    FILE *log_file_ptr = nullptr;
+    FileFlush *file_flush_ptr = nullptr;
+    char *const buffer_ptr = nullptr;
+    char *data_ptr = nullptr;
+    char *write_ptr = nullptr;
+
+    size_t buffer_size = 0;
+    std::recursive_mutex log_mtx;
+
+    BufferHeader buffer_header;
+    z_stream zStream{};
+    bool compress = false;
+
+    void Clear();
+
+    void SetLength(size_t len);
+
+    bool InitCompress(bool compress);
+
+    bool OpenLogFile(const char *path);
+
+
+};
+
+
+#endif //DOLIN_ZAP_BUFFER_H

+ 41 - 0
library_zap/src/main/cpp/third_part/buffer/buffer_flush.h

@@ -0,0 +1,41 @@
+//
+// Created by #Suyghur, on 4/7/21.
+//
+
+#ifndef DOLIN_ZAP_BUFFER_FLUSH_H
+#define DOLIN_ZAP_BUFFER_FLUSH_H
+
+
+#include <cstdio>
+
+class BufferFlush {
+
+public:
+    BufferFlush(FILE *log_file, size_t size = 128);
+
+    ~BufferFlush();
+
+    void Write(void *data, size_t len);
+
+    void Reset();
+
+    size_t GetLength();
+
+    void *GetPtr();
+
+    FILE *GetLogFile();
+
+    void ReleaseThiz(void *buffer);
+
+private:
+    FILE *log_file_ptr = nullptr;
+    void *release_ptr = nullptr;
+    char *data_ptr = nullptr;
+    char *write_ptr = nullptr;
+    size_t capacity;
+
+    size_t EmptySize();
+};
+
+
+#endif //DOLIN_ZAP_BUFFER_FLUSH_H

+ 60 - 0
library_zap/src/main/cpp/third_part/buffer/buffer_header.h

@@ -0,0 +1,60 @@
+//
+// Created by #Suyghur, on 4/7/21.
+//
+
+#ifndef DOLIN_ZAP_BUFFER_HEADER_H
+#define DOLIN_ZAP_BUFFER_HEADER_H
+
+#include <string>
+
+namespace dolin_common {
+    static const char kMagicHeader = '\x11';
+
+    struct Header {
+        char magic;
+        size_t log_len;
+        size_t log_path_len;
+        char *log_path;
+        bool compress;
+    };
+
+    class BufferHeader {
+    public:
+        BufferHeader(void *data, size_t size);
+
+        ~BufferHeader();
+
+        void InitHeader(Header &header);
+
+        void *GetOriginPtr();
+
+        void *GetPtr();
+
+        void *GetWritePtr();
+
+        Header *GetHeader();
+
+        size_t GetHeaderLen();
+
+        void SetLogLen(size_t len);
+
+        size_t GetLogLen();
+
+        size_t GetLogPathLen();
+
+        char *GetLogPath();
+
+        bool IsCompress();
+
+        bool IsAvailable();
+
+        static size_t CalculateHeaderLen(size_t path_len);
+
+    private:
+        char *data_ptr;
+        size_t data_size;
+    };
+}
+
+
+#endif //DOLIN_ZAP_BUFFER_HEADER_H

+ 36 - 0
library_zap/src/main/cpp/third_part/buffer/file_flush.h

@@ -0,0 +1,36 @@
+//
+// Created by #Suyghur, on 4/7/21.
+//
+
+#ifndef DOLIN_ZAP_FILE_FLUSH_H
+#define DOLIN_ZAP_FILE_FLUSH_H
+
+
+#include "buffer_flush.h"
+#include <vector>
+#include <thread>
+
+class FileFlush {
+public:
+    FileFlush();
+
+    ~FileFlush();
+
+    bool AsyncFlush(BufferFlush *buffer);
+
+    void StopFlush();
+
+private:
+    void AsyncLogThread();
+
+    static ssize_t Flush(BufferFlush *buffer);
+
+    bool exit = false;
+    std::vector<BufferFlush *> async_buffer;
+    std::thread async_thread;
+    std::condition_variable async_condition;
+    std::mutex async_mtx;
+};
+
+
+#endif //DOLIN_ZAP_FILE_FLUSH_H

+ 0 - 17
library_zap/src/main/cpp/third_part/common.h

@@ -1,17 +0,0 @@
-//
-// Created by #Suyghur, on 4/13/21.
-//
-
-#ifndef DOLIN_COMMON_H
-#define DOLIN_COMMON_H
-
-
-#include <string>
-
-class Common {
-public:
-    static std::string TestFunc();
-};
-
-
-#endif //DOLIN_COMMON_H

+ 7 - 17
library_zap/src/main/cpp/zap.cpp

@@ -6,10 +6,9 @@
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <sstream>
-#include "include/buffer.h"
-#include "include/file_flush.h"
-#include "include/buffer_header.h"
-#include "third_part/common.h"
+#include "third_part/buffer/buffer.h"
+#include "third_part/buffer/file_flush.h"
+#include "third_part/buffer/buffer_header.h"
 
 static FileFlush *pFileFlush = nullptr;
 
@@ -18,9 +17,8 @@ static void WriteDirty2File(int buffer_fd) {
     if (fstat(buffer_fd, &file_stat) >= 0) {
         auto buffer_size = static_cast<size_t>(file_stat.st_size);
         //buffer size必须大于文件头长度,否则下标溢出
-        if (buffer_size > zap::BufferHeader::CalculateHeaderLen(0)) {
-            char *buffer_ptr_tmp = (char *) mmap(0, buffer_size, PROT_WRITE | PROT_READ, MAP_SHARED,
-                                                 buffer_fd, 0);
+        if (buffer_size > dolin_common::BufferHeader::CalculateHeaderLen(0)) {
+            char *buffer_ptr_tmp = (char *) mmap(0, buffer_size, PROT_WRITE | PROT_READ, MAP_SHARED, buffer_fd, 0);
             if (buffer_ptr_tmp != MAP_FAILED) {
                 auto *tmp = new Buffer(buffer_ptr_tmp, buffer_size);
                 size_t data_size = tmp->GetLength();
@@ -42,8 +40,7 @@ static char *OpenMMap(int buffer_fd, size_t buffer_size) {
         //根据buffer size 调整 buffer 大小
         ftruncate(buffer_fd, static_cast<int >(buffer_size));
         lseek(buffer_fd, 0, SEEK_SET);
-        map_ptr = (char *) mmap(nullptr, buffer_size, PROT_WRITE | PROT_READ, MAP_SHARED, buffer_fd,
-                                0);
+        map_ptr = (char *) mmap(nullptr, buffer_size, PROT_WRITE | PROT_READ, MAP_SHARED, buffer_fd, 0);
         if (map_ptr == MAP_FAILED) {
             map_ptr = nullptr;
         }
@@ -121,19 +118,12 @@ static void ReleaseNative(JNIEnv *env, jobject thiz, jlong ptr) {
     pFileFlush = nullptr;
 }
 
-
-static jstring TestCommonLib(JNIEnv *env, jobject thiz, jlong ptr) {
-
-    return env->NewStringUTF(Common::TestFunc().c_str());
-}
-
 static JNINativeMethod gMethods[] = {
         {"initNative",          "(Ljava/lang/String;ILjava/lang/String;Z)J", (void *) InitNative},
         {"writeNative",         "(JLjava/lang/String;)V",                    (void *) WriteNative},
         {"asyncFlushNative",    "(J)V",                                      (void *) AsyncFlushNative},
         {"changeLogPathNative", "(JLjava/lang/String;)V",                    (void *) ChangeLogPathNative},
-        {"releaseNative",       "(J)V",                                      (void *) ReleaseNative},
-        {"testCommonLib",       "(J)Ljava/lang/String;",                     (void *) TestCommonLib}
+        {"releaseNative",       "(J)V",                                      (void *) ReleaseNative}
 };
 
 extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {

+ 1 - 1
library_zap/src/main/java/com/dolin/zap/crashlytics/JavaCrashCatcher.kt

@@ -1,7 +1,7 @@
 package com.dolin.zap.crashlytics
 
 import android.os.Process
-import com.suyghur.dolin.zap.Zap
+import com.dolin.zap.Zap
 import kotlin.system.exitProcess
 
 /**

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

@@ -1,6 +1,5 @@
 package com.dolin.zap.impl
 
-import android.util.Log
 import com.dolin.zap.internal.IRecord
 
 /**
@@ -24,7 +23,6 @@ class Record2MMap(bufferPath: String, capacity: Int, logPath: String, compress:
     override fun write(msg: String) {
         if (ptr != 0L) {
             try {
-                Log.d("dolin_zap", "test : ${testCommonLib(ptr)}")
                 writeNative(ptr, msg)
             } catch (e: Exception) {
                 e.printStackTrace()
@@ -70,8 +68,6 @@ class Record2MMap(bufferPath: String, capacity: Int, logPath: String, compress:
 
     private external fun releaseNative(ptr: Long)
 
-    private external fun testCommonLib(ptr: Long): String
-
     companion object {
         @JvmStatic
         private external fun initNative(bufferPath: String, capacity: Int, logPath: String, compress: Boolean): Long