1 /*
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "js_console_log.h"
17
18 #include <string>
19
20 #include "hilog_tag_wrapper.h"
21 #include "js_runtime_utils.h"
22
23 namespace OHOS {
24 namespace AbilityRuntime {
25 namespace {
26 constexpr uint32_t JS_CONSOLE_LOG_MAX_LOG_LEN = 1024;
27 constexpr uint32_t JS_CONSOLE_LOG_DOMAIN = 0xFEFE;
28 constexpr char JS_CONSOLE_LOG_TAG[] = "JsApp";
29
MakeLogContent(napi_env env,napi_callback_info info)30 std::string MakeLogContent(napi_env env, napi_callback_info info)
31 {
32 std::string content;
33
34 size_t argc = ARGC_MAX_COUNT;
35 napi_value argv[ARGC_MAX_COUNT] = {nullptr};
36 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
37 for (size_t i = 0; i < argc; i++) {
38 napi_value value = argv[i];
39 if (!CheckTypeForNapiValue(env, value, napi_string)) {
40 napi_value resultStr = nullptr;
41 napi_coerce_to_string(env, value, &resultStr);
42 value = resultStr;
43 }
44
45 if (value == nullptr) {
46 TAG_LOGE(AAFwkTag::ABILITY_SIM, "convert to string failed");
47 continue;
48 }
49
50 size_t bufferLen = 0;
51 napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &bufferLen);
52 if (status != napi_ok || bufferLen == 0 || bufferLen >= JS_CONSOLE_LOG_MAX_LOG_LEN) {
53 TAG_LOGD(AAFwkTag::ABILITY_SIM, "Log length exceeds maximum");
54 return content;
55 }
56
57 auto buff = new (std::nothrow) char[bufferLen + 1];
58 if (buff == nullptr) {
59 TAG_LOGE(AAFwkTag::ABILITY_SIM, "failed, size=%zu", bufferLen + 1);
60 break;
61 }
62
63 size_t strLen = 0;
64 napi_get_value_string_utf8(env, value, buff, bufferLen + 1, &strLen);
65 if (!content.empty()) {
66 content.append(" ");
67 }
68 content.append(buff);
69 delete [] buff;
70 }
71
72 return content;
73 }
74
75 template<LogLevel LEVEL>
ConsoleLog(napi_env env,napi_callback_info info)76 napi_value ConsoleLog(napi_env env, napi_callback_info info)
77 {
78 if (env == nullptr || info == nullptr) {
79 TAG_LOGE(AAFwkTag::ABILITY_SIM, "null env/callback info");
80 return nullptr;
81 }
82
83 std::string content = MakeLogContent(env, info);
84 HiLogPrint(LOG_APP, LEVEL, JS_CONSOLE_LOG_DOMAIN, JS_CONSOLE_LOG_TAG, "%{public}s", content.c_str());
85
86 return CreateJsUndefined(env);
87 }
88 }
89
InitConsoleLogModule(napi_env env,napi_value globalObject)90 void InitConsoleLogModule(napi_env env, napi_value globalObject)
91 {
92 napi_value consoleObj = nullptr;
93 napi_create_object(env, &consoleObj);
94 if (consoleObj == nullptr) {
95 TAG_LOGE(AAFwkTag::ABILITY_SIM, "create console object failed");
96 return;
97 }
98 const char *moduleName = "console";
99 BindNativeFunction(env, consoleObj, "log", moduleName, ConsoleLog<LOG_INFO>);
100 BindNativeFunction(env, consoleObj, "debug", moduleName, ConsoleLog<LOG_DEBUG>);
101 BindNativeFunction(env, consoleObj, "info", moduleName, ConsoleLog<LOG_INFO>);
102 BindNativeFunction(env, consoleObj, "warn", moduleName, ConsoleLog<LOG_WARN>);
103 BindNativeFunction(env, consoleObj, "error", moduleName, ConsoleLog<LOG_ERROR>);
104 BindNativeFunction(env, consoleObj, "fatal", moduleName, ConsoleLog<LOG_FATAL>);
105
106 napi_set_named_property(env, globalObject, "console", consoleObj);
107 }
108 } // namespace AbilityRuntime
109 } // namespace OHOS