1 /*
2 * Copyright (C) 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 "berberis/base/config_globals.h"
18
19 #include <bitset>
20 #include <string>
21
22 #include "berberis/base/logging.h"
23 #include "berberis/base/strings.h"
24
25 namespace berberis {
26
27 namespace {
28
ToString(ConfigFlag flag)29 std::string ToString(ConfigFlag flag) {
30 switch (flag) {
31 case kVerboseTranslation:
32 return "verbose-translation";
33 case kNumConfigFlags:
34 break;
35 }
36 return "<unknown-config-flag>";
37 }
38
MakeConfigFlagsSet()39 std::bitset<kNumConfigFlags> MakeConfigFlagsSet() {
40 ConfigStr var("BERBERIS_FLAGS", "ro.berberis.flags");
41 std::bitset<kNumConfigFlags> flags_set;
42 if (!var.get()) {
43 return flags_set;
44 }
45 auto token_vector = Split(var.get(), ",");
46 for (const auto& token : token_vector) {
47 bool found = false;
48 for (int flag = 0; flag < kNumConfigFlags; flag++) {
49 if (token == ToString(ConfigFlag(flag))) {
50 flags_set.set(flag);
51 found = true;
52 break;
53 }
54 }
55 if (!found) {
56 ALOGW("Unrecognized config flag '%s' - ignoring", token.c_str());
57 }
58 }
59 return flags_set;
60 }
61
62 } // namespace
63
GetTracingConfig()64 const char* GetTracingConfig() {
65 static ConfigStr var("BERBERIS_TRACING", "berberis.tracing");
66 return var.get();
67 }
68
GetTranslationModeConfig()69 const char* GetTranslationModeConfig() {
70 static ConfigStr var("BERBERIS_MODE", "berberis.mode");
71 return var.get();
72 }
73
GetProfilingConfig()74 const char* GetProfilingConfig() {
75 static ConfigStr var("BERBERIS_PROFILING", "berberis.profiling");
76 return var.get();
77 }
78
IsConfigFlagSet(ConfigFlag flag)79 bool IsConfigFlagSet(ConfigFlag flag) {
80 static auto flags_set = MakeConfigFlagsSet();
81 return flags_set.test(flag);
82 }
83
84 } // namespace berberis
85