Kaynağa Gözat

Z991239-5349 #comment fea: change method to generate uuid4

chenliangyu 1 yıl önce
ebeveyn
işleme
c094189ce2

+ 3 - 0
Framework/RvcLogSdk/CMakeLists.txt

@@ -4,6 +4,7 @@ set(MODULE_PREFIX "RVCLOGSDK")
 file(GLOB ${MODULE_PREFIX}_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cpp" "*.h" "*.c")
 file(GLOB SQLITE3_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "sqlite3/*.cpp" "sqlite3/*.h" "sqlite3/*.c")
 list(APPEND SQLITE3_SRCS ${CMAKE_SOURCE_DIR}/ThirdParty/Include/CJson/cJSON.c)
+list(APPEND SQLITE3_SRCS ${CMAKE_SOURCE_DIR}/ThirdParty/Include/uuid4/uuid4.cpp)
 
 add_library(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS} ${SQLITE3_SRCS})
 
@@ -15,6 +16,7 @@ target_include_directories(${MODULE_NAME} PRIVATE
     ${CONAN_INCLUDE_DIRS_JSONCPP}
     ${RVCFRAMEWORK_SOURCES_DIR}/RvcRestfulSdk
     ${CMAKE_SOURCE_DIR}/ThirdParty/Include/CJson
+    ${CMAKE_SOURCE_DIR}/ThirdParty/Include/uuid4
     ${CONAN_INCLUDE_DIRS_ZLIB}
     ${CONAN_INCLUDE_DIRS_OPENSSL}
 )
