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 #ifndef BBOX_DETECTOR_PLUGIN_H
16 #define BBOX_DETECTOR_PLUGIN_H
17 
18 #include <mutex>
19 #include <string>
20 
21 #include "event_source.h"
22 #include "plugin.h"
23 #include "sys_event.h"
24 namespace OHOS {
25 namespace HiviewDFX {
26 class BBoxDetectorPlugin : public Plugin {
27 public:
BBoxDetectorPlugin()28     BBoxDetectorPlugin() {};
~BBoxDetectorPlugin()29     ~BBoxDetectorPlugin() {};
30     void OnLoad() override;
31     void OnUnload() override;
32     bool OnEvent(std::shared_ptr<Event> &event) override;
33 
34 private:
35     void StartBootScan();
36     void WaitForLogs(const std::string& logDir);
37     void HandleBBoxEvent(std::shared_ptr<SysEvent> &sysEvent);
38     uint64_t GetHappenTime(std::string& line);
39     int CheckAndHiSysEventWrite(std::string& name, std::map<std::string, std::string>& historyMap,
40         uint64_t& happenTime);
41     std::map<std::string, std::string> GetValueFromHistory(std::string& line);
42     void AddDetectBootCompletedTask();
43     void RemoveDetectBootCompletedTask();
44     void NotifyBootStable();
45     void NotifyBootCompleted();
46     void InitPanicReporter();
47     static constexpr int SECONDS = 60;
48     static constexpr int READ_LINE_NUM = 5;
49     static constexpr int MILLSECONDS = 1000;
50     static constexpr int ONE_DAY = 86400 * MILLSECONDS;
51     bool hisiHistoryPath_ = false;
52     std::shared_ptr<EventLoop> eventLoop_;
53     uint64_t timeEventId_ = 0;
54     std::mutex lock_;
55     bool timeEventAdded_ = false;
56 };
57 }
58 }
59 #endif /* BBOX_DETECTOR_PLUGIN_H */