• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_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(NativeCallbackInfo & info)30 std::string MakeLogContent(NativeCallbackInfo& info)
31 {
32     std::string content;
33 
34     for (size_t i = 0; i < info.argc; i++) {
35         NativeValue* value = info.argv[i];
36         if (value->TypeOf() != NATIVE_STRING) {
37             value = value->ToString();
38         }
39 
40         NativeString* str = ConvertNativeValueTo<NativeString>(value);
41         if (str == nullptr) {
42             HILOG_ERROR("Failed to convert to string object");
43             continue;
44         }
45 
46         size_t bufferLen = str->GetLength();
47         if (bufferLen >= JS_CONSOLE_LOG_MAX_LOG_LEN) {
48             HILOG_DEBUG("Log length exceeds maximum");
49             return content;
50         }
51 
52         auto buff = new (std::nothrow) char[bufferLen + 1];
53         if (buff == nullptr) {
54             HILOG_ERROR("Failed to allocate buffer, size = %zu", bufferLen + 1);
55             break;
56         }
57 
58         size_t strLen = 0;
59         str->GetCString(buff, bufferLen + 1, &strLen);
60         if (!content.empty()) {
61             content.append(" ");
62         }
63         content.append(buff);
64         delete [] buff;
65     }
66 
67     return content;
68 }
69 
70 template<LogLevel LEVEL>
ConsoleLog(NativeEngine * engine,NativeCallbackInfo * info)71 NativeValue* ConsoleLog(NativeEngine* engine, NativeCallbackInfo* info)
72 {
73     if (engine == nullptr || info == nullptr) {
74         HILOG_ERROR("engine or callback info is nullptr");
75         return nullptr;
76     }
77 
78     std::string content = MakeLogContent(*info);
79     HiLogPrint(LOG_APP, LEVEL, JS_CONSOLE_LOG_DOMAIN, JS_CONSOLE_LOG_TAG, "%{public}s", content.c_str());
80 
81     return engine->CreateUndefined();
82 }
83 }
84 
InitConsoleLogModule(NativeEngine & engine,NativeObject & globalObject)85 void InitConsoleLogModule(NativeEngine& engine, NativeObject& globalObject)
86 {
87     NativeValue* consoleValue = engine.CreateObject();
88     NativeObject* consoleObj = ConvertNativeValueTo<NativeObject>(consoleValue);
89     if (consoleObj == nullptr) {
90         HILOG_ERROR("Failed to create console object");
91         return;
92     }
93     const char *moduleName = "console";
94     BindNativeFunction(engine, *consoleObj, "log", moduleName, ConsoleLog<LOG_INFO>);
95     BindNativeFunction(engine, *consoleObj, "debug", moduleName, ConsoleLog<LOG_DEBUG>);
96     BindNativeFunction(engine, *consoleObj, "info", moduleName, ConsoleLog<LOG_INFO>);
97     BindNativeFunction(engine, *consoleObj, "warn", moduleName, ConsoleLog<LOG_WARN>);
98     BindNativeFunction(engine, *consoleObj, "error", moduleName, ConsoleLog<LOG_ERROR>);
99     BindNativeFunction(engine, *consoleObj, "fatal", moduleName, ConsoleLog<LOG_FATAL>);
100 
101     globalObject.SetProperty("console", consoleValue);
102 }
103 } // namespace AbilityRuntime
104 } // namespace OHOS