• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "sys_event_db_mgr.h"
17 
18 #include <cinttypes>
19 #include <ctime>
20 #include <string>
21 
22 #include "file_util.h"
23 #include "logger.h"
24 #include "sys_event_dao.h"
25 #include "sys_event_db_backup.h"
26 #include "sys_event_db_cleaner.h"
27 #include "sys_event.h"
28 #include "time_util.h"
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 using EventStore::StoreType;
33 using EventStore::SysEventDao;
34 DEFINE_LOG_TAG("HiView-SysEventDbMgr");
35 namespace {
36 constexpr StoreType STORE_TYPES[] = {
37     StoreType::BEHAVIOR, StoreType::FAULT, StoreType::STATISTIC, StoreType::SECURITY
38 };
39 }
40 
SaveToStore(std::shared_ptr<SysEvent> event) const41 void SysEventDbMgr::SaveToStore(std::shared_ptr<SysEvent> event) const
42 {
43     SysEventDao::Insert(event);
44     HIVIEW_LOGD("save sys event %{public}" PRId64 ", %{public}s", event->GetSeq(), event->eventName_.c_str());
45 }
46 
StartCheckStoreTask(std::shared_ptr<EventLoop> looper)47 void SysEventDbMgr::StartCheckStoreTask(std::shared_ptr<EventLoop> looper)
48 {
49     if (looper == nullptr) {
50         HIVIEW_LOGE("can not init check store task as looper null");
51         return;
52     }
53     HIVIEW_LOGI("init check store task");
54     auto statusTask = std::bind(&SysEventDbMgr::CheckStore, this);
55     int delay = TimeUtil::SECONDS_PER_HOUR; // 1 hour
56     looper->AddTimerEvent(nullptr, nullptr, statusTask, delay, true);
57 }
58 
CheckStore()59 void SysEventDbMgr::CheckStore()
60 {
61     HIVIEW_LOGI("start to check store");
62     SysEventDbCleaner::TryToClean();
63 }
64 
TryToRecover()65 void SysEventDbMgr::TryToRecover()
66 {
67     for (auto type : STORE_TYPES) {
68         SysEventDbBackup dbBackup(type);
69         if (dbBackup.IsBroken() && !dbBackup.Recover()) {
70             HIVIEW_LOGE("failed to recover db, type=%{public}d", type);
71         }
72     }
73 }
74 } // namespace HiviewDFX
75 } // namespace OHOS