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 
Add(napi_env env, napi_callback_info info)42 static napi_value Add(napi_env env, napi_callback_info info)
43 {
44     OH_LOG_SetCallback(callback);
45     printf("hahahahha");
46     OH_LOG_INFO(LOG_APP, "123456");
47     size_t argc = 2;
48     napi_value args[2] = {nullptr};
49 
50     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
51 
52     napi_valuetype valuetype0;
53     napi_typeof(env, args[0], &valuetype0);
54 
55     napi_valuetype valuetype1;
56     napi_typeof(env, args[1], &valuetype1);
57 
58     double value0;
59     napi_get_value_double(env, args[0], &value0);
60 
61     double value1;
62     napi_get_value_double(env, args[1], &value1);
63 
64     g_test = value0 + value1; // 5
65 
66     OH_LOG_INFO(LOG_APP, "666666");
67     napi_create_double(env, g_test, &g_sum);
68 
69     return g_sum;
70 }
71 
GetMsg(napi_env env, napi_callback_info info)72 static napi_value GetMsg(napi_env env, napi_callback_info info)
73 {
74     napi_value message;
75     napi_create_string_utf8(env, g_msg.c_str(), strlen(g_msg.c_str()), &message);
76     return message;
77 }
78 
OhIsLoggableTest(napi_env env, napi_callback_info info)79 static napi_value OhIsLoggableTest(napi_env env, napi_callback_info info)
80 {
81     napi_value res = nullptr;
82     bool isLoggable = OH_LOG_IsLoggable(0x3200, "testTag", LOG_DEBUG);
83     napi_get_boolean(env, isLoggable, &res);
84     return res;
85 }
86 
OhPrintTest(napi_env env, napi_callback_info info)87 static napi_value OhPrintTest(napi_env env, napi_callback_info info)
88 {
89     napi_value res = nullptr;
90     LogType type = LOG_APP;
91     LogLevel level = LOG_ERROR;
92     int retLen = OH_LOG_Print(type, level, 0x3200, "testTag", "string for hilog test");
93     bool ret = (retLen > 0) ? true : false;
94     napi_get_boolean(env, ret, &res);
95     return res;
96 }
97 
98 EXTERN_C_START
Init(napi_env env, napi_value exports)99 static napi_value Init(napi_env env, napi_value exports)
100 {
101     napi_property_descriptor desc[] = {
102         { "ohIsLoggableTest", nullptr, OhIsLoggableTest,
103             nullptr, nullptr, nullptr, napi_default, nullptr },
104         { "ohPrintTest", nullptr, OhPrintTest,
105             nullptr, nullptr, nullptr, napi_default, nullptr },
106         { "add", nullptr, Add,
107             nullptr, nullptr, nullptr, napi_default, nullptr },
108         { "getMsg", nullptr, GetMsg,
109             nullptr, nullptr, nullptr, napi_default, nullptr },
110     };
111     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
112     return exports;
113 }
114 EXTERN_C_END
115 
116 static napi_module demoModule = {
117     .nm_version =1,
118     .nm_flags = 0,
119     .nm_filename = nullptr,
120     .nm_register_func = Init,
121     .nm_modname = "libhilogndk",
122     .nm_priv = ((void*)0),
123     .reserved = { 0 },
124 };
125 
RegisterModule(void)126 extern "C" __attribute__((constructor)) void RegisterModule(void)
127 {
128     napi_module_register(&demoModule);
129 }
130