• 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 #ifndef HIVIEWDFX_NAPI_HISYSEVENT_ADAPTER_H
17 #define HIVIEWDFX_NAPI_HISYSEVENT_ADAPTER_H
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "hisysevent.h"
24 #include "napi/native_api.h"
25 #include "napi/native_node_api.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 using HiSysEventInfo = struct HiSysEventInfo {
30     std::string domain;
31     std::string name;
32     HiSysEvent::EventType eventType;
33     std::unordered_map<std::string, bool> boolParams;
34     std::unordered_map<std::string, std::vector<bool>> boolArrayParams;
35     std::unordered_map<std::string, double> doubleParams;
36     std::unordered_map<std::string, std::vector<double>> doubleArrayParams;
37     std::unordered_map<std::string, std::string> stringParams;
38     std::unordered_map<std::string, std::vector<std::string>> stringArrayParams;
39 };
40 
41 using HiSysEventAsyncContext = struct HiSysEventAsyncContext {
42     napi_env env;
43     napi_async_work asyncWork;
44     napi_deferred deferred;
45     napi_ref callback;
46     HiSysEventInfo eventInfo;
47     int eventWroteResult;
48 };
49 
50 class NapiHiSysEventAdapter {
51 public:
52     static void Write(const napi_env env, HiSysEventAsyncContext* eventAsyncContext);
53 
54 private:
55     static void InnerWrite(HiSysEvent::EventBase& eventBase, const HiSysEventInfo& eventInfo);
56     static int Write(const HiSysEventInfo& eventInfo);
57 
58     template<typename T>
AppendData(HiSysEvent::EventBase & eventBase,std::unordered_map<std::string,T> tMap)59     static void AppendData(HiSysEvent::EventBase& eventBase, std::unordered_map<std::string, T> tMap)
60     {
61         for (auto iter = tMap.cbegin(); iter != tMap.cend(); iter++) {
62             auto key = iter->first;
63             auto value = iter->second;
64             HiSysEvent::AppendData<T>(eventBase, key, value);
65         }
66     }
67 
68     template<typename T>
AppendArrayData(HiSysEvent::EventBase & eventBase,std::unordered_map<std::string,std::vector<T>> tMap)69     static void AppendArrayData(HiSysEvent::EventBase& eventBase,
70         std::unordered_map<std::string, std::vector<T>> tMap)
71     {
72         for (auto iter = tMap.cbegin(); iter != tMap.cend(); iter++) {
73             auto key = iter->first;
74             auto value = iter->second;
75             HiSysEvent::AppendArrayData<T>(eventBase, key, value);
76         }
77     }
78 };
79 } // namespace HiviewDFX
80 } // namespace OHOS
81 
82 #endif // HIVIEWDFX_NAPI_HISYSEVENT_ADAPTER_H