• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "napi_hitrace_init.h"
17 
18 #include <map>
19 
20 #include "hilog/log.h"
21 #include "hitrace/trace.h"
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 namespace {
26 constexpr char HITRACE_FLAG_ENUM_NAME[] = "HiTraceFlag";
27 constexpr char HITRACE_TRACE_POINT_TYPE_ENUM_NAME[] = "HiTraceTracepointType";
28 constexpr char HITRACE_COMMUNICATION_MODE_ENUM_NAME[] = "HiTraceCommunicationMode";
29 
ClassConstructor(napi_env env,napi_callback_info info)30 napi_value ClassConstructor(napi_env env, napi_callback_info info)
31 {
32     size_t argc = 0;
33     napi_value argv = nullptr;
34     napi_value thisArg = nullptr;
35     void* data = nullptr;
36     napi_get_cb_info(env, info, &argc, &argv, &thisArg, &data);
37 
38     napi_value global = 0;
39     napi_get_global(env, &global);
40 
41     return thisArg;
42 }
43 
InitHiTraceFlagEnum(napi_env env,std::map<const char *,napi_value> & traceFlagMap)44 void InitHiTraceFlagEnum(napi_env env, std::map<const char*, napi_value>& traceFlagMap)
45 {
46     napi_value defaultFlag = nullptr;
47     napi_create_int32(env, HITRACE_FLAG_DEFAULT, &defaultFlag);
48     napi_value includeAsyncFlag = nullptr;
49     napi_create_int32(env, HITRACE_FLAG_INCLUDE_ASYNC, &includeAsyncFlag);
50     napi_value doNotCreateSpanFlag = nullptr;
51     napi_create_int32(env, HITRACE_FLAG_DONOT_CREATE_SPAN, &doNotCreateSpanFlag);
52     napi_value tpInfoFlag = nullptr;
53     napi_create_int32(env, HITRACE_FLAG_TP_INFO, &tpInfoFlag);
54     napi_value noBeInfoFlag = nullptr;
55     napi_create_int32(env, HITRACE_FLAG_NO_BE_INFO, &noBeInfoFlag);
56     napi_value doNotEnableLogFlag = nullptr;
57     napi_create_int32(env, HITRACE_FLAG_DONOT_ENABLE_LOG, &doNotEnableLogFlag);
58     napi_value failureTriggerFlag = nullptr;
59     napi_create_int32(env, HITRACE_FLAG_FAULT_TRIGGER, &failureTriggerFlag);
60     napi_value d2dTpInfoFlag = nullptr;
61     napi_create_int32(env, HITRACE_FLAG_D2D_TP_INFO, &d2dTpInfoFlag);
62 
63     traceFlagMap["DEFAULT"] = defaultFlag;
64     traceFlagMap["INCLUDE_ASYNC"] = includeAsyncFlag;
65     traceFlagMap["DONOT_CREATE_SPAN"] = doNotCreateSpanFlag;
66     traceFlagMap["TP_INFO"] = tpInfoFlag;
67     traceFlagMap["NO_BE_INFO"] = noBeInfoFlag;
68     traceFlagMap["DISABLE_LOG"] = doNotEnableLogFlag;
69     traceFlagMap["FAILURE_TRIGGER"] = failureTriggerFlag;
70     traceFlagMap["D2D_TP_INFO"] = d2dTpInfoFlag;
71 }
72 
InitHiTraceTracepointTypeEnum(napi_env env,std::map<const char *,napi_value> & tracePointMap)73 void InitHiTraceTracepointTypeEnum(napi_env env,
74     std::map<const char*, napi_value>& tracePointMap)
75 {
76     napi_value csTracePoint = nullptr;
77     napi_create_int32(env, HITRACE_TP_CS, &csTracePoint);
78     napi_value crTracePoint = nullptr;
79     napi_create_int32(env, HITRACE_TP_CR, &crTracePoint);
80     napi_value ssTracePoint = nullptr;
81     napi_create_int32(env, HITRACE_TP_SS, &ssTracePoint);
82     napi_value srTracePoint = nullptr;
83     napi_create_int32(env, HITRACE_TP_SR, &srTracePoint);
84     napi_value generalTracePoint = nullptr;
85     napi_create_int32(env, HITRACE_TP_GENERAL, &generalTracePoint);
86 
87     tracePointMap["CS"] = csTracePoint;
88     tracePointMap["CR"] = crTracePoint;
89     tracePointMap["SS"] = ssTracePoint;
90     tracePointMap["SR"] = srTracePoint;
91     tracePointMap["GENERAL"] = generalTracePoint;
92 }
93 
InitHiTraceCommunicationModeEnum(napi_env env,std::map<const char *,napi_value> & commuicationModeMap)94 void InitHiTraceCommunicationModeEnum(napi_env env,
95     std::map<const char*, napi_value>& commuicationModeMap)
96 {
97     napi_value defaultMode = nullptr;
98     napi_create_int32(env, HITRACE_CM_DEFAULT, &defaultMode);
99     napi_value threadMode = nullptr;
100     napi_create_int32(env, HITRACE_CM_THREAD, &threadMode);
101     napi_value processMode = nullptr;
102     napi_create_int32(env, HITRACE_CM_PROCESS, &processMode);
103     napi_value deviceMode = nullptr;
104     napi_create_int32(env, HITRACE_CM_DEVICE, &deviceMode);
105 
106     commuicationModeMap["DEFAULT"] = defaultMode;
107     commuicationModeMap["THREAD"] = threadMode;
108     commuicationModeMap["PROCESS"] = processMode;
109     commuicationModeMap["DEVICE"] = deviceMode;
110 }
111 
InitConstClassByName(napi_env env,napi_value exports,std::string name)112 void InitConstClassByName(napi_env env, napi_value exports, std::string name)
113 {
114     std::map<const char*, napi_value> propertyMap;
115     if (name == HITRACE_FLAG_ENUM_NAME) {
116         InitHiTraceFlagEnum(env, propertyMap);
117     } else if (name == HITRACE_TRACE_POINT_TYPE_ENUM_NAME) {
118         InitHiTraceTracepointTypeEnum(env, propertyMap);
119     } else if (name == HITRACE_COMMUNICATION_MODE_ENUM_NAME) {
120         InitHiTraceCommunicationModeEnum(env, propertyMap);
121     } else {
122         return;
123     }
124     int i = 0;
125     napi_property_descriptor descriptors[propertyMap.size()];
126     for (auto& it : propertyMap) {
127         descriptors[i++] = DECLARE_NAPI_STATIC_PROPERTY(it.first, it.second);
128     }
129     napi_value result = nullptr;
130     napi_define_class(env, name.c_str(), NAPI_AUTO_LENGTH, ClassConstructor,
131         nullptr, sizeof(descriptors) / sizeof(*descriptors), descriptors, &result);
132     napi_set_named_property(env, exports, name.c_str(), result);
133 }
134 }
135 
InitNapiClass(napi_env env,napi_value exports)136 napi_value InitNapiClass(napi_env env, napi_value exports)
137 {
138     InitConstClassByName(env, exports, HITRACE_FLAG_ENUM_NAME);
139     InitConstClassByName(env, exports, HITRACE_TRACE_POINT_TYPE_ENUM_NAME);
140     InitConstClassByName(env, exports, HITRACE_COMMUNICATION_MODE_ENUM_NAME);
141     return exports;
142 }
143 } // namespace HiviewDFX
144 } // namespace OHOS
145