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 "event_export_util.h"
17
18 #include "event.h"
19 #include "export_db_storage.h"
20 #include "file_util.h"
21 #include "sys_event_sequence_mgr.h"
22 #include "hiview_global.h"
23 #include "hiview_logger.h"
24 #include "parameter.h"
25 #include "parameter_ex.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace {
30 DEFINE_LOG_TAG("HiView-EventExportUtil");
31
GenerateDeviceId()32 std::string GenerateDeviceId()
33 {
34 constexpr int32_t deviceIdLength = 65;
35 char id[deviceIdLength] = {0};
36 if (GetDevUdid(id, deviceIdLength) == 0) {
37 return std::string(id);
38 }
39 return "";
40 }
41
IsExportDirEmpty(const std::string & exportDir)42 bool IsExportDirEmpty(const std::string& exportDir)
43 {
44 std::vector<std::string> eventZipFiles;
45 FileUtil::GetDirFiles(exportDir, eventZipFiles);
46 return !any_of(eventZipFiles.begin(), eventZipFiles.end(), [] (const std::string& file) {
47 return !FileUtil::IsDirectory(file);
48 });
49 }
50
PostExportEvent(const std::string & moduleName,int16_t taskType)51 void PostExportEvent(const std::string& moduleName, int16_t taskType)
52 {
53 auto event = std::make_shared<Event>("post_export_type_event");
54 event->messageType_ = Event::MessageType::EVENT_EXPORT_TYPE;
55 event->SetValue("reportModule", moduleName);
56 if (taskType == ALL_EVENT_TASK_TYPE) {
57 event->SetValue("reportInterval", "0");
58 } else {
59 event->SetValue("reportInterval", std::to_string(taskType));
60 }
61
62 auto& context = HiviewGlobal::GetInstance();
63 if (context == nullptr) {
64 HIVIEW_LOGW("hiview context is invalid.");
65 return;
66 }
67 context->PostUnorderedEvent(event);
68 }
69
IsNeedPostEvent(std::shared_ptr<ExportConfig> config)70 bool IsNeedPostEvent(std::shared_ptr<ExportConfig> config)
71 {
72 if (config == nullptr) {
73 HIVIEW_LOGW("export cfg file is invalid.");
74 return false;
75 }
76 if (!config->needPostEvent) {
77 HIVIEW_LOGW("no need to post event");
78 return false;
79 }
80 if (IsExportDirEmpty(config->exportDir)) {
81 HIVIEW_LOGW("no event zip file found");
82 return false;
83 }
84 return true;
85 }
86 }
87
GetDeviceId()88 std::string EventExportUtil::GetDeviceId()
89 {
90 static std::string deviceId = Parameter::GetUserType() == Parameter::USER_TYPE_OVERSEA_COMMERCIAL ?
91 Parameter::GetString("persist.hiviewdfx.priv.packid", "") : GenerateDeviceId();
92 return deviceId;
93 }
94
CheckAndPostExportEvent(std::shared_ptr<ExportConfig> config)95 bool EventExportUtil::CheckAndPostExportEvent(std::shared_ptr<ExportConfig> config)
96 {
97 if (!IsNeedPostEvent(config)) {
98 return false;
99 }
100 PostExportEvent(config->moduleName, config->taskType);
101 return true;
102 }
103 } // namespace HiviewDFX
104 } // namespace OHOS