1 /*
2 * Copyright (C) 2025 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 "sys_event.h"
17
18 #include <cstdint>
19 #include <string>
20 #include <unordered_map>
21
22 #include "hisysevent.h"
23 #include "log.h"
24
25 namespace OHOS {
26 namespace Request {
27
28 namespace {
29 //event params
30 const std::string PARAM_DFX_CODE = "CODE";
31 const std::string PARAM_BUNDLE_NAME = "BUNDLE_NAME";
32 const std::string PARAM_MODULE_NAME = "MODULE_NAME";
33 const std::string PARAM_EXTRA_INFO = "EXTRA_INFO";
34
35 } // namespace
36
SendSysEventLog(const std::string & eventName,const uint32_t dCode,const std::string bundleName,const std::string moduleName,const std::string extraInfo)37 void SysEventLog::SendSysEventLog(const std::string &eventName, const uint32_t dCode, const std::string bundleName,
38 const std::string moduleName, const std::string extraInfo)
39 {
40 auto iter = sysEventMap_.find(eventName);
41 if (iter == sysEventMap_.end()) {
42 return;
43 }
44
45 SysEventInfo info = { .dCode = dCode, .bundleName = bundleName, .moduleName = moduleName, .extraInfo = extraInfo };
46 iter->second(info);
47 }
48
SendSysEventLog(const std::string & eventName,const uint32_t dCode,const std::string extraInfo)49 void SysEventLog::SendSysEventLog(const std::string &eventName, const uint32_t dCode, const std::string extraInfo)
50 {
51 auto iter = sysEventMap_.find(eventName);
52 if (iter == sysEventMap_.end()) {
53 return;
54 }
55
56 SysEventInfo info = { .dCode = dCode, .bundleName = "", .moduleName = "", .extraInfo = extraInfo };
57 iter->second(info);
58 }
59
SendSysEventLog(const std::string & eventName,const uint32_t dCode,const int32_t one,const int32_t two)60 void SysEventLog::SendSysEventLog(
61 const std::string &eventName, const uint32_t dCode, const int32_t one, const int32_t two)
62 {
63 auto iter = sysEventMap_.find(eventName);
64 if (iter == sysEventMap_.end()) {
65 return;
66 }
67
68 SysEventInfo info = { .dCode = dCode,
69 .bundleName = "",
70 .moduleName = "",
71 .extraInfo = "expect" + std::to_string(one) + "=" + std::to_string(two) };
72 iter->second(info);
73 }
74
75 std::unordered_map<std::string, void (*)(const SysEventInfo &info)> SysEventLog::sysEventMap_ = {
__anonb6e88bd40202() 76 { STATISTIC_EVENT, [](const SysEventInfo &info) { SendStatisticEvent(info); } },
__anonb6e88bd40302() 77 { FAULT_EVENT, [](const SysEventInfo &info) { SendFaultEvent(info); } },
78 };
79
80 template<typename... Types>
HisysWrite(const std::string & eventName,HiviewDFX::HiSysEvent::EventType type,Types...keyValues)81 int32_t SysEventLog::HisysWrite(const std::string &eventName, HiviewDFX::HiSysEvent::EventType type, Types... keyValues)
82 {
83 int32_t res = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::REQUEST, eventName,
84 static_cast<HiviewDFX::HiSysEvent::EventType>(type), keyValues...);
85 return res;
86 }
87
SendStatisticEvent(const SysEventInfo & info)88 void SysEventLog::SendStatisticEvent(const SysEventInfo &info)
89 {
90 HisysWrite(STATISTIC_EVENT, HiviewDFX::HiSysEvent::EventType::STATISTIC, PARAM_DFX_CODE, info.dCode,
91 PARAM_BUNDLE_NAME, info.bundleName, PARAM_MODULE_NAME, info.moduleName, PARAM_EXTRA_INFO, info.extraInfo);
92 }
93
SendFaultEvent(const SysEventInfo & info)94 void SysEventLog::SendFaultEvent(const SysEventInfo &info)
95 {
96 HisysWrite(FAULT_EVENT, HiviewDFX::HiSysEvent::EventType::FAULT, PARAM_DFX_CODE, info.dCode, PARAM_BUNDLE_NAME,
97 info.bundleName, PARAM_MODULE_NAME, info.moduleName, PARAM_EXTRA_INFO, info.extraInfo);
98 }
99
100 } // namespace Request
101 } // namespace OHOS
102