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 "napi/native_api.h"
17 #include "hilog/log.h"
18
19 #include <cstdio>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <string>
24
25 #undef LOG_DOMAIN
26 #undef LOG_TAG
27 #define LOG_DOMAIN 0xD003200
28 #define LOG_TAG "testTag"
29
30 using namespace std;
31
32 napi_value g_sum = 0;
33 double g_test = 0;
34 std::string g_msg = "";
35
callback(const LogType type,const LogLevel level,const unsigned int domain,const char * tag,const char * msg)36 void callback(const LogType type, const LogLevel level, const unsigned int domain, const char *tag, const char *msg)
37 {
38 g_msg = msg;
39 g_test = 6; //test number : 6
40 }
41
OhPrintVTest(LogType type,LogLevel level,unsigned int domain,const char * tag,const char * fmt,...)42 static int OhPrintVTest(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...)
43 {
44 int ret;
45 va_list ap;
46 va_start(ap, fmt);
47 ret = OH_LOG_VPrint(type, level, domain, tag, fmt, ap);
48 va_end(ap);
49 return ret;
50 }
51
Add(napi_env env,napi_callback_info info)52 static napi_value Add(napi_env env, napi_callback_info info)
53 {
54 OH_LOG_SetCallback(callback);
55 printf("hahahahha");
56 OH_LOG_INFO(LOG_APP, "123456");
57 size_t argc = 2;
58 napi_value args[2] = {nullptr};
59
60 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
61
62 napi_valuetype valuetype0;
63 napi_typeof(env, args[0], &valuetype0);
64
65 napi_valuetype valuetype1;
66 napi_typeof(env, args[1], &valuetype1);
67
68 double value0;
69 napi_get_value_double(env, args[0], &value0);
70
71 double value1;
72 napi_get_value_double(env, args[1], &value1);
73
74 g_test = value0 + value1; // 5
75
76 OH_LOG_INFO(LOG_APP, "666666");
77 napi_create_double(env, g_test, &g_sum);
78
79 return g_sum;
80 }
81
GetMsg(napi_env env,napi_callback_info info)82 static napi_value GetMsg(napi_env env, napi_callback_info info)
83 {
84 napi_value message;
85 napi_create_string_utf8(env, g_msg.c_str(), strlen(g_msg.c_str()), &message);
86 return message;
87 }
88
OhIsLoggableTest(napi_env env,napi_callback_info info)89 static napi_value OhIsLoggableTest(napi_env env, napi_callback_info info)
90 {
91 napi_value res = nullptr;
92 bool isLoggable = OH_LOG_IsLoggable(0x3200, "testTag", LOG_DEBUG);
93 napi_get_boolean(env, isLoggable, &res);
94 return res;
95 }
96
OhPrintTest(napi_env env,napi_callback_info info)97 static napi_value OhPrintTest(napi_env env, napi_callback_info info)
98 {
99 napi_value res = nullptr;
100 LogType type = LOG_APP;
101 LogLevel level = LOG_ERROR;
102 OH_LOG_SetMinLogLevel(LOG_INFO);
103 int retLen = OH_LOG_Print(type, level, 0x3200, "testTag", "string for hilog test");
104 bool ret = (retLen > 0) ? true : false;
105 napi_get_boolean(env, ret, &res);
106 return res;
107 }
108
OhPrintMsgTest(napi_env env,napi_callback_info info)109 static napi_value OhPrintMsgTest(napi_env env, napi_callback_info info)
110 {
111 napi_value res = nullptr;
112 LogType type = LOG_APP;
113 LogLevel level = LOG_ERROR;
114 int retLen = OH_LOG_PrintMsg(type, level, 0x3200, "testTag", "string for hilog PrintMsg test");
115 bool ret = (retLen > 0) ? true : false;
116 napi_get_boolean(env, ret, &res);
117 return res;
118 }
119
OhPrintMsgByLenTest(napi_env env,napi_callback_info info)120 static napi_value OhPrintMsgByLenTest(napi_env env, napi_callback_info info)
121 {
122 napi_value res = nullptr;
123 LogType type = LOG_APP;
124 LogLevel level = LOG_ERROR;
125 int retLen = OH_LOG_PrintMsgByLen(type, level, 0x3200, "testTag", 7, "string for hilog PrintMsgByLen test", 30);
126 bool ret = (retLen > 0) ? true : false;
127 napi_get_boolean(env, ret, &res);
128 return res;
129 }
130
OhVPrintTest(napi_env env,napi_callback_info info)131 static napi_value OhVPrintTest(napi_env env, napi_callback_info info)
132 {
133 napi_value res = nullptr;
134 LogType type = LOG_APP;
135 LogLevel level = LOG_ERROR;
136 int retLen = OhPrintVTest(type, level, 0x3200, "testTag", "%{public}s", "OH_LOG_VPrint11111");
137 bool ret = (retLen > 0) ? true : false;
138 napi_get_boolean(env, ret, &res);
139 return res;
140 }
141
142 EXTERN_C_START
Init(napi_env env,napi_value exports)143 static napi_value Init(napi_env env, napi_value exports)
144 {
145 napi_property_descriptor desc[] = {
146 { "ohIsLoggableTest", nullptr, OhIsLoggableTest,
147 nullptr, nullptr, nullptr, napi_default, nullptr },
148 { "ohPrintTest", nullptr, OhPrintTest,
149 nullptr, nullptr, nullptr, napi_default, nullptr },
150 { "add", nullptr, Add,
151 nullptr, nullptr, nullptr, napi_default, nullptr },
152 { "getMsg", nullptr, GetMsg,
153 nullptr, nullptr, nullptr, napi_default, nullptr },
154 { "ohPrintMsgTest", nullptr, OhPrintMsgTest,
155 nullptr, nullptr, nullptr, napi_default, nullptr },
156 { "ohPrintMsgByLenTest", nullptr, OhPrintMsgByLenTest,
157 nullptr, nullptr, nullptr, napi_default, nullptr },
158 { "ohVPrintTest", nullptr, OhVPrintTest,
159 nullptr, nullptr, nullptr, napi_default, nullptr },
160 };
161 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
162 return exports;
163 }
164 EXTERN_C_END
165
166 static napi_module demoModule = {
167 .nm_version =1,
168 .nm_flags = 0,
169 .nm_filename = nullptr,
170 .nm_register_func = Init,
171 .nm_modname = "libhilogndk",
172 .nm_priv = ((void*)0),
173 .reserved = { 0 },
174 };
175
RegisterModule(void)176 extern "C" __attribute__((constructor)) void RegisterModule(void)
177 {
178 napi_module_register(&demoModule);
179 }
180