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 #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::unordered_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 Json::CharReaderBuilder jsonRBuilder;
50 Json::CharReaderBuilder::strictMode(&jsonRBuilder.settings_);
51 JSONCPP_STRING errs;
52 if (!parseFromStream(jsonRBuilder, fin, &root, &errs)) {
53 return;
54 }
55
56 std::vector<std::string> members = root.getMemberNames();
57 for (auto iter = members.begin(); iter != members.end(); ++iter) {
58 if (EVENT_TYPE_MAP.find(*iter) == EVENT_TYPE_MAP.end()) {
59 continue;
60 }
61 if (auto node = root[*iter]; node.type() == Json::objectValue) {
62 StoreConfig config = {
63 .storeDay = ParseUint32(node, KEY_STORE_DAY),
64 .pageSize = ParseUint32(node, KEY_PAGE_SIZE),
65 .maxFileSize = ParseUint32(node, KEY_MAX_FILE_SIZE),
66 .maxFileNum = ParseUint32(node, KEY_MAX_FILE_NUM),
67 .maxSize = ParseUint32(node, KEY_MAX_SIZE),
68 };
69 configMap_.emplace(EVENT_TYPE_MAP.at(*iter), config);
70 }
71 }
72 }
73
Contain(int eventType)74 bool EventStoreConfig::Contain(int eventType)
75 {
76 return configMap_.find(eventType) != configMap_.end();
77 }
78
GetStoreDay(int eventType)79 uint32_t EventStoreConfig::GetStoreDay(int eventType)
80 {
81 return Contain(eventType) ? configMap_[eventType].storeDay : 0;
82 }
83
GetMaxSize(int eventType)84 uint32_t EventStoreConfig::GetMaxSize(int eventType)
85 {
86 return Contain(eventType) ? configMap_[eventType].maxSize : 0;
87 }
88
GetMaxFileNum(int eventType)89 uint32_t EventStoreConfig::GetMaxFileNum(int eventType)
90 {
91 return Contain(eventType) ? configMap_[eventType].maxFileNum : 0;
92 }
93
GetPageSize(int eventType)94 uint32_t EventStoreConfig::GetPageSize(int eventType)
95 {
96 return Contain(eventType) ? configMap_[eventType].pageSize : 0;
97 }
98
GetMaxFileSize(int eventType)99 uint32_t EventStoreConfig::GetMaxFileSize(int eventType)
100 {
101 return Contain(eventType) ? configMap_[eventType].maxFileSize : 0;
102 }
103 } // EventStore
104 } // HiviewDFX
105 } // OHOS
106