• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 #include "event_store_config.h"
16 
17 #include <fstream>
18 
19 #include "json/json.h"
20 
21 namespace OHOS {
22 namespace HiviewDFX {
23 namespace EventStore {
24 namespace {
25 const char CONFIG_FILE_PATH[] = "/system/etc/hiview/event_store_config.json";
26 const char KEY_STORE_DAY[] = "StoreDay";
27 const char KEY_PAGE_SIZE[] = "PageSize";
28 const char KEY_MAX_SIZE[] = "MaxSize";
29 const char KEY_MAX_FILE_NUM[] = "MaxFileNum";
30 const char KEY_MAX_FILE_SIZE[] = "MaxFileSize";
31 const std::map<std::string, int> EVENT_TYPE_MAP = {
32     {"FAULT", 1}, {"STATISTIC", 2}, {"SECURITY", 3}, {"BEHAVIOR", 4}
33 };
34 
ParseUint32(const Json::Value & root,const std::string & key)35 uint32_t ParseUint32(const Json::Value& root, const std::string& key)
36 {
37     return (root.isMember(key) && root[key].isUInt()) ? root[key].asUInt() : 0;
38 }
39 }
EventStoreConfig()40 EventStoreConfig::EventStoreConfig()
41 {
42     Init();
43 }
44 
Init()45 void EventStoreConfig::Init()
46 {
47     Json::Value root;
48     std::ifstream fin(CONFIG_FILE_PATH, std::ifstream::binary);
49     if (!fin.is_open()) {
50         return;
51     }
52     Json::CharReaderBuilder jsonRBuilder;
53     Json::CharReaderBuilder::strictMode(&jsonRBuilder.settings_);
54     JSONCPP_STRING errs;
55     if (!parseFromStream(jsonRBuilder, fin, &root, &errs)) {
56         return;
57     }
58 
59     std::vector<std::string> members = root.getMemberNames();
60     for (auto iter = members.begin(); iter != members.end(); ++iter) {
61         if (EVENT_TYPE_MAP.find(*iter) == EVENT_TYPE_MAP.end()) {
62             continue;
63         }
64         if (auto node = root[*iter]; node.type() == Json::objectValue) {
65             StoreConfig config = {
66                 .storeDay = ParseUint32(node, KEY_STORE_DAY),
67                 .pageSize = ParseUint32(node, KEY_PAGE_SIZE),
68                 .maxFileSize = ParseUint32(node, KEY_MAX_FILE_SIZE),
69                 .maxFileNum = ParseUint32(node, KEY_MAX_FILE_NUM),
70                 .maxSize = ParseUint32(node, KEY_MAX_SIZE),
71             };
72             configMap_.emplace(EVENT_TYPE_MAP.at(*iter), config);
73         }
74     }
75 }
76 
Contain(int eventType)77 bool EventStoreConfig::Contain(int eventType)
78 {
79     return configMap_.find(eventType) != configMap_.end();
80 }
81 
GetStoreDay(int eventType)82 uint32_t EventStoreConfig::GetStoreDay(int eventType)
83 {
84     return Contain(eventType) ? configMap_[eventType].storeDay : 0;
85 }
86 
GetMaxSize(int eventType)87 uint32_t EventStoreConfig::GetMaxSize(int eventType)
88 {
89     return Contain(eventType) ? configMap_[eventType].maxSize : 0;
90 }
91 
GetMaxFileNum(int eventType)92 uint32_t EventStoreConfig::GetMaxFileNum(int eventType)
93 {
94     return Contain(eventType) ? configMap_[eventType].maxFileNum : 0;
95 }
96 
GetPageSize(int eventType)97 uint32_t EventStoreConfig::GetPageSize(int eventType)
98 {
99     return Contain(eventType) ? configMap_[eventType].pageSize : 0;
100 }
101 
GetMaxFileSize(int eventType)102 uint32_t EventStoreConfig::GetMaxFileSize(int eventType)
103 {
104     return Contain(eventType) ? configMap_[eventType].maxFileSize : 0;
105 }
106 } // EventStore
107 } // HiviewDFX
108 } // OHOS
109