1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <LogSize.h>
18
19 #include <array>
20 #include <optional>
21 #include <string>
22
23 #include <android-base/parseint.h>
24 #include <android-base/properties.h>
25
IsValidBufferSize(size_t value)26 bool IsValidBufferSize(size_t value) {
27 return kLogBufferMinSize <= value && value <= kLogBufferMaxSize;
28 }
29
GetBufferSizeProperty(const std::string & key)30 static std::optional<size_t> GetBufferSizeProperty(const std::string& key) {
31 std::string value = android::base::GetProperty(key, "");
32 if (value.empty()) {
33 return {};
34 }
35
36 uint32_t size;
37 if (!android::base::ParseByteCount(value, &size)) {
38 return {};
39 }
40
41 if (!IsValidBufferSize(size)) {
42 return {};
43 }
44
45 return size;
46 }
47
GetBufferSizeFromProperties(log_id_t log_id)48 size_t GetBufferSizeFromProperties(log_id_t log_id) {
49 std::string buffer_name = android_log_id_to_name(log_id);
50 std::array<std::string, 4> properties = {
51 "persist.logd.size." + buffer_name,
52 "ro.logd.size." + buffer_name,
53 "persist.logd.size",
54 "ro.logd.size",
55 };
56
57 for (const auto& property : properties) {
58 if (auto size = GetBufferSizeProperty(property)) {
59 return *size;
60 }
61 }
62
63 if (android::base::GetBoolProperty("ro.config.low_ram", false)) {
64 return kLogBufferMinSize;
65 }
66
67 return kDefaultLogBufferSize;
68 }
69