整理输出日志

This commit is contained in:
JackLee 2026-03-02 20:40:36 +08:00
parent a509aabe9c
commit 2175ab0d5d
4 changed files with 63 additions and 67 deletions

View File

@ -1,18 +1,3 @@
/*
* Copyright (c) 2025 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 "NativeManager.h"
#include <ace/xcomponent/native_interface_xcomponent.h>
#include <cstdint>
@ -23,11 +8,15 @@
#include "arkui/native_node_napi.h"
#include "arkui/native_interface.h"
#include "common.h"
#include <random>
#include <map>
#define COLUMN_MARGIN 10
#define XC_WIDTH 800
#define XC_HEIGHT 600
#define ARG_CNT 2
#ifndef NATIVE_TAG
#define NATIVE_TAG "NativeManager"
#endif
namespace NativeOpenCAX {
// [Start plugin_manager_cpp]
@ -66,6 +55,26 @@ NativeManager::~NativeManager() {
}
pluginManagerMap_.clear();
}
std::string uuid_v4() {
// 使用当前时间戳和随机数生成UUID
auto timestamp = std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_int_distribution<uint64_t> dis(0, std::numeric_limits<uint64_t>::max());
uint64_t random_part = dis(gen);
// 合并时间戳和随机数并转换为UUID格式
uint64_t full_uuid = (timestamp << 32) | (random_part & 0xFFFFFFFF);
std::stringstream ss;
ss << std::hex << std::uppercase << std::setw(8) << std::setfill('0') << (full_uuid & 0xFFFFFFFF)
<< "-" << std::setw(4) << std::setfill('0') << (full_uuid >> 32 & 0xFFFF)
<< "-4" // UUID版本4中间四位固定为04xx
<< std::setw(3) << std::setfill('0') << (random_part >> 48 & 0x0FFF | 0x4000) // 设置版本号和一些随机位
<< "-" << std::setw(4) << std::setfill('0') << (random_part >> 32 & 0xFFFF)
<< "-" << std::setw(12) << std::setfill('0') << (random_part & 0xFFFFFFFFFFFF);
return ss.str();
}
// Surface回调事件
//void OnSurfaceCreatedNative(OH_ArkUI_SurfaceHolder *holder) {
// auto window = OH_ArkUI_XComponent_GetNativeWindow(holder); // 获取native window
@ -232,7 +241,7 @@ ArkUI_NodeHandle CreateNodeHandle(const std::string &tag) {
ArkUI_NumberValue comTypeData[] = {ARKUI_XCOMPONENT_TYPE_SURFACE};
ArkUI_AttributeItem comTypeItem = {comTypeData, 1};
// 组件ID Item
ArkUI_AttributeItem comIdItem = {.string = tag.c_str(), .size = 1};
ArkUI_AttributeItem comIdItem = {.string = uuid_v4().c_str(), .size = 1};
// 组件Surface Size
//ArkUI_NumberValue surfaceSizeData[] = {NAN, NAN};
ArkUI_AttributeItem surfaceSizeItem = {nodeSizeData, 2};
@ -251,25 +260,22 @@ ArkUI_NodeHandle CreateNodeHandle(const std::string &tag) {
nodeAPI->setAttribute(xc, NODE_WIDTH, &surfaceSizeItem);
nodeAPI->setAttribute(xc, NODE_HEIGHT, &surfaceSizeItem);
// 节点ID
ArkUI_AttributeItem nodeIdItem = {.string = "ndkxcomponent", .size = 1};
ArkUI_AttributeItem nodeIdItem = {.string = uuid_v4().c_str(), .size = 1};
nodeAPI->setAttribute(xc, NODE_ID, &nodeIdItem);
auto *nativeXComponent = OH_NativeXComponent_GetNativeXComponent(xc);
if (!nativeXComponent) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "GetNativeXComponent error");
HILOG_ERROR(NATIVE_TAG,"GetNativeXComponent error");
return nodeHandel;
}
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeManager", "GetNativeXComponent success");
// 注册XComponent回调函数
OH_NativeXComponent_RegisterCallback(nativeXComponent, &NativeManager::callback_);
// 组件类型
auto comTypeRet = nodeAPI->getAttribute(xc, NODE_XCOMPONENT_TYPE);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeManager", "com type: %{public}d",
comTypeRet->value[0].i32);
HILOG_INFO(NATIVE_TAG,"XCom type: %{public}d",comTypeRet->value[0].i32);
// 组件ID
auto comIdRet = nodeAPI->getAttribute(xc, NODE_XCOMPONENT_ID);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeManager", "com id: %{public}s", comIdRet->string);
HILOG_INFO(NATIVE_TAG,"XCom ID: %{public}d",comIdRet->string);
// 增加组件到节点
nodeAPI->addChild(nodeHandel, xc);
return nodeHandel;
@ -277,31 +283,29 @@ ArkUI_NodeHandle CreateNodeHandle(const std::string &tag) {
// Native侧创建Node
napi_value NativeManager::createNativeNode(napi_env env, napi_callback_info info) {
if ((env == nullptr) || (info == nullptr)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "CreateNativeNode env or info is null");
HILOG_ERROR(NATIVE_TAG,"CreateNativeNode env or info is null");
return nullptr;
}
size_t argCnt = 2;
napi_value args[2] = {nullptr, nullptr};
//获取传入NodeContent实例
size_t argCnt = 1;
napi_value args[1] = {nullptr};
if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "CreateNativeNode napi_get_cb_info failed");
}
if (argCnt != ARG_CNT) {
napi_throw_type_error(env, NULL, "Wrong number of arguments");
return nullptr;
HILOG_ERROR(NATIVE_TAG,"CreateNativeNode napi_get_cb_info failed");
}
ArkUI_NodeContentHandle _nodeContentHandle = nullptr;
// 获取ArkTS侧创建的NodeContent对象映射到Native侧的
OH_ArkUI_GetNodeContentFromNapiValue(env, args[0], &_nodeContentHandle);
// 查询指定的模块接口名
nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1 *>(
OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));
// 节点名
std::string node_id = value2String(env, args[1]);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeManager", "CreateNativeNode_Node_ID:", node_id.c_str());
// 设置用户自定义数据
int32_t ret = OH_ArkUI_NodeContent_SetUserData(_nodeContentHandle, new std::string(node_id));
nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1 *>(OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));
//下面的代码主要用于创建一个Native侧和arkui侧绑定数据的结构传递传输的操作
//node_data可以理解为一个结构体等等之类的指针
// 生成节点名采用NODE_ID_+UUID
std::string node_data = uuid_v4();
HILOG_INFO(NATIVE_TAG,"NODE_DATA:%{public}d",node_data.c_str());
// 在NodeContent对象上保存自定义数据。
int32_t ret = OH_ArkUI_NodeContent_SetUserData(_nodeContentHandle, new std::string(node_data));
if (ret != ARKUI_ERROR_CODE_NO_ERROR) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "setUserData failed error=%{public}d", ret);
HILOG_ERROR(NATIVE_TAG,"setUserData failed error=%{public}d",ret);
}
if (nodeAPI != nullptr && nodeAPI->createNode != nullptr && nodeAPI->addChild != nullptr) {
auto nodeContentEvent = [](ArkUI_NodeContentEvent *event) {

View File

@ -23,16 +23,25 @@
#include <napi/native_api.h>
#include "hilog/log.h"
#include <thread>
namespace NativeOpenCAX {
/**
* Log print domain.
*/
LOG_APP
LOG_ERROR
DOMAIN
TAG
##__VA_ARGS__ 消息(可为空)
*/
#define DOMAIN 0xD001000
//#define LOGD(fmt, ...) HILOG_DEBUG(DOMAIN, TAG, fmt, ##__VA_ARGS__)
//#define LOGI(fmt, ...) HILOG_INFO(DOMAIN, TAG, fmt, ##__VA_ARGS__)
//#define LOGW(fmt, ...) HILOG_WARN(DOMAIN, TAG, fmt, ##__VA_ARGS__)
//#define LOGE(fmt, ...) HILOG_ERROR(DOMAIN, TAG, fmt, ##__VA_ARGS__)
#define LOG_TYPE 0
const unsigned int LOG_PRINT_DOMAIN = 0xFF00;
} // namespace NativeXComponentSample
#if LOG_TYPE == 0
#define HILOG_DEBUG(TAG, fmt, ...) OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, TAG, fmt, ##__VA_ARGS__)
#define HILOG_INFO(TAG, fmt, ...) OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, TAG, fmt, ##__VA_ARGS__)
#define HILOG_WARN(TAG, fmt, ...) OH_LOG_Print(LOG_APP, LOG_WARN, LOG_PRINT_DOMAIN, TAG, fmt, ##__VA_ARGS__)
#define HILOG_ERROR(TAG, fmt, ...) OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, TAG, fmt, ##__VA_ARGS__)
#elif LOG_TYPE == 1
#define HILOG_DEBUG(TAG, fmt, ...)
#define HILOG_INFO(TAG, fmt, ...)
#define HILOG_WARN(TAG, fmt, ...)
#define HILOG_ERROR(TAG, fmt, ...)
#endif
#endif // NATIVE_XCOMPONENT_COMMON_H

