Commit b9c31bfa authored by dsq's avatar dsq

日志触发

parent af7fc89e
# the minimum version of CMake. # the minimum version of CMake.
cmake_minimum_required(VERSION 3.5.0) cmake_minimum_required(VERSION 3.5.0)
project(harmonyApp) project(KmpDemo)
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
...@@ -9,7 +9,22 @@ if(DEFINED PACKAGE_FIND_FILE) ...@@ -9,7 +9,22 @@ if(DEFINED PACKAGE_FIND_FILE)
endif() endif()
include_directories(${NATIVERENDER_ROOT_PATH} include_directories(${NATIVERENDER_ROOT_PATH}
${NATIVERENDER_ROOT_PATH}/include) ${NATIVERENDER_ROOT_PATH}/include)
add_library(entry SHARED napi_init.cpp) add_library(entry SHARED napi_init.cpp)
target_link_libraries(entry PUBLIC libace_napi.z.so)
\ No newline at end of file # 按 ABI 选 libkn.so 路径(当前 abiFilters 仅 x86_64 时只构建模拟器)
if(CMAKE_OHOS_ARCH_ABI STREQUAL "x86_64" OR OHOS_ARCH STREQUAL "x86_64")
set(LIBKN_ABI_DIR "x86_64")
else()
set(LIBKN_ABI_DIR "arm64-v8a")
endif()
add_library(kn SHARED IMPORTED)
set_target_properties(kn PROPERTIES
IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${LIBKN_ABI_DIR}/libkn.so"
)
message(STATUS "Loading libkn.so for ${LIBKN_ABI_DIR} architecture")
target_link_libraries(entry PUBLIC libace_napi.z.so)
target_link_libraries(entry PRIVATE kn)
...@@ -73,6 +73,7 @@ typedef struct { ...@@ -73,6 +73,7 @@ typedef struct {
libkn_KNativePtr pinned; libkn_KNativePtr pinned;
} libkn_kref_com_dong_nocomposedemo_OhosPlatform; } libkn_kref_com_dong_nocomposedemo_OhosPlatform;
extern void FaultOut();
extern libkn_KInt hiLogPrintMsg(libkn_KUInt type, libkn_KUInt level, libkn_KUInt domain, const char* tag, const char* message); extern libkn_KInt hiLogPrintMsg(libkn_KUInt type, libkn_KUInt level, libkn_KUInt domain, const char* tag, const char* message);
extern void pirntAllLevelHiLog(); extern void pirntAllLevelHiLog();
extern void pirntRandomHiLog(); extern void pirntRandomHiLog();
...@@ -130,7 +131,7 @@ typedef struct { ...@@ -130,7 +131,7 @@ typedef struct {
const char* (*get_name)(libkn_kref_com_dong_nocomposedemo_OhosPlatform thiz); const char* (*get_name)(libkn_kref_com_dong_nocomposedemo_OhosPlatform thiz);
} OhosPlatform; } OhosPlatform;
libkn_kref_com_dong_nocomposedemo_Platform (*getPlatform)(); libkn_kref_com_dong_nocomposedemo_Platform (*getPlatform)();
void (*FaultOut)(); void (*FaultOut_)();
libkn_KInt (*hiLogPrintMsg_)(libkn_KUInt type, libkn_KUInt level, libkn_KUInt domain, const char* tag, const char* message); libkn_KInt (*hiLogPrintMsg_)(libkn_KUInt type, libkn_KUInt level, libkn_KUInt domain, const char* tag, const char* message);
void (*pirntAllLevelHiLog_)(); void (*pirntAllLevelHiLog_)();
void (*pirntRandomHiLog_)(); void (*pirntRandomHiLog_)();
......
#include "napi/native_api.h" #include "napi/native_api.h"
#include "libkn_api.h"
static napi_value Add(napi_env env, napi_callback_info info) static napi_value Add(napi_env env, napi_callback_info info)
{ {
...@@ -26,11 +27,55 @@ static napi_value Add(napi_env env, napi_callback_info info) ...@@ -26,11 +27,55 @@ static napi_value Add(napi_env env, napi_callback_info info)
} }
// pirntSingleHiLog 包装函数
static napi_value PirntSingleHiLog(napi_env env, napi_callback_info info)
{
pirntSingleHiLog();
napi_value undefined;
napi_get_undefined(env, &undefined);
return undefined;
}
// pirntAllLevelHiLog 包装函数
static napi_value PirntAllLevelHiLog(napi_env env, napi_callback_info info)
{
pirntAllLevelHiLog();
napi_value undefined;
napi_get_undefined(env, &undefined);
return undefined;
}
// pirntRandomHiLog 包装函数
static napi_value PirntRandomHiLog(napi_env env, napi_callback_info info)
{
pirntRandomHiLog();
napi_value undefined;
napi_get_undefined(env, &undefined);
return undefined;
}
// FaultOut 包装函数
static napi_value FaultOut(napi_env env, napi_callback_info info)
{
FaultOut();
napi_value undefined;
napi_get_undefined(env, &undefined);
return undefined;
}
EXTERN_C_START EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) static napi_value Init(napi_env env, napi_value exports)
{ {
napi_property_descriptor desc[] = { napi_property_descriptor desc[] = {
{ "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr } { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "pirntSingleHiLog", nullptr, PirntSingleHiLog, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "pirntAllLevelHiLog", nullptr, PirntAllLevelHiLog, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "pirntRandomHiLog", nullptr, PirntRandomHiLog, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "FaultOut", nullptr, FaultOut, nullptr, nullptr, nullptr, napi_default, nullptr }
}; };
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports; return exports;
......
export const add: (a: number, b: number) => number; export interface testNapi {
\ No newline at end of file add: (value1: number, value2: number) => number;
pirntSingleHiLog: () => void;
pirntAllLevelHiLog: () => void;
pirntRandomHiLog: () => void;
FaultOut: () => void;
}
declare const testNapi: testNapi;
export default testNapi;
\ No newline at end of file
...@@ -9,18 +9,55 @@ struct Index { ...@@ -9,18 +9,55 @@ struct Index {
@State message: string = 'Hello World'; @State message: string = 'Hello World';
build() { build() {
Row() { Column() {
Column() { // 标题
Text(this.message) Text('日志测试模块')
.fontSize($r('app.float.page_text_font_size')) .id('LogTitle')
.fontWeight(FontWeight.Bold) .fontSize(24)
.onClick(() => { .fontWeight(FontWeight.Bold)
this.message = 'Welcome'; .margin({ top: 50, bottom: 30 })
hilog.info(DOMAIN, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3));
}) // 单次日志测试按钮
} Button('单次日志测试')
.width('100%') .width('80%')
.height(50)
.backgroundColor('#007DFF')
.margin({ bottom: 20 })
.onClick(() => {
testNapi.pirntSingleHiLog();
})
// 一次打印所有层级日志按钮
Button('一次打印所有层级日志')
.width('80%')
.height(50)
.backgroundColor('#007DFF')
.margin({ bottom: 20 })
.onClick(() => {
testNapi.pirntAllLevelHiLog();
})
// 大量持续日志输出按钮
Button('大量持续日志输出')
.width('80%')
.height(50)
.backgroundColor('#007DFF')
.margin({ bottom: 20 })
.onClick(() => {
testNapi.pirntRandomHiLog();
})
// 越界崩溃测试按钮
Button('越界崩溃测试')
.width('80%')
.height(50)
.backgroundColor('#FF0000')
.onClick(() => {
testNapi.FaultOut();
})
} }
.width('100%')
.height('100%') .height('100%')
} }
} }
...@@ -52,7 +52,15 @@ actual fun pirntAllLevelHiLog(){ ...@@ -52,7 +52,15 @@ actual fun pirntAllLevelHiLog(){
@OptIn(ExperimentalNativeApi::class) @OptIn(ExperimentalNativeApi::class)
@CName("pirntRandomHiLog") @CName("pirntRandomHiLog")
actual fun pirntRandomHiLog() { actual fun pirntRandomHiLog() {
val levels = listOf(3u, 4u, 5u, 6u, 7u) // DEBUG, INFO, WARN, ERROR, FATAL
val randomLevel = levels.random()
OH_LOG_PrintMsg(0u, randomLevel, 4660u, "TestTag", "随机日志:级别=$randomLevel")
} }
@OptIn(ExperimentalNativeApi::class)
@CName("FaultOut")
actual fun FaultOut() { actual fun FaultOut() {
val array = intArrayOf(1, 2, 3)
// 故意访问越界索引,触发崩溃
println(array[5])
} }
\ No newline at end of file
...@@ -52,8 +52,19 @@ actual fun pirntAllLevelHiLog(){ ...@@ -52,8 +52,19 @@ actual fun pirntAllLevelHiLog(){
@OptIn(ExperimentalNativeApi::class) @OptIn(ExperimentalNativeApi::class)
@CName("pirntRandomHiLog") @CName("pirntRandomHiLog")
actual fun pirntRandomHiLog() { actual fun pirntRandomHiLog() {
val levels = listOf(3u, 4u, 5u, 6u, 7u) // DEBUG, INFO, WARN, ERROR, FATAL
val randomLevel = levels.random()
OH_LOG_PrintMsg(0u, randomLevel, 4660u, "TestTag", "随机日志:级别=$randomLevel")
} }
@OptIn(ExperimentalNativeApi::class)
@CName("FaultOut")
actual fun FaultOut() { actual fun FaultOut() {
val array = intArrayOf(1, 2, 3)
// 故意访问越界索引,触发崩溃
println(array[5])
// 备用崩溃方式:空指针异常
val nullStr: String? = null
println(nullStr!!.length)
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment