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 #include "exception_manager.h"
17
18 #include <ctime>
19 #include <dirent.h>
20 #include <fstream>
21 #include <iostream>
22 #include <string>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <vector>
26
27 #include "csv_utils.h"
28 #include "hisysevent_manager.h"
29 #include "sysevent_listener.h"
30 #include "wukong_util.h"
31
32 namespace OHOS {
33 namespace WuKong {
34 using HiviewDFX::HiSysEventManager;
35 using HiviewDFX::ListenerRule;
36 namespace {
37 std::string DEFAULT_DIR = "/data/local/tmp/wukong";
38
InitReportFolder()39 bool InitReportFolder()
40 {
41 DIR *rootDir = nullptr;
42 if ((rootDir = opendir(DEFAULT_DIR.c_str())) == nullptr) {
43 int ret = mkdir(DEFAULT_DIR.c_str(), S_IROTH | S_IRWXU | S_IRWXG);
44 if (ret != 0) {
45 ERROR_LOG_STR("failed to create dir: %s", DEFAULT_DIR.c_str());
46 return false;
47 }
48 } else {
49 closedir(rootDir);
50 }
51 return true;
52 }
53
InitCsvFile(std::ofstream & csvFile,std::string & filePath)54 bool InitCsvFile(std::ofstream &csvFile, std::string &filePath)
55 {
56 TRACK_LOG_STD();
57 if (!InitReportFolder()) {
58 return false;
59 }
60
61 filePath = DEFAULT_DIR + "/wukong_" + WuKongUtil::GetInstance()->GetStartRunTime() + ".csv";
62 csvFile.open(filePath, std::ios_base::out | std::ios_base::trunc);
63 if (!csvFile) {
64 ERROR_LOG_STR("Failed to create csv file at: %s", filePath.c_str());
65 return false;
66 }
67 CsvUtils::WriteHeader(csvFile);
68 INFO_LOG_STR("CSV: (%s)", filePath.c_str());
69 TRACK_LOG_END();
70 return true;
71 }
72 } // namespace
StartCatching()73 bool ExceptionManager::StartCatching()
74 {
75 if (!InitCsvFile(csvFile, currentCsvFilePath)) {
76 return false;
77 }
78 std::vector<ListenerRule> sysRules;
79 std::string domain = "";
80 std::string eventName = "";
81 sysRules.emplace_back(domain, eventName);
82 toolListener = std::make_shared<SysEventListener>(csvFile);
83 if (toolListener == nullptr) {
84 ERROR_LOG("toolListener is nullptr and please check the csvFile is ok");
85 return false;
86 }
87 return HiSysEventManager::AddListener(toolListener, sysRules) == 0;
88 }
89
StopCatching()90 void ExceptionManager::StopCatching()
91 {
92 int32_t result = HiSysEventManager::RemoveListener(toolListener);
93 DEBUG_LOG_STR("remove listener result: %d", result);
94 if (csvFile.is_open()) {
95 csvFile.flush();
96 csvFile.close();
97 }
98 TEST_RUN_LOG("catching stopped");
99 }
100 } // namespace WuKong
101 } // namespace OHOS
102