View File

@ -2,5 +2,5 @@ export interface NativeXOpenCAX {
setFrameRate(nodeId: string, min: number, max: number, expected: number): void;
setNeedSoftKeyboard(nodeId: string, need: boolean): void;
}
export function createNativeNode(nodeContent: any, nodeId: string): void;
export function createNativeNode(nodeContent: any): void;
export function loadModel(stepFilePath: string): void;

View File

@ -5,20 +5,6 @@ import {NodeContent} from '@kit.ArkUI';
import NativeOpenCAX from 'libopencax.so';
const DOMAIN = 0x0000;
const TAG = 'ModelView';
function NativeLoadModelTest(){
try {
hilog.info(0x0000, 'ModelView', `[NDK] 模块类型: ${typeof NativeOpenCAX}`);
hilog.info(0x0000, 'ModelView', `[NDK] 模块值: ${JSON.stringify(NativeOpenCAX)}`);
hilog.info(0x0000, 'ModelView', `[NDK] 所有属性: ${Object.keys(NativeOpenCAX).join(', ')}`);
//hilog.info(0x0000, 'ModelView', `[NDK] 模块函数加法测试结果: ${NativeOpenCAX.nativeLoadTest(2, 22)}`);
if (!NativeOpenCAX) throw new Error("模块为 undefined");
} catch (e) {
console.error(`[NDK] 加载失败: ${e.message}`, e);
// 此处会触发你看到的错误
}
}
@Component
export struct ModelView {
@ -30,7 +16,7 @@ export struct ModelView {
private nodeContent: NodeContent = new NodeContent();
aboutToAppear() {
NativeOpenCAX.createNativeNode(this.nodeContent,'CreatNativeNode');
NativeOpenCAX.createNativeNode(this.nodeContent);
this.copyRawFileToSandbox();
}
async copyRawFileToSandbox() {
@ -71,9 +57,6 @@ export struct ModelView {
hilog.error(0x0000, 'ModelView', `LoadModel Failed: ${JSON.stringify(e)}`);
}
})
Button('模块&&so库加载测试').onClick(()=>{
NativeLoadModelTest();
})
}.height('5%')
Row(){
ContentSlot(this.nodeContent);