@@ -32,6 +34,7 @@ target_include_directories(${MODULE_NAME} PRIVATE
     ${CONAN_INCLUDE_DIRS_JSONCPP}
     ${RVCFRAMEWORK_SOURCES_DIR}/RvcRestfulSdk
     ${CMAKE_SOURCE_DIR}/ThirdParty/Include/CJson
+    ${CMAKE_SOURCE_DIR}/ThirdParty/Include/uuid4
     ${CONAN_INCLUDE_DIRS_ZLIB}
 )
 target_link_directories(${MODULE_NAME} PRIVATE 

+ 0 - 14
Framework/RvcLogSdk/log_builder.c

@@ -111,13 +111,6 @@ int add_beidou_log(log_group_builder* bder, beidou_log_item* log) {
     //统一日志
     {
 
-        // 应用信息
-        //cJSON_AddItemToObject(united_content, "appName", cJSON_CreateString(bder->config->unitedAppName));
-        //cJSON_AddItemToObject(united_content, "_CMB_LOG_SPEC_VERSION", cJSON_CreateString(bder->config->unitedVersion));
-
-        //cJSON_AddItemToObject(meta_log_content, "uuid", cJSON_CreateString(bder->grp->logs[bder->grp->n_logs].uuid));
-        //cJSON_AddItemToObject(meta_log_content, "needArchived", cJSON_CreateBool(bder->config->unitedNeedArchived));
-
         cJSON_AddItemToObject(united_content, "businessId", cJSON_CreateString(log->BussID));
         cJSON_AddItemToObject(united_content, "traceId", cJSON_CreateString(log->TraceID));
         cJSON_AddItemToObject(united_content, "parentSpanId", cJSON_CreateString(log->ParentSpanId));
@@ -384,15 +377,8 @@ int add_log(log_group_builder* bder, long start_time, uint32_t pack_index, log_i
 
         cJSON_AddItemToObject(united_content, "ts", cJSON_CreateString(unitedNowTime));
 
-        //cJSON_AddItemToObject(meta_log_content, "uuid", cJSON_CreateString(bder->grp->logs[bder->grp->n_logs].uuid));
-        //cJSON_AddItemToObject(meta_log_content, "needArchived", cJSON_CreateBool(bder->config->unitedNeedArchived));
-
-        //cJSON_AddItemToObject(log_content, "ts", cJSON_CreateString(unitedNowTime));
-        //cJSON_AddItemToObject(log_content, "deployUnitId", cJSON_CreateString(bder->config->unitedDeployUnitId));
-        //cJSON_AddItemToObject(log_content, "serviceUnitId", cJSON_CreateString(bder->config->unitedServiceUnitId));
         cJSON_AddItemToObject(simpleLog_content, "content", skyeye_content);
         cJSON_AddItemToObject(united_content, "log", simpleLog_content);
-        //cJSON_AddItemToObject(united_content, "content", skyeye_content);
         cJSON_AddItemToObject(united_content, "type", cJSON_CreateString("BASETYPE"));
         cJSON_AddItemToObject(united_content, "serviceUnitId", cJSON_CreateString(bder->config->unitedServiceUnitId));
 

+ 6 - 24
Framework/RvcLogSdk/log_util.c → Framework/RvcLogSdk/log_util.cpp

@@ -17,12 +17,14 @@
 #include <netinet/tcp.h>
 #include <net/if.h>
 #include <sys/stat.h>
-#include "uuid4.h"
 #endif
 #include <string.h>
 #include "log_util.h"
 #include "md5.h"
 #include <stdio.h>
+#include "uuid4.h"
+#include <direct.h>
+#include <io.h>
 
 static const char *g_hex_hash = "0123456789ABCDEF";
 
@@ -88,29 +90,9 @@ int signature_to_base64(const char * sig, int sigLen, const char * key, int keyL
 
 void get_format_uuid(char* strbuffer, int ulen)
 {
-#if defined(_MSC_VER)
-    UUID uuid;
-    int uuidlen = 0;
-    RPC_CSTR buf;
-    UuidCreate((UUID*)&uuid);
-    UuidToStringA((UUID*)&uuid, &buf);
-
-    uuidlen = strlen((const char*)buf);
-    if (uuidlen < ulen) {
-        memcpy(strbuffer, (const char*)buf, uuidlen);
-    }
-
-    RpcStringFree(&buf);
-#else
-    int uuidlen = 0;
-    char buf[UUID4_LEN];
-    uuid4_init();
-    uuid4_generate(buf);
-    uuidlen = strlen((const char*)buf);
-    if (uuidlen < ulen) {
-        memcpy(strbuffer, (const char*)buf, uuidlen);
-    }
-#endif //_MSC_VER
+	int uuidlen = 0;
+	uuid4_init();
+	uuid4_generate(strbuffer);
 }
 
 void GetTimeStr(time_t time, char* szTime)

+ 0 - 90
Framework/RvcLogSdk/uuid4.c

@@ -1,90 +0,0 @@
-/**
- * Copyright (c) 2018 rxi
- *
- * This library is free software; you can redistribute it and/or modify it
- * under the terms of the MIT license. See LICENSE for details.
- */
-
-#include <stdio.h>
-#include <stdint.h>
-
-#if defined(_WIN32)
-#include <windows.h>
-#include <wincrypt.h>
-#endif
-
-#include "uuid4.h"
-
-
-static uint64_t seed[2];
-
-
-static uint64_t xorshift128plus(uint64_t *s) {
-  /* http://xorshift.di.unimi.it/xorshift128plus.c */
-  uint64_t s1 = s[0];
-  const uint64_t s0 = s[1];
-  s[0] = s0;
-  s1 ^= s1 << 23;
-  s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5);
-  return s[1] + s0;
-}
-
-
-int uuid4_init(void) {
-#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
-  int res;
-  FILE *fp = fopen("/dev/urandom", "rb");
-  if (!fp) {
-    return UUID4_EFAILURE;
-  }
-  res = fread(seed, 1, sizeof(seed), fp);
-  fclose(fp);
-  if ( res != sizeof(seed) ) {
-    return UUID4_EFAILURE;
-  }
-
-#elif defined(_WIN32)
-  int res;
-  HCRYPTPROV hCryptProv;
-  res = CryptAcquireContext(
-    &hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
-  if (!res) {
-    return UUID4_EFAILURE;
-  }
-  res = CryptGenRandom(hCryptProv, (DWORD) sizeof(seed), (PBYTE) seed);
-  CryptReleaseContext(hCryptProv, 0);
-  if (!res) {
-    return UUID4_EFAILURE;
-  }
-
-#else
-  #error "unsupported platform"
-#endif
-  return UUID4_ESUCCESS;
-}
-
-
-void uuid4_generate(char *dst) {
-  static const char *template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
-  static const char *chars = "0123456789abcdef";
-  union { unsigned char b[16]; uint64_t word[2]; } s;
-  const char *p;
-  int i, n;
-  /* get random */
-  s.word[0] = xorshift128plus(seed);
-  s.word[1] = xorshift128plus(seed);
-  /* build string */
-  p = template;
-  i = 0;
-  while (*p) {
-    n = s.b[i >> 1];
-    n = (i & 1) ? (n >> 4) : (n & 0xf);
-    switch (*p) {
-      case 'x'  : *dst = chars[n];              i++;  break;
-      case 'y'  : *dst = chars[(n & 0x3) + 8];  i++;  break;
-      default   : *dst = *p;
-    }
-    dst++, p++;
-  }
-  *dst = '\0';
-}

+ 0 - 22
Framework/RvcLogSdk/uuid4.h

@@ -1,22 +0,0 @@
-/**
- * Copyright (c) 2018 rxi
- *
- * This library is free software; you can redistribute it and/or modify it
- * under the terms of the MIT license. See LICENSE for details.
- */
-
-#ifndef UUID4_H
-#define UUID4_H
-
-#define UUID4_VERSION "1.0.0"
-#define UUID4_LEN 37
-
-enum {
-  UUID4_ESUCCESS =  0,
-  UUID4_EFAILURE = -1
-};
-
-int  uuid4_init(void);
-void uuid4_generate(char *dst);
-
-#endif

+ 3 - 0
Framework/spbase/CMakeLists.txt

@@ -41,6 +41,8 @@ if(MSVC)
     "sp_tbs_win.cpp"
     )
 endif(MSVC)
+list(APPEND ${MODULE_PREFIX}_SRCS ${CMAKE_SOURCE_DIR}/ThirdParty/Include/uuid4/uuid4.cpp)
+#list(APPEND ${MODULE_PREFIX}_SRCS ${CMAKE_SOURCE_DIR}/ThirdParty/Include/uuid4/uuid.cpp)
 
 set(SPBASE_VERSION_STRING "1.1.1-dev2")
 rvc_set_win_dll_ver(${MODULE_PREFIX} ${SPBASE_VERSION_STRING})
@@ -68,6 +70,7 @@ target_include_directories(${MODULE_NAME} PRIVATE
     ${CONAN_INCLUDE_DIRS_JSONCPP}
     ${RVCFRAMEWORK_SOURCES_DIR}/RvcLogSdk
     ${RVCFRAMEWORK_SOURCES_DIR}/RvcRestfulSdk
+    ${CMAKE_SOURCE_DIR}/ThirdParty/Include/uuid4
     )
 target_link_directories(${MODULE_NAME} PRIVATE 
 	${CONAN_LIB_DIRS_JSONCPP}

+ 7 - 0
Framework/spbase/SpBase.cpp

@@ -41,6 +41,7 @@
 #include<winpr/library.h>
 #include <winpr/locale.h>
 #include <winpr/exception.h>
+#include "uuid4.h"
 
 
 #ifndef RVC_OS_WIN
@@ -938,3 +939,9 @@ CClientSessionBase::~CClientSessionBase()
 		pFunction->DecrementRef();
 	}
 }
+
+CSimpleStringA uuid4_generateStr(int len)
+{
+	auto ret = uuid4_generate(len);
+	return ret.c_str();
+}

+ 1 - 1
Framework/spbase/sp_logwithlink.cpp

@@ -897,4 +897,4 @@ ErrorCodeEnum GetAllEntityIdx(CAutoArray<CSimpleStringA>& strEntityNames, CAutoA
         wEntityDevelopIDs[i - 1] = ent->idx;
     }
     return Error_Succeed;
-}
+}

+ 0 - 6
Framework/spbase/uuid4.cpp → ThirdParty/Include/uuid4/uuid4.cpp

@@ -15,7 +15,6 @@
 #endif
 
 #include "uuid4.h"
-#include "SpBase.h"
 
 
 static uint64_t seed[2];
@@ -104,8 +103,3 @@ std::string uuid4_generate(int len)
         return "";
 }
 
-CSimpleStringA uuid4_generateStr(int len)
-{
-    auto ret = uuid4_generate(len);
-    return ret.c_str();
-}

+ 0 - 0
Framework/spbase/uuid4.h → ThirdParty/Include/uuid4/uuid4.h