• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_SYS_EVENT_SERVICE_H
17 #define HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_SYS_EVENT_SERVICE_H
18 
19 #include <fstream>
20 #include <list>
21 #include <memory>
22 
23 #include "event.h"
24 #include "period_file_operator.h"
25 #include "plugin.h"
26 #include "sys_event_db_mgr.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 struct StorePeriodInfo {
31     // format: YYYYMMDDHH
32     std::string timeStamp;
33 
34     // count of event which has been stored into db file in 1 hour
35     size_t storedCnt = 0;
36 
StorePeriodInfoStorePeriodInfo37     StorePeriodInfo(const std::string& timeStamp, size_t storedCnt) : timeStamp(timeStamp), storedCnt(storedCnt) {}
38 };
39 
40 class SysEventStore : public Plugin {
41 public:
42     SysEventStore();
~SysEventStore()43     ~SysEventStore() {}
44 
45     void OnLoad() override;
46     void OnUnload() override;
47     bool OnEvent(std::shared_ptr<Event>& event) override;
48 
49 private:
50     std::shared_ptr<SysEvent> Convert2SysEvent(std::shared_ptr<Event>& event);
51     bool IsNeedBackup(const std::string& dateStr);
52     void StatisticStorePeriodInfo(const std::shared_ptr<SysEvent> event);
53     void RecordStorePeriodInfo();
54 
55 private:
56     std::unique_ptr<SysEventDbMgr> sysEventDbMgr_ = nullptr;
57     std::atomic<bool> hasLoaded_ { false };
58     std::string lastBackupTime_;
59     std::list<std::shared_ptr<StorePeriodInfo>> periodInfoList_;
60     std::unique_ptr<PeriodInfoFileOperator> periodFileOpt_;
61     std::once_flag exportEngineStartFlag_;
62 }; // SysEventService
63 } // namespace HiviewDFX
64 } // namespace OHOS
65 #endif // HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_SYS_EVENT_SERVICE_H
66