Skip to content

Commit c3b55da

Browse files
committed
feat(ohos): support logAdapter in EngineInitParams
1 parent b48b984 commit c3b55da

File tree

6 files changed

+99
-6
lines changed

6 files changed

+99
-6
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making
3+
* Hippy available.
4+
*
5+
* Copyright (C) 2022 THL A29 Limited, a Tencent company.
6+
* All rights reserved.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
import { HippyLogAdapter, HippyLogSeverity } from 'hippy'
21+
import hilog from '@ohos.hilog';
22+
23+
export class ExampleLogAdapter implements HippyLogAdapter {
24+
onReceiveLogMessage(level: number, tag: string, msg: string): void {
25+
switch (level) {
26+
case HippyLogSeverity.LOG_SEVERITY_DEBUG:
27+
case HippyLogSeverity.LOG_SEVERITY_INFO:
28+
break;
29+
case HippyLogSeverity.LOG_SEVERITY_WARNING:
30+
hilog.warn(0x0001, tag, msg);
31+
case HippyLogSeverity.LOG_SEVERITY_ERROR:
32+
hilog.error(0x0001, tag, msg);
33+
case HippyLogSeverity.LOG_SEVERITY_FATAL:
34+
hilog.fatal(0x0001, tag, msg);
35+
default:
36+
break;
37+
}
38+
}
39+
40+
}

framework/examples/ohos-demo/src/main/ets/pages/PageConfiguration.ets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { LogUtils } from 'hippy/src/main/ets/support/utils/LogUtils'
3636
import { ExampleFontAdapter } from '../hippy_extend/ExampleFontAdapter'
3737
import { ExampleImageLocalLoader,
3838
ExampleImageRemoteLoader } from '../hippy_extend/ExampleImageLoaderAdapter'
39+
import { ExampleLogAdapter } from '../hippy_extend/ExampleLogAdapter'
3940

4041
export enum DriverMode {
4142
JS_REACT,
@@ -183,6 +184,9 @@ struct PageConfiguration {
183184

184185
params.fontAdapter = new ExampleFontAdapter()
185186

187+
// 日志重定向输出适配器举例
188+
//params.logAdapter = new ExampleLogAdapter()
189+
186190
// 图片加载适配器举例
187191
//params.imageLocalLoader = new ExampleImageLocalLoader();
188192
//params.imageRemoteLoader = new ExampleImageRemoteLoader();

framework/ohos/src/main/cpp/impl/CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ set(HIPPY_DRIVER_JS_CPP_DIR "${PROJECT_ROOT_DIR}/driver/js")
3434
set(HIPPY_DOM_CPP_DIR "${PROJECT_ROOT_DIR}/dom")
3535
set(HIPPY_VFS_CPP_DIR "${PROJECT_ROOT_DIR}/modules/vfs")
3636

37-
add_library(hippy_impl STATIC
38-
"${HIPPY_IMPL_CPP_DIR}/entry.cc"
39-
)
37+
add_library(hippy_impl STATIC)
38+
39+
set(PUBLIC_SOURCE_SET
40+
"${HIPPY_IMPL_CPP_DIR}/entry.cc")
41+
42+
target_sources(${PROJECT_NAME} PUBLIC ${PUBLIC_SOURCE_SET})
4043

4144
target_include_directories(hippy_impl PUBLIC
4245
"${HIPPY_IMPL_CPP_DIR}"

framework/ohos/src/main/cpp/impl/entry.cc

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,45 @@
2020
*
2121
*/
2222

23-
#include <napi/native_api.h>
24-
#include <stdio.h>
23+
#include <js_native_api.h>
24+
#include <js_native_api_types.h>
25+
#include "oh_napi/oh_napi_object.h"
26+
#include "oh_napi/oh_napi_register.h"
27+
#include "oh_napi/oh_napi_task_runner.h"
28+
#include "oh_napi/ark_ts.h"
29+
#include "footstone/check.h"
30+
31+
using OhNapiTaskRunner = hippy::OhNapiTaskRunner;
32+
33+
static bool s_is_initialized_ = false;
34+
35+
static napi_value SetNativeLogHandler(napi_env env, napi_callback_info info) {
36+
ArkTS arkTs(env);
37+
auto args = arkTs.GetCallbackArgs(info, 1);
38+
auto ts_log_adapter = args[0];
39+
auto ts_log_adapter_ref = arkTs.CreateReference(ts_log_adapter);
40+
41+
if (!s_is_initialized_) {
42+
footstone::log::LogMessage::InitializeDelegate([env, ts_log_adapter_ref](
43+
const std::ostringstream& stream,
44+
footstone::log::LogSeverity severity) {
45+
auto msg = stream.str();
46+
OhNapiTaskRunner *taskRunner = OhNapiTaskRunner::Instance(env);
47+
taskRunner->RunAsyncTask([env, ts_log_adapter_ref, severity, msg]() {
48+
ArkTS arkTs(env);
49+
std::vector<napi_value> args = {
50+
arkTs.CreateInt(severity), // level
51+
arkTs.CreateString("HippyLog"), // tag
52+
arkTs.CreateString(msg), // msg
53+
};
54+
auto delegateObject = arkTs.GetObject(ts_log_adapter_ref);
55+
delegateObject.Call("onReceiveLogMessage", args);
56+
});
57+
});
58+
s_is_initialized_ = true;
59+
}
60+
61+
return arkTs.GetUndefined();
62+
}
63+
64+
REGISTER_OH_NAPI("Entry", "Entry_SetNativeLogHandler", SetNativeLogHandler)

framework/ohos/src/main/ets/hippy_framework/HippyEngine.ets

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ export function createHippyEngine(params: EngineInitParams): HippyEngine {
104104
params.libHippy?.Setting_SetDesktop();
105105
}
106106

107+
if (params.logAdapter) {
108+
params.libHippy?.Entry_SetNativeLogHandler(params.logAdapter);
109+
}
110+
107111
return new HippyEngineManagerImpl(params)
108112
}
109113

@@ -250,7 +254,7 @@ export class EngineInitParams {
250254
this.deviceAdapter = new DefaultDeviceAdapter()
251255
}
252256
if (this.logAdapter === null) {
253-
this.logAdapter = new DefaultLogAdapter()
257+
// 这里不用创建DefaultLogAdapter,默认c层输出log性能更好
254258
}
255259
if (this.engineMonitor === null) {
256260
this.engineMonitor = new DefaultEngineMonitorAdapter()

framework/ohos/src/main/ets/hippy_library/HippyLibrary.ets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
import {
3737
ImageRemoteLoaderInnerAdapter
3838
} from "../hippy_framework/adapter/image/inner/ImageRemoteLoaderInnerAdapter";
39+
import { HippyLogAdapter } from "../hippy_framework/adapter/log/HippyLogAdapter";
3940

4041
export interface HippyRenderViewInfo {
4142
tag: number
@@ -159,4 +160,5 @@ export interface HippyLibrary {
159160
Worker_UnregisterWModules(moduleNames: HippyArray): void;
160161
Worker_CallJsFunction(instanceId: number, action: string, buffer: ArrayBuffer): void;
161162

163+
Entry_SetNativeLogHandler(logAdapter: HippyLogAdapter): void;
162164
}

0 commit comments

Comments
 (0)