1 /*
2 * Copyright (c) 2021 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 "hilog_napi.h"
17 #include "hilog/log.h"
18 #include "hilog/log_c.h"
19 #include "n_class.h"
20 #include "n_func_arg.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 namespace OHOS {
27 namespace HiviewDFX {
28 using namespace std;
29
LogLevelTypeConstructor(napi_env env,napi_callback_info info)30 napi_value LogLevelTypeConstructor(napi_env env, napi_callback_info info)
31 {
32 napi_value thisArg = nullptr;
33 void* data = nullptr;
34
35 napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
36
37 napi_value global = nullptr;
38 napi_get_global(env, &global);
39
40 return thisArg;
41 }
42
LogLevelTypeEnumInit(napi_env env,napi_value exports)43 static void LogLevelTypeEnumInit(napi_env env, napi_value exports)
44 {
45 napi_value DEBUG = nullptr;
46 napi_value INFO = nullptr;
47 napi_value WARN = nullptr;
48 napi_value ERROR = nullptr;
49 napi_value FATAL = nullptr;
50 napi_create_int32(env, LogLevel::LOG_DEBUG, &DEBUG);
51 napi_create_int32(env, LogLevel::LOG_INFO, &INFO);
52 napi_create_int32(env, LogLevel::LOG_WARN, &WARN);
53 napi_create_int32(env, LogLevel::LOG_ERROR, &ERROR);
54 napi_create_int32(env, LogLevel::LOG_FATAL, &FATAL);
55
56 napi_property_descriptor descriptors[] = {
57 NVal::DeclareNapiStaticProperty("DEBUG", DEBUG),
58 NVal::DeclareNapiStaticProperty("INFO", INFO),
59 NVal::DeclareNapiStaticProperty("WARN", WARN),
60 NVal::DeclareNapiStaticProperty("ERROR", ERROR),
61 NVal::DeclareNapiStaticProperty("FATAL", FATAL),
62 };
63
64 napi_value result = nullptr;
65 napi_define_class(env, "LogLevel", NAPI_AUTO_LENGTH, LogLevelTypeConstructor, nullptr,
66 sizeof(descriptors) / sizeof(*descriptors), descriptors, &result);
67
68 napi_set_named_property(env, exports, "LogLevel", result);
69 }
70
Export(napi_env env,napi_value exports)71 bool HilogNapi::Export(napi_env env, napi_value exports)
72 {
73 LogLevelTypeEnumInit(env, exports);
74 return exports_.AddProp({
75 NVal::DeclareNapiFunction("debug", HilogNapiBase::Debug),
76 NVal::DeclareNapiFunction("info", HilogNapiBase::Info),
77 NVal::DeclareNapiFunction("error", HilogNapiBase::Error),
78 NVal::DeclareNapiFunction("warn", HilogNapiBase::Warn),
79 NVal::DeclareNapiFunction("fatal", HilogNapiBase::Fatal),
80 NVal::DeclareNapiFunction("sLogD", HilogNapiBase::SysLogDebug),
81 NVal::DeclareNapiFunction("sLogI", HilogNapiBase::SysLogInfo),
82 NVal::DeclareNapiFunction("sLogW", HilogNapiBase::SysLogWarn),
83 NVal::DeclareNapiFunction("sLogE", HilogNapiBase::SysLogError),
84 NVal::DeclareNapiFunction("sLogF", HilogNapiBase::SysLogFatal),
85 NVal::DeclareNapiFunction("isLoggable", HilogNapiBase::IsLoggable),
86 });
87 }
88
GetClassName()89 string HilogNapi::GetClassName()
90 {
91 return HilogNapi::className;
92 }
93 } // namespace HiviewDFX
94 } // namespace OHOS
95
96 #ifdef __cplusplus
97 }
98 #endif
99