1 /*
2 * Copyright (c) 2021-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 "hiappevent_config.h"
16
17 #include <algorithm>
18 #include <mutex>
19 #include <regex>
20 #include <sstream>
21 #include <string>
22
23 #include "app_event_observer_mgr.h"
24 #include "application_context.h"
25 #include "context.h"
26 #include "hiappevent_base.h"
27 #include "hiappevent_read.h"
28 #include "hilog/log.h"
29
30 #undef LOG_DOMAIN
31 #define LOG_DOMAIN 0xD002D07
32
33 #undef LOG_TAG
34 #define LOG_TAG "Config"
35
36 namespace OHOS {
37 namespace HiviewDFX {
38 namespace {
39 const std::string DISABLE = "disable";
40 const std::string MAX_STORAGE = "max_storage";
41 const std::string DEFAULT_STORAGE_DIR = "";
42 const std::string APP_EVENT_DIR = "/hiappevent/";
43 constexpr uint64_t STORAGE_UNIT_KB = 1024;
44 constexpr uint64_t STORAGE_UNIT_MB = STORAGE_UNIT_KB * 1024;
45 constexpr uint64_t STORAGE_UNIT_GB = STORAGE_UNIT_MB * 1024;
46 constexpr uint64_t STORAGE_UNIT_TB = STORAGE_UNIT_GB * 1024;
47 constexpr int DECIMAL_UNIT = 10;
48
49 std::mutex g_mutex;
50
TransUpperToUnderscoreAndLower(const std::string & str)51 std::string TransUpperToUnderscoreAndLower(const std::string& str)
52 {
53 if (str.empty()) {
54 return "";
55 }
56
57 std::stringstream ss;
58 for (size_t i = 0; i < str.size(); i++) {
59 char tmp = str[i];
60 if (tmp < 'A' || tmp > 'Z') {
61 ss << tmp;
62 continue;
63 }
64 if (i != 0) { // prevent string from starting with an underscore
65 ss << "_";
66 }
67 tmp += 32; // 32 means upper case to lower case
68 ss << tmp;
69 }
70
71 return ss.str();
72 }
73 }
74
GetInstance()75 HiAppEventConfig& HiAppEventConfig::GetInstance()
76 {
77 static HiAppEventConfig instance;
78 return instance;
79 }
80
SetConfigurationItem(std::string name,std::string value)81 bool HiAppEventConfig::SetConfigurationItem(std::string name, std::string value)
82 {
83 // trans uppercase to underscore and lowercase
84 name = TransUpperToUnderscoreAndLower(name);
85 HILOG_DEBUG(LOG_CORE, "start to configure.");
86
87 if (name == "") {
88 HILOG_ERROR(LOG_CORE, "item name can not be empty.");
89 return false;
90 }
91 std::transform(name.begin(), name.end(), name.begin(), ::tolower);
92
93 if (value == "") {
94 HILOG_ERROR(LOG_CORE, "item value can not be empty.");
95 return false;
96 }
97 std::transform(value.begin(), value.end(), value.begin(), ::tolower);
98
99 if (name == DISABLE) {
100 return SetDisableItem(value);
101 } else if (name == MAX_STORAGE) {
102 return SetMaxStorageSizeItem(value);
103 } else {
104 HILOG_ERROR(LOG_CORE, "unrecognized configuration item name.");
105 return false;
106 }
107 }
108
SetDisableItem(const std::string & value)109 bool HiAppEventConfig::SetDisableItem(const std::string& value)
110 {
111 if (value == "true") {
112 SetDisable(true);
113 } else if (value == "false") {
114 SetDisable(false);
115 } else {
116 HILOG_ERROR(LOG_CORE, "invalid bool value=%{public}s of the application dotting switch.", value.c_str());
117 return false;
118 }
119 return true;
120 }
121
SetMaxStorageSizeItem(const std::string & value)122 bool HiAppEventConfig::SetMaxStorageSizeItem(const std::string& value)
123 {
124 if (!std::regex_match(value, std::regex("[0-9]+[k|m|g|t]?[b]?"))) {
125 HILOG_ERROR(LOG_CORE, "invalid value=%{public}s of the event file dir storage quota size.", value.c_str());
126 return false;
127 }
128
129 auto len = value.length();
130 std::string::size_type numEndIndex = 0;
131 uint64_t numValue = std::stoull(value, &numEndIndex, DECIMAL_UNIT);
132 if (numEndIndex == len) {
133 SetMaxStorageSize(numValue);
134 return true;
135 }
136
137 uint32_t unitLen = (numEndIndex == (len - 1)) ? 1 : 2; // 1 2, means the length of the storage unit
138 char unitChr = value[len - unitLen];
139 uint64_t maxStoSize = 0;
140 switch (unitChr) {
141 case 'b':
142 maxStoSize = numValue;
143 break;
144 case 'k':
145 maxStoSize = numValue * STORAGE_UNIT_KB;
146 break;
147 case 'm':
148 maxStoSize = numValue * STORAGE_UNIT_MB;
149 break;
150 case 'g':
151 maxStoSize = numValue * STORAGE_UNIT_GB;
152 break;
153 case 't':
154 maxStoSize = numValue * STORAGE_UNIT_TB;
155 break;
156 default:
157 HILOG_ERROR(LOG_CORE, "invalid storage unit value=%{public}c.", unitChr);
158 return false;
159 }
160
161 SetMaxStorageSize(maxStoSize);
162 return true;
163 }
164
SetDisable(bool disable)165 void HiAppEventConfig::SetDisable(bool disable)
166 {
167 std::lock_guard<std::mutex> lockGuard(g_mutex);
168 this->disable = disable;
169 }
170
SetMaxStorageSize(uint64_t size)171 void HiAppEventConfig::SetMaxStorageSize(uint64_t size)
172 {
173 std::lock_guard<std::mutex> lockGuard(g_mutex);
174 this->maxStorageSize = size;
175 }
176
SetStorageDir(const std::string & dir)177 void HiAppEventConfig::SetStorageDir(const std::string& dir)
178 {
179 this->storageDir = dir;
180 LogAssistant::Instance().UpdateHiAppEventLogDir(dir);
181 }
182
GetDisable()183 bool HiAppEventConfig::GetDisable()
184 {
185 std::lock_guard<std::mutex> lockGuard(g_mutex);
186 return this->disable;
187 }
188
GetMaxStorageSize()189 uint64_t HiAppEventConfig::GetMaxStorageSize()
190 {
191 std::lock_guard<std::mutex> lockGuard(g_mutex);
192 return this->maxStorageSize;
193 }
194
GetStorageDir()195 std::string HiAppEventConfig::GetStorageDir()
196 {
197 std::lock_guard<std::mutex> lockGuard(g_mutex);
198 if (!this->storageDir.empty()) {
199 return this->storageDir;
200 }
201 std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> context =
202 OHOS::AbilityRuntime::Context::GetApplicationContext();
203 if (context == nullptr) {
204 HILOG_ERROR(LOG_CORE, "Context is null.");
205 return DEFAULT_STORAGE_DIR;
206 }
207 if (context->GetFilesDir().empty()) {
208 HILOG_ERROR(LOG_CORE, "The files dir obtained from context is empty.");
209 return DEFAULT_STORAGE_DIR;
210 }
211 std::string dir = context->GetFilesDir() + APP_EVENT_DIR;
212 SetStorageDir(dir);
213 return this->storageDir;
214 }
215
GetRunningId()216 std::string HiAppEventConfig::GetRunningId()
217 {
218 std::lock_guard<std::mutex> lockGuard(g_mutex);
219 if (!this->runningId.empty()) {
220 return this->runningId;
221 }
222 std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> context =
223 OHOS::AbilityRuntime::Context::GetApplicationContext();
224 if (context == nullptr) {
225 HILOG_ERROR(LOG_CORE, "Context is null.");
226 return "";
227 }
228 this->runningId = context->GetAppRunningUniqueId();
229 if (this->runningId.empty()) {
230 HILOG_ERROR(LOG_CORE, "The running id from context is empty.");
231 }
232 return this->runningId;
233 }
234 } // namespace HiviewDFX
235 } // namespace OHOS
236