diff --git a/mars/CMakeLists.txt b/mars/CMakeLists.txt index 169d926b1..a9a5fde6f 100644 --- a/mars/CMakeLists.txt +++ b/mars/CMakeLists.txt @@ -84,4 +84,31 @@ if(ANDROID) libzstd_static ${SSL_LIB} ${CRYPT_LIB}) + +elseif(OHOS) +message("start build ohos") +add_definitions(-DOHOS) +if(NATIVE_CALLBACK) + message("common native callback") + add_definitions(-DNATIVE_CALLBACK) +endif() + +link_directories(app baseevent xlog sdt stn comm boost zstd) + +# marsxlog +set(SELF_LIB_NAME marsxlog) +file(GLOB SELF_SRC_FILES ohoslibraries/mars_xlog_sdk/import.cc) +add_library(${SELF_LIB_NAME} SHARED ${SELF_SRC_FILES}) +install(TARGETS ${SELF_LIB_NAME} LIBRARY DESTINATION ${SELF_LIBS_OUT} ARCHIVE DESTINATION ${SELF_LIBS_OUT}) +get_filename_component(EXPORT_XLOG_EXP_FILE ohoslibraries/export.exp ABSOLUTE) +set(SELF_XLOG_LINKER_FLAG "-Wl,--gc-sections -Wl,--version-script='${EXPORT_XLOG_EXP_FILE}'") +target_link_libraries(${SELF_LIB_NAME} "-Wl,--gc-sections" + xlog + mars-boost + comm + libzstd_static + libace_napi.z.so + libhilog_ndk.z.so + libz.so + ) endif() diff --git a/mars/app/CMakeLists.txt b/mars/app/CMakeLists.txt index e903f6f48..65fe91873 100644 --- a/mars/app/CMakeLists.txt +++ b/mars/app/CMakeLists.txt @@ -7,7 +7,9 @@ project (app) include(../comm/CMakeUtils.txt) include(../comm/CMakeExtraFlags.txt) - +if(OHOS) + add_definitions(-DOHOS) +endif() include_directories(.) include_directories(src) include_directories(..) diff --git a/mars/baseevent/CMakeLists.txt b/mars/baseevent/CMakeLists.txt index 658903532..e07891e2e 100644 --- a/mars/baseevent/CMakeLists.txt +++ b/mars/baseevent/CMakeLists.txt @@ -8,7 +8,9 @@ project (baseevent) include(../comm/CMakeUtils.txt) include(../comm/CMakeExtraFlags.txt) - +if(OHOS) + add_definitions(-DOHOS) +endif() include_directories(.) include_directories(src) include_directories(..) diff --git a/mars/boot/CMakeLists.txt b/mars/boot/CMakeLists.txt index af48150f0..c7c68106d 100644 --- a/mars/boot/CMakeLists.txt +++ b/mars/boot/CMakeLists.txt @@ -7,7 +7,9 @@ project(boot) include(../comm/CMakeUtils.txt) include(../comm/CMakeExtraFlags.txt) - +if(OHOS) + add_definitions(-DOHOS) +endif() include_directories(.) include_directories(src) include_directories(..) diff --git a/mars/build_ohos.py b/mars/build_ohos.py new file mode 100644 index 000000000..853a9d2a8 --- /dev/null +++ b/mars/build_ohos.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python3 +import os +import sys +import glob +import time +import shutil +import platform + +from mars_utils import * + + +SCRIPT_PATH = os.path.split(os.path.realpath(__file__))[0] + +def system_is_windows(): + return platform.system() == 'Windows' + +def system_architecture_is64(): + return platform.machine().endswith('64') + + +if system_is_windows(): + OHOS_GENERATOR = '-G "Unix Makefiles"' +else: + OHOS_GENERATOR = '' + +try: + OHOS_SDK_ROOT = os.environ['OHOS_SDK_ROOT'] +except KeyError as identifier: + OHOS_SDK_ROOT = '' + + +BUILD_OUT_PATH = 'cmake_build/ohos' +OHOS_LIBS_INSTALL_PATH = BUILD_OUT_PATH + '/' +OHOS_BUILD_CMD = 'cmake %s -DOHOS_ARCH="%s" ' \ + '-DOHOS_PLATFORM=OHOS -DCMAKE_TOOLCHAIN_FILE=%s/openharmony/11/native/build/cmake/ohos.toolchain.cmake ' \ + '-DOHOS_STL="c++_shared" ' \ + '&& cmake --build . ' +OHOS_SYMBOL_PATH = 'ohoslibraries/mars_ohos_sdk/obj/local/' +OHOS_LIBS_PATH = 'ohoslibraries/mars_ohos_sdk/libs/' +OHOS_XLOG_SYMBOL_PATH = 'ohoslibraries/mars_xlog_sdk/obj/local/' +OHOS_XLOG_LIBS_PATH = 'ohoslibraries/mars_xlog_sdk/libs/' + + +OHOS_STL_FILE = { + 'armeabi-v7a': OHOS_SDK_ROOT + '/openharmony/11/native/llvm/lib/arm-linux-ohos/libc++_shared.so', + 'x86': OHOS_SDK_ROOT + '/sources/cxx-stl/llvm-libc++/libs/x86_64-linux-ohos/libc++_shared.so', + 'arm64-v8a': OHOS_SDK_ROOT + '/openharmony/11/native/llvm/lib/aarch64-linux-ohos/libc++_shared.so', + 'x86_64': OHOS_SDK_ROOT + '/sources/cxx-stl/llvm-libc++/libs/x86_64-linux-ohos/libc++_shared.so', + } + + + +def build_ohos(incremental, arch, target_option=''): + + before_time = time.time() + + clean(BUILD_OUT_PATH, incremental) + os.chdir(BUILD_OUT_PATH) + + build_cmd = OHOS_BUILD_CMD %(SCRIPT_PATH, arch, OHOS_SDK_ROOT) + print("build cmd:" + build_cmd) + ret = os.system(build_cmd) + os.chdir(SCRIPT_PATH) + + if 0 != ret: + print('!!!!!!!!!!!!!!!!!!build fail!!!!!!!!!!!!!!!!!!!!') + return False + + if len(target_option) > 0: + symbol_path = OHOS_XLOG_SYMBOL_PATH + lib_path = OHOS_XLOG_LIBS_PATH + else: + symbol_path = OHOS_SYMBOL_PATH + lib_path = OHOS_LIBS_PATH + + if not os.path.exists(symbol_path): + os.makedirs(symbol_path) + + symbol_path = symbol_path + arch + if os.path.exists(symbol_path): + shutil.rmtree(symbol_path) + + os.mkdir(symbol_path) + + + if not os.path.exists(lib_path): + os.makedirs(lib_path) + + lib_path = lib_path + arch + if os.path.exists(lib_path): + shutil.rmtree(lib_path) + + os.mkdir(lib_path) + + + for f in glob.glob(OHOS_LIBS_INSTALL_PATH + "*.so"): + shutil.copy(f, symbol_path) + shutil.copy(f, lib_path) + + # copy stl + shutil.copy(OHOS_STL_FILE[arch], symbol_path) + shutil.copy(OHOS_STL_FILE[arch], lib_path) + + + print('==================Output========================') + print('libs(release): %s' %(lib_path)) + print('symbols(must store permanently): %s' %(symbol_path)) + + + after_time = time.time() + + print("use time:%d s" % (int(after_time - before_time))) + return True + +def main(incremental, archs, target_option='', tag=''): + gen_mars_revision_file(SCRIPT_PATH + '/comm', tag) + + # if os.path.exists(OHOS_LIBS_PATH): + # shutil.rmtree(OHOS_LIBS_PATH) + + # if os.path.exists(OHOS_SYMBOL_PATH): + # shutil.rmtree(OHOS_SYMBOL_PATH) + + for arch in archs: + if not build_ohos(incremental, arch, target_option): + return + +if __name__ == '__main__': + + while True: + if len(sys.argv) >= 3: + archs = sys.argv[2:] + main(False, archs, tag=sys.argv[1]) + break + else: + archs = {'armeabi-v7a', 'arm64-v8a'} + num = input('Enter menu:\n1. Clean && build mars.\n2. Build incrementally mars.\n3. Clean && build xlog.\n4. Exit\n') + if num == '1': + main(False, archs) + break + elif num == '2': + main(True, archs) + break + elif num == '3': + main(False, archs, '--target libzstd_static marsxlog') + break + elif num == '4': + break + else: + main(False, archs) + break + + diff --git a/mars/comm/CMakeLists.txt b/mars/comm/CMakeLists.txt index 8438f75e2..919ea4515 100644 --- a/mars/comm/CMakeLists.txt +++ b/mars/comm/CMakeLists.txt @@ -7,7 +7,10 @@ project (comm) include(CMakeUtils.txt) include(CMakeExtraFlags.txt) - +if(OHOS) + message("common ohos...") + add_definitions(-DOHOS) +endif() include_directories(.) include_directories(..) include_directories(../..) diff --git a/mars/comm/assert/__assert.h b/mars/comm/assert/__assert.h index 2667c28cc..e3e70b336 100644 --- a/mars/comm/assert/__assert.h +++ b/mars/comm/assert/__assert.h @@ -31,7 +31,11 @@ #define ASSERT2(e, fmt, ...) ((e) ? (void)0 : __ASSERT2(__FILE__, __LINE__, __func__, #e, fmt, ##__VA_ARGS__)) #define ASSERTV2(e, fmt, valist) ((e) ? (void)0 : __ASSERTV2(__FILE__, __LINE__, __func__, #e, fmt, valist)) +#ifdef OHOS +__BEGIN_EXTERN_C +#else __BEGIN_DECLS +#endif void ENABLE_ASSERT(); void DISABLE_ASSERT(); int IS_ASSERT_ENABLE(); @@ -49,6 +53,9 @@ __attribute__((__nonnull__(1, 3, 4, 5))) __attribute__((__format__(printf, 5, 0) const char*, const char*, va_list); +#ifdef OHOS +__END_EXTERN_C +#else __END_DECLS - +#endif #endif /* COMM_COMM_ASSERT_H_ */ diff --git a/mars/comm/unique_resource_factory.cc b/mars/comm/unique_resource_factory.cc index 21cafa7b9..a3c1080c8 100644 --- a/mars/comm/unique_resource_factory.cc +++ b/mars/comm/unique_resource_factory.cc @@ -36,7 +36,9 @@ std::wstring string2wstring(const std::string& input) { #else #include +#ifndef OHOS #include +#endif #include #define READFLAG (O_RDONLY) diff --git a/mars/comm/unix/thread/thread.h b/mars/comm/unix/thread/thread.h index eedfda576..bb6d0eaf7 100644 --- a/mars/comm/unix/thread/thread.h +++ b/mars/comm/unix/thread/thread.h @@ -372,7 +372,7 @@ class Thread { ASSERT2(false, "In Android, use SIGUSR2(handler call pthread_exit) to pthread_cancel"); return kill(SIGUSR2); #else - return pthread_cancel(tid()); + //return pthread_cancel(tid()); #endif } diff --git a/mars/comm/verinfo.h b/mars/comm/verinfo.h index f707e7eef..af37975e9 100644 --- a/mars/comm/verinfo.h +++ b/mars/comm/verinfo.h @@ -2,10 +2,10 @@ #ifndef Mars_verinfo_h #define Mars_verinfo_h -#define MARS_REVISION "${mars_rev}" -#define MARS_PATH "${mars_branch}" +#define MARS_REVISION "a7e9d522" +#define MARS_PATH "harmony-xlog" #define MARS_URL "" -#define MARS_BUILD_TIME "20231114" -#define MARS_TAG "1.0.0-SNAPSHOT" +#define MARS_BUILD_TIME "2024-02-18 14:13:41" +#define MARS_TAG "" #endif diff --git a/mars/ohoslibraries/export.exp b/mars/ohoslibraries/export.exp new file mode 100644 index 000000000..dac4d2ca9 --- /dev/null +++ b/mars/ohoslibraries/export.exp @@ -0,0 +1,51 @@ +{ +global: + xlogger_Write; + xlogger_dump; + xlogger_memory_dump; + xlogger_IsEnabledFor; + xlogger_Assert; + xlogger_AssertP; + xlogger_pid; + xlogger_tid; + xlogger_maintid; + xlogger_SetLevel; + xlogger_SetAppender; + xlogger_VPrint; + xlogger_Level; + + __xlogger_Level_impl; + __xlogger_SetLevel_impl; + __xlogger_IsEnabledFor_impl; + __xlogger_SetAppender_impl; + __xlogger_AssertP_impl; + __xlogger_Assert_impl; + __xlogger_VPrint_impl; + __xlogger_Print_impl; + __xlogger_Write_impl; + + + *appender_*; + *LogLogic*; + *LogBuffer*; + + + *mars*xlog*NewXloggerInstance*; + *mars*xlog*GetXloggerInstance*; + *mars*xlog*ReleaseXloggerInstance*; + *mars*xlog*XloggerWrite*; + *mars*xlog*IsEnabledFor*; + *mars*xlog*GetLevel*; + *mars*xlog*SetLevel*; + *mars*xlog*SetAppenderMode*; + *mars*xlog*Flush*; + *mars*xlog*FlushAll*; + *mars*xlog*SetConsoleLogOpen*; + *mars*xlog*SetMaxFileSize*; + *mars*xlog*SetMaxAliveTime*; + +local: + _Z22appender_set_console_logb; + _Z24appender_set_console_logb; + *; +}; diff --git a/mars/ohoslibraries/mars_ohos_sdk/libs/arm64-v8a/libc++_shared.so b/mars/ohoslibraries/mars_ohos_sdk/libs/arm64-v8a/libc++_shared.so new file mode 100644 index 000000000..1a89dd709 Binary files /dev/null and b/mars/ohoslibraries/mars_ohos_sdk/libs/arm64-v8a/libc++_shared.so differ diff --git a/mars/ohoslibraries/mars_ohos_sdk/libs/arm64-v8a/libmarsxlog.so b/mars/ohoslibraries/mars_ohos_sdk/libs/arm64-v8a/libmarsxlog.so new file mode 100755 index 000000000..d5527891f Binary files /dev/null and b/mars/ohoslibraries/mars_ohos_sdk/libs/arm64-v8a/libmarsxlog.so differ diff --git a/mars/ohoslibraries/mars_ohos_sdk/libs/armeabi-v7a/libc++_shared.so b/mars/ohoslibraries/mars_ohos_sdk/libs/armeabi-v7a/libc++_shared.so new file mode 100644 index 000000000..4b8338380 Binary files /dev/null and b/mars/ohoslibraries/mars_ohos_sdk/libs/armeabi-v7a/libc++_shared.so differ diff --git a/mars/ohoslibraries/mars_ohos_sdk/libs/armeabi-v7a/libmarsxlog.so b/mars/ohoslibraries/mars_ohos_sdk/libs/armeabi-v7a/libmarsxlog.so new file mode 100755 index 000000000..db20a95e9 Binary files /dev/null and b/mars/ohoslibraries/mars_ohos_sdk/libs/armeabi-v7a/libmarsxlog.so differ diff --git a/mars/ohoslibraries/mars_xlog_sdk/import.cc b/mars/ohoslibraries/mars_xlog_sdk/import.cc new file mode 100644 index 000000000..75a133f25 --- /dev/null +++ b/mars/ohoslibraries/mars_xlog_sdk/import.cc @@ -0,0 +1,9 @@ +/** + * created on : 2012-07-19 + * author : yanguoyue + */ + +void ImportLibs() { + extern void ExportXlog(); + ExportXlog(); +} diff --git a/mars/openssl/openssl_lib_ohos/arm64-v8a/libcrypto.a b/mars/openssl/openssl_lib_ohos/arm64-v8a/libcrypto.a new file mode 100644 index 000000000..b7925b34c Binary files /dev/null and b/mars/openssl/openssl_lib_ohos/arm64-v8a/libcrypto.a differ diff --git a/mars/openssl/openssl_lib_ohos/arm64-v8a/libssl.a b/mars/openssl/openssl_lib_ohos/arm64-v8a/libssl.a new file mode 100644 index 000000000..cde32cf60 Binary files /dev/null and b/mars/openssl/openssl_lib_ohos/arm64-v8a/libssl.a differ diff --git a/mars/openssl/openssl_lib_ohos/armeabi-v7a/libcrypto.a b/mars/openssl/openssl_lib_ohos/armeabi-v7a/libcrypto.a new file mode 100644 index 000000000..f8c0911c8 Binary files /dev/null and b/mars/openssl/openssl_lib_ohos/armeabi-v7a/libcrypto.a differ diff --git a/mars/openssl/openssl_lib_ohos/armeabi-v7a/libssl.a b/mars/openssl/openssl_lib_ohos/armeabi-v7a/libssl.a new file mode 100644 index 000000000..c9c88b476 Binary files /dev/null and b/mars/openssl/openssl_lib_ohos/armeabi-v7a/libssl.a differ diff --git a/mars/xlog/CMakeLists.txt b/mars/xlog/CMakeLists.txt index 02ef949be..e53704312 100644 --- a/mars/xlog/CMakeLists.txt +++ b/mars/xlog/CMakeLists.txt @@ -11,7 +11,9 @@ set(SELF_LIBS_OUT ${CMAKE_SYSTEM_NAME}.out) include(../comm/CMakeUtils.txt) include(../comm/CMakeExtraFlags.txt) - +if(OHOS) + add_definitions(-DOHOS) +endif() include_directories(.) include_directories(src) include_directories(..) @@ -59,7 +61,13 @@ elseif(ANDROID) get_filename_component(EXPORT_EXP_FILE jni/export.exp ABSOLUTE) set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--version-script=${EXPORT_EXP_FILE}") - +elseif(OHOS) + include_directories(../comm/ohos) + file(GLOB SELF_OHOS_SRC_FILES RELATIVE ${PROJECT_SOURCE_DIR} + ../comm/xlogger/xloggerbase.c + ../comm/xlogger/xlogger.cc + ohos/*.cc) + list(APPEND SELF_SRC_FILES ${SELF_OHOS_SRC_FILES}) endif() # add_library(${PROJECT_NAME} STATIC ${SELF_SRC_FILES}) diff --git a/mars/xlog/crypt/micro-ecc-master/asm_arm.inc b/mars/xlog/crypt/micro-ecc-master/asm_arm.inc index 688fdc75a..89ccb5d4c 100755 --- a/mars/xlog/crypt/micro-ecc-master/asm_arm.inc +++ b/mars/xlog/crypt/micro-ecc-master/asm_arm.inc @@ -38,7 +38,7 @@ #if (uECC_PLATFORM == uECC_arm_thumb2) #define RESUME_SYNTAX #else - #define RESUME_SYNTAX ".syntax divided \n\t" + #define RESUME_SYNTAX ".syntax unified \n\t" #endif #if (uECC_OPTIMIZATION_LEVEL >= 2) @@ -620,7 +620,7 @@ uECC_VLI_API void uECC_vli_mult(uECC_word_t *result, "str %[r3], [%[r0], %[r6]] \n\t" /* result[num_words * 2 - 1] = c0 */ "pop {%[r0]} \n\t" /* pop result off the stack */ - ".syntax divided \n\t" + ".syntax unified \n\t" : [r3] "+l" (num_words), [r4] "=&l" (r4), [r5] "=&l" (r5), [r6] "=&l" (r6), [r7] "=&l" (r7) : [r0] "l" (result), [r1] "l" (left), [r2] "l" (right) @@ -805,7 +805,7 @@ uECC_VLI_API void uECC_vli_square(uECC_word_t *result, "str %[r2], [%[r0], %[r5]] \n\t" /* result[num_words * 2 - 1] = c0 */ "pop {%[r0]} \n\t" /* pop result off the stack */ - ".syntax divided \n\t" + ".syntax unified \n\t" : [r2] "+l" (num_words), [r3] "=&l" (r3), [r4] "=&l" (r4), [r5] "=&l" (r5), [r6] "=&l" (r6), [r7] "=&l" (r7) : [r0] "l" (result), [r1] "l" (left) diff --git a/mars/xlog/ohos/ConsoleLog.cc b/mars/xlog/ohos/ConsoleLog.cc new file mode 100644 index 000000000..77a9fcde0 --- /dev/null +++ b/mars/xlog/ohos/ConsoleLog.cc @@ -0,0 +1,31 @@ +#include "hilog/log.h" +#include +#include + +#include "mars/comm/autobuffer.h" +#include "mars/comm/xlogger/loginfo_extract.h" +#include "mars/comm/xlogger/xloggerbase.h" + + +namespace mars { +namespace xlog { +#define LOG_DOMAIN 0x0201 +//这里不能加日志,会导致循环调用 +void ConsoleLog(const XLoggerInfo* _info, const char* _log) { + char result_log[16 * 1024] = {0}; + if (_info) { + const char* filename = ExtractFileName(_info->filename); + const char* strFuncName = NULL == _info->func_name ? "" : _info->func_name; + OH_LOG_Print(LOG_APP,LOG_WARN, LOG_DOMAIN, _info->tag ? _info->tag : "", "[%s:%d, %s]:%s", + filename, + _info->line, + strFuncName, + _log ? _log : "NULL==log!!!"); + } else { + snprintf(result_log, sizeof(result_log), "%s", _log ? _log : "NULL==log!!!"); + OH_LOG_Print(LOG_APP,LOG_WARN, LOG_DOMAIN, "", "%s", result_log); + } +} + +} // namespace xlog +} // namespace mars \ No newline at end of file diff --git a/mars/xlog/ohos/Js2C_Xlog.cc b/mars/xlog/ohos/Js2C_Xlog.cc new file mode 100644 index 000000000..a2627ef84 --- /dev/null +++ b/mars/xlog/ohos/Js2C_Xlog.cc @@ -0,0 +1,285 @@ +// +// Created on 2024/1/17. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". + +#include "napi/native_api.h" +#include "napi_utils.h" +#include "xlogger.h" +#include "xlogger_interface.h" +#include +#include "hilog/log.h" +#define LOG_DOMAIN 0x0201 +#define LOG_TAG "MY_TAG" +const int32_t STR_DEFAULT_SIZE = 2048; +#define LONGTHREADID2INT(a) ((a >> 32) ^ ((a & 0xFFFF))) +static napi_value newXlogInstance(napi_env env, napi_callback_info info) { + size_t argc = 9; + napi_value args[9] = {nullptr}; + + OH_LOG_Print(LOG_APP, LOG_WARN, LOG_DOMAIN, LOG_TAG, "newXlogInstance"); + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + int level; + napi_get_value_int32(env, args[0], &level); + int mode; + napi_get_value_int32(env, args[1], &mode); + std::string logDir; + NapiUtil::JsValueToString(env, args[2], STR_DEFAULT_SIZE, logDir); + std::string nameprefix; + NapiUtil::JsValueToString(env, args[3], STR_DEFAULT_SIZE, nameprefix); + std::string pubkey; + NapiUtil::JsValueToString(env, args[4], STR_DEFAULT_SIZE, pubkey); + int compressmode; + napi_get_value_int32(env, args[5], &compressmode); + int compresslevel; + napi_get_value_int32(env, args[6], &compresslevel); + std::string cachedir; + NapiUtil::JsValueToString(env, args[7], STR_DEFAULT_SIZE, cachedir); + int cachedays; + napi_get_value_int32(env, args[8], &cachedays); + + mars::xlog::XLogConfig config = { + (mars::xlog::TAppenderMode)mode, logDir.c_str(), nameprefix.c_str(), pubkey.c_str(), + (mars::xlog::TCompressMode)compressmode, compresslevel, cachedir.c_str(), cachedays}; + mars::comm::XloggerCategory *category = mars::xlog::NewXloggerInstance(config, (TLogLevel)level); + if (nullptr == category) { + return 0; + } + OH_LOG_Print(LOG_APP,LOG_WARN, LOG_DOMAIN, LOG_TAG, "[%s:%d, %s]:%s", + "a", + 0, + "a", + "newXlogInstance"); + napi_value categoryPointer; + napi_create_int64(env, reinterpret_cast(category), &categoryPointer); + return categoryPointer; +} +static napi_value getXlogInstance(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + std::string nameprefix; + NapiUtil::JsValueToString(env, args[0], STR_DEFAULT_SIZE, nameprefix); + mars::comm::XloggerCategory *category = mars::xlog::GetXloggerInstance(nameprefix.c_str()); + if (nullptr == category) { + return 0; + } + napi_value result; + napi_create_int64(env, reinterpret_cast(category), &result); + return result; +} +static napi_value releaseXlogInstance(napi_env env, napi_callback_info info) { + + size_t argc = 1; + napi_value args[1] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + std::string nameprefix; + NapiUtil::JsValueToString(env, args[0], STR_DEFAULT_SIZE, nameprefix); + mars::xlog::ReleaseXloggerInstance(nameprefix.c_str()); + return nullptr; +} + +static napi_value appenderOpen(napi_env env, napi_callback_info info) { + size_t argc = 9; + napi_value args[9] = {nullptr}; + OH_LOG_Print(LOG_APP, LOG_WARN, LOG_DOMAIN, LOG_TAG, "appenderOpen111"); + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + int level; + napi_get_value_int32(env, args[0], &level); + int mode; + napi_get_value_int32(env, args[1], &mode); + std::string logDir; + NapiUtil::JsValueToString(env, args[2], STR_DEFAULT_SIZE, logDir); + std::string nameprefix; + NapiUtil::JsValueToString(env, args[3], STR_DEFAULT_SIZE, nameprefix); + std::string pubkey; + NapiUtil::JsValueToString(env, args[4], STR_DEFAULT_SIZE, pubkey); + int compressmode; + napi_get_value_int32(env, args[5], &compressmode); + int compresslevel; + napi_get_value_int32(env, args[6], &compresslevel); + std::string cachedir; + NapiUtil::JsValueToString(env, args[7], STR_DEFAULT_SIZE, cachedir); + int cachedays; + napi_get_value_int32(env, args[8], &cachedays); + OH_LOG_Print(LOG_APP, LOG_WARN, LOG_DOMAIN, LOG_TAG, "appenderOpen2222"); + mars::xlog::XLogConfig config = {(mars::xlog::TAppenderMode)mode, logDir.c_str(),nameprefix.c_str(),pubkey.c_str(), + (mars::xlog::TCompressMode)compressmode, compresslevel, cachedir.c_str(), cachedays}; + appender_open(config); + OH_LOG_Print(LOG_APP, LOG_WARN, LOG_DOMAIN, LOG_TAG, "appenderOpen3333"); + xlogger_SetLevel((TLogLevel)level); + return nullptr; +} + +static napi_value appenderClose(napi_env env, napi_callback_info info) { + mars::xlog::appender_close(); + return nullptr; +} +static napi_value appenderFlush(napi_env env, napi_callback_info info) { + size_t argc = 2; + napi_value args[2] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + int64_t _log_instance_ptr; + napi_get_value_int64(env, args[0], &_log_instance_ptr); + bool _is_sync; + napi_get_value_bool(env, args[1], &_is_sync); + mars::xlog::Flush(_log_instance_ptr, _is_sync); + return nullptr; +} +static napi_value logWrite2(napi_env env, napi_callback_info info) { + size_t argc = 10; + napi_value args[10] = {nullptr}; +OH_LOG_Print(LOG_APP, LOG_WARN, LOG_DOMAIN, LOG_TAG, "logWriter2"); + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + int64_t _log_instance_ptr; + napi_get_value_int64(env, args[0], &_log_instance_ptr); + int level; + napi_get_value_int32(env, args[1], &level); + + std::string tag; + NapiUtil::JsValueToString(env, args[2], STR_DEFAULT_SIZE, tag); + std::string filename; + NapiUtil::JsValueToString(env, args[3], STR_DEFAULT_SIZE, filename); + + std::string funcname; + NapiUtil::JsValueToString(env, args[4], STR_DEFAULT_SIZE, funcname); + + int line; + napi_get_value_int32(env, args[5], &line); + + int64_t pid; + napi_get_value_int64(env, args[6], &pid); + + int64_t tid; + napi_get_value_int64(env, args[7], &tid); + + int maintid; + napi_get_value_int32(env, args[8], &maintid); + + std::string log; + NapiUtil::JsValueToString(env, args[9], STR_DEFAULT_SIZE, log); + + + XLoggerInfo xlog_info = XLOGGER_INFO_INITIALIZER; + gettimeofday(&xlog_info.timeval, NULL); + xlog_info.level = (TLogLevel)level; + xlog_info.line = (int)line; + xlog_info.pid = (int)pid; + xlog_info.tid = LONGTHREADID2INT(tid); + xlog_info.maintid = LONGTHREADID2INT(maintid); + + xlog_info.tag = NULL == tag.c_str() ? "" : tag.c_str(); + xlog_info.filename = NULL == filename.c_str() ? "" : filename.c_str(); + xlog_info.func_name = NULL == funcname.c_str() ? "" : funcname.c_str(); +OH_LOG_Print(LOG_APP, LOG_WARN, LOG_DOMAIN, LOG_TAG, "logWriter2 11111"); + mars::xlog::XloggerWrite(_log_instance_ptr, &xlog_info, NULL == log.c_str() ? "NULL == log" : log.c_str()); +OH_LOG_Print(LOG_APP, LOG_WARN, LOG_DOMAIN, LOG_TAG, "logWriter2 22222"); + return nullptr; +} +static napi_value getLogLevel(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + int64_t _log_instance_ptr; + napi_get_value_int64(env, args[0], &_log_instance_ptr); + napi_value result; + napi_create_int32(env, mars::xlog::GetLevel(_log_instance_ptr), &result); + return result; +} +static napi_value setAppenderMode(napi_env env, napi_callback_info info) { + size_t argc = 2; + napi_value args[2] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + int64_t _log_instance_ptr; + napi_get_value_int64(env, args[0], &_log_instance_ptr); + + int mode; + napi_get_value_int32(env, args[1], &mode); + + mars::xlog::SetAppenderMode(_log_instance_ptr, (mars::xlog::TAppenderMode)mode); + return nullptr; +} +static napi_value setConsoleLogOpen(napi_env env, napi_callback_info info) { + size_t argc = 2; + napi_value args[2] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + int64_t _log_instance_ptr; + napi_get_value_int64(env, args[0], &_log_instance_ptr); + int _is_open; + napi_get_value_int32(env, args[1], &_is_open); + + mars::xlog::SetConsoleLogOpen(_log_instance_ptr, _is_open); + + return nullptr; +} + +static napi_value setMaxFileSize(napi_env env, napi_callback_info info) { + size_t argc = 2; + napi_value args[2] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + int64_t _log_instance_ptr; + napi_get_value_int64(env, args[0], &_log_instance_ptr); + int _max_size; + napi_get_value_int32(env, args[1], &_max_size); + + mars::xlog::SetMaxFileSize(_log_instance_ptr, _max_size); + return nullptr; +} +static napi_value setMaxAliveTime(napi_env env, napi_callback_info info) { + size_t argc = 2; + napi_value args[2] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + int64_t _log_instance_ptr; + napi_get_value_int64(env, args[0], &_log_instance_ptr); + int _max_time; + napi_get_value_int32(env, args[1], &_max_time); + + mars::xlog::SetMaxAliveTime(_log_instance_ptr, _max_time); + return nullptr; +} +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) { + napi_property_descriptor desc[] = { + {"newXlogInstance", nullptr, newXlogInstance, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"getXlogInstance", nullptr, getXlogInstance, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"releaseXlogInstance", nullptr, releaseXlogInstance, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"getLogLevel", nullptr, getLogLevel, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"appenderClose", nullptr, appenderClose, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"appenderFlush", nullptr, appenderFlush, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"logWrite2", nullptr, logWrite2, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"setAppenderMode", nullptr, setAppenderMode, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"setConsoleLogOpen", nullptr, setConsoleLogOpen, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"appenderOpen", nullptr, appenderOpen, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"setMaxFileSize", nullptr, setMaxFileSize, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"setMaxAliveTime", nullptr, setMaxAliveTime, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + OH_LOG_Print(LOG_APP, LOG_WARN, LOG_DOMAIN, LOG_TAG, "init ..."); + return exports; +} +EXTERN_C_END + + static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "xlog", + .nm_priv = ((void *)0), + .reserved = {0}, + }; + + extern "C" __attribute__((constructor)) void RegisterXlogModule(void) { napi_module_register(&demoModule); } + +void ExportXlog() { +} \ No newline at end of file diff --git a/mars/xlog/ohos/napi_utils.cc b/mars/xlog/ohos/napi_utils.cc new file mode 100644 index 000000000..556cff7d1 --- /dev/null +++ b/mars/xlog/ohos/napi_utils.cc @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi_utils.h" +#include +#include +#include +#include +#include + +const int32_t MAX_STR_LENGTH = 2048; +void NapiUtil::JsValueToString(const napi_env & env, const napi_value & value, const int32_t bufLen, std::string & target) +{ + if (bufLen <= 0 || bufLen > MAX_STR_LENGTH) { + return; + } + + std::unique_ptr < char[] > buf = std::make_unique < char[] >(bufLen); + if (buf.get() == nullptr) { + return; + } + (void)memset(buf.get(), 0, bufLen); + size_t result = 0; + napi_get_value_string_utf8(env, value, buf.get(), bufLen, &result); + target = buf.get(); +} + +napi_value NapiUtil::SetNapiCallInt32(const napi_env & env, const int32_t intValue) +{ + napi_value result; + napi_create_int32(env, intValue, &result); + return result; +} + +napi_value NapiUtil::SetNapiCallBool(napi_env env, bool value) +{ + napi_value temp; + napi_value result; + napi_create_int32(env, value == true ? 1 : 0, &temp); + napi_coerce_to_bool(env, temp, &result); + return result; +} + +int NapiUtil::StringToInt(std::string value){ + return atoi((char*)value.c_str()); +} + +int NapiUtil::StringToLong(std::string value){ + return atol((char*)value.c_str()); +} + +float NapiUtil::StringToFloat(std::string value){ + return std::stof(value); +} + +bool NapiUtil::StringToBool(const std::string value){ + return value=="true" ? true:false; +} + diff --git a/mars/xlog/ohos/napi_utils.h b/mars/xlog/ohos/napi_utils.h new file mode 100644 index 000000000..a2853fd7b --- /dev/null +++ b/mars/xlog/ohos/napi_utils.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ijkplayer_napi_utils_H +#define ijkplayer_napi_utils_H +#include +#include +#include "native_common.h" + +class NapiUtil { +public: + static void JsValueToString(const napi_env &env, const napi_value &value, const int32_t bufLen, + std::string &target); + static napi_value SetNapiCallInt32(const napi_env & env, const int32_t intValue); + static napi_value SetNapiCallBool(const napi_env env, bool value); + static int StringToInt(std::string value); + static int StringToLong(std::string value); + static float StringToFloat(std::string value); + static bool StringToBool(std::string value); +}; + + +#endif //ijkplayer_napi_utils_H diff --git a/mars/xlog/ohos/native_common.h b/mars/xlog/ohos/native_common.h new file mode 100644 index 000000000..9f04b1aeb --- /dev/null +++ b/mars/xlog/ohos/native_common.h @@ -0,0 +1,84 @@ + +#ifndef _NATIVE_COMMON_H_ +#define _NATIVE_COMMON_H_ + +#define NAPI_RETVAL_NOTHING + +#define GET_AND_THROW_LAST_ERROR(env) \ + do { \ + const napi_extended_error_info* errorInfo = nullptr; \ + napi_get_last_error_info((env), &errorInfo); \ + bool isPending = false; \ + napi_is_exception_pending((env), &isPending); \ + if (!isPending && errorInfo != nullptr) { \ + const char* errorMessage = \ + errorInfo->error_message != nullptr ? errorInfo->error_message : "empty error message"; \ + napi_throw_error((env), nullptr, errorMessage); \ + } \ + } while (0) + +#define NAPI_ASSERT_BASE(env, assertion, message, retVal) \ + do { \ + if (!(assertion)) { \ + napi_throw_error((env), nullptr, "assertion (" #assertion ") failed: " message); \ + return retVal; \ + } \ + } while (0) + +#define NAPI_ASSERT(env, assertion, message) NAPI_ASSERT_BASE(env, assertion, message, nullptr) + +#define NAPI_ASSERT_RETURN_VOID(env, assertion, message) NAPI_ASSERT_BASE(env, assertion, message, NAPI_RETVAL_NOTHING) + +#define NAPI_CALL_BASE(env, theCall, retVal) \ + do { \ + if ((theCall) != napi_ok) { \ + GET_AND_THROW_LAST_ERROR((env)); \ + return retVal; \ + } \ + } while (0) + +#define NAPI_CALL(env, theCall) NAPI_CALL_BASE(env, theCall, nullptr) + +#define NAPI_CALL_RETURN_VOID(env, theCall) NAPI_CALL_BASE(env, theCall, NAPI_RETVAL_NOTHING) + +#define DECLARE_NAPI_PROPERTY(name, val) \ + { \ + (name), nullptr, nullptr, nullptr, nullptr, val, napi_default, nullptr \ + } + +#define DECLARE_NAPI_STATIC_PROPERTY(name, val) \ + { \ + (name), nullptr, nullptr, nullptr, nullptr, val, napi_static, nullptr \ + } + +#define DECLARE_NAPI_FUNCTION(name, func) \ + { \ + (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, nullptr \ + } + +#define DECLARE_NAPI_FUNCTION_WITH_DATA(name, func, data) \ + { \ + (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, data \ + } + +#define DECLARE_NAPI_STATIC_FUNCTION(name, func) \ + { \ + (name), nullptr, (func), nullptr, nullptr, nullptr, napi_static, nullptr \ + } + +#define DECLARE_NAPI_GETTER(name, getter) \ + { \ + (name), nullptr, nullptr, (getter), nullptr, nullptr, napi_default, nullptr \ + } + +#define DECLARE_NAPI_SETTER(name, setter) \ + { \ + (name), nullptr, nullptr, nullptr, (setter), nullptr, napi_default, nullptr \ + } + +#define DECLARE_NAPI_GETTER_SETTER(name, getter, setter) \ + { \ + (name), nullptr, nullptr, (getter), (setter), nullptr, napi_default, nullptr \ + } + +#endif /* _NATIVE_COMMON_H_ */