1 /* 2 * Copyright (c) 2023 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 "faultevent_listener.h" 17 18 #include <cstring> 19 #include <iostream> 20 21 namespace OHOS { 22 namespace HiviewDFX { 23 SetKeyWords(const std::vector<std::string> & keyWords)24void FaultEventListener::SetKeyWords(const std::vector<std::string>& keyWords) 25 { 26 this->keyWords = keyWords; 27 { 28 std::lock_guard<std::mutex> lock(setFlagMutex); 29 allFindFlag = false; 30 } 31 } 32 OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent)33void FaultEventListener::OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent) 34 { 35 if (sysEvent == nullptr) { 36 return; 37 } 38 auto str = sysEvent->AsJson(); 39 for (const auto& keyWord : keyWords) { 40 if (str.find(keyWord) == std::string::npos) { 41 return; 42 } 43 } 44 45 // find all keywords, set allFindFlag to true 46 { 47 std::lock_guard<std::mutex> lock(setFlagMutex); 48 allFindFlag = true; 49 keyWordCheckCondition.notify_all(); 50 } 51 } 52 CheckKeyWords()53bool FaultEventListener::CheckKeyWords() 54 { 55 std::unique_lock<std::mutex> lock(setFlagMutex); 56 if (allFindFlag) { 57 return true; 58 } 59 60 auto flagCheckFunc = [&]() { 61 return allFindFlag; 62 }; 63 64 // 8: wait allFindFlag set true for 8 seconds 65 if (keyWordCheckCondition.wait_for(lock, std::chrono::seconds(8), flagCheckFunc)) { 66 return true; 67 } else { 68 std::cout << "match keywords timeout" << std::endl; 69 return false; 70 } 71 } 72 } // namespace HiviewDFX 73 } // namespace OHOS 74