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 "wukong_exception_manager.h" 17 18 #include <dirent.h> 19 #include <string> 20 #include <sys/stat.h> 21 #include <sys/types.h> 22 #include <ctime> 23 #include <fstream> 24 #include <iostream> 25 #include <vector> 26 27 #include "hisysevent_manager.h" 28 #include "wukong_sysevent_listener.h" 29 #include "wukong_csv_utils.h" 30 31 namespace OHOS { 32 namespace AppExecFwk { 33 using HiviewDFX::ListenerRule; 34 using HiviewDFX::HiSysEventManager; 35 namespace { 36 std::string DEFAULT_DIR = "/data/local/wukong"; 37 InitReportFolder()38 bool InitReportFolder() 39 { 40 DIR *rootDir = nullptr; 41 if ((rootDir = opendir(DEFAULT_DIR.c_str())) == nullptr) { 42 int ret = mkdir(DEFAULT_DIR.c_str(), S_IROTH | S_IRWXU | S_IRWXG); 43 if (ret != 0) { 44 std::cerr << "failed to create dir: " << DEFAULT_DIR << std::endl; 45 return false; 46 } 47 } 48 return true; 49 } 50 initCsvFile(std::ofstream & csvFile,std::string & filePath)51 bool initCsvFile(std::ofstream &csvFile, std::string &filePath) 52 { 53 if (!InitReportFolder()) { 54 return false; 55 } 56 time_t currentTime = time(0); 57 char buf[32] = {0}; 58 int copiedSize = strftime(buf, sizeof(buf), "%Y%m%d_%H%M%S", localtime(¤tTime)); 59 if (copiedSize <= 0) { 60 return false; 61 } 62 filePath = DEFAULT_DIR + "/wukong_" + buf + ".csv"; 63 csvFile.open(filePath, std::ios_base::out | std::ios_base::trunc); 64 if (!csvFile) { 65 std::cerr << "Failed to create csv file at:" << filePath << std::endl; 66 return false; 67 } 68 WuKongCsvUtils::WriteHeader(csvFile); 69 std::cout << "The result will be written in csv file at location: " << filePath << std::endl; 70 return true; 71 } 72 } StartCatching()73 bool WuKongExceptionManager::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<WuKongSysEventListener>(csvFile); 83 return HiSysEventManager::AddEventListener(toolListener, sysRules); 84 } 85 StopCatching()86 void WuKongExceptionManager::StopCatching() 87 { 88 HiSysEventManager::RemoveListener(toolListener); 89 if (csvFile.is_open()) { 90 csvFile.flush(); 91 csvFile.close(); 92 } 93 std::cout << "catching stopped" << std::endl; 94 std::cout << "The result file location:" << GetCurrentCsvFilePath() << std::endl; 95 } 96 } // namespace AppExecFwk 97 } // namespace OHOS 98