1 /******************************************************************************
2 *
3 * Copyright 2019 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include "init_flags.h"
20
21 #include <string>
22
23 #include "common/strings.h"
24 #include "os/log.h"
25
26 namespace bluetooth {
27 namespace common {
28
29 bool InitFlags::logging_debug_enabled_for_all = false;
30 std::unordered_map<std::string, bool> InitFlags::logging_debug_explicit_tag_settings = {};
31
ParseBoolFlag(const std::vector<std::string> & flag_pair,const std::string & flag,bool * variable)32 bool ParseBoolFlag(const std::vector<std::string>& flag_pair, const std::string& flag, bool* variable) {
33 if (flag != flag_pair[0]) {
34 return false;
35 }
36 auto value = BoolFromString(flag_pair[1]);
37 if (!value) {
38 return false;
39 }
40 *variable = *value;
41 return true;
42 }
43
Load(const char ** flags)44 void InitFlags::Load(const char** flags) {
45 const char** flags_copy = flags;
46 SetAll(false);
47 while (flags != nullptr && *flags != nullptr) {
48 std::string flag_element = *flags;
49 auto flag_pair = StringSplit(flag_element, "=", 2);
50 if (flag_pair.size() != 2) {
51 flags++;
52 continue;
53 }
54
55 ParseBoolFlag(flag_pair, "INIT_logging_debug_enabled_for_all", &logging_debug_enabled_for_all);
56 if ("INIT_logging_debug_enabled_for_tags" == flag_pair[0]) {
57 auto tags = StringSplit(flag_pair[1], ",");
58 for (const auto& tag : tags) {
59 auto setting = logging_debug_explicit_tag_settings.find(tag);
60 if (setting == logging_debug_explicit_tag_settings.end()) {
61 logging_debug_explicit_tag_settings.insert_or_assign(tag, true);
62 }
63 }
64 }
65 if ("INIT_logging_debug_disabled_for_tags" == flag_pair[0]) {
66 auto tags = StringSplit(flag_pair[1], ",");
67 for (const auto& tag : tags) {
68 logging_debug_explicit_tag_settings.insert_or_assign(tag, false);
69 }
70 }
71 flags++;
72 }
73
74 std::vector<std::string> logging_debug_enabled_tags;
75 std::vector<std::string> logging_debug_disabled_tags;
76 for (const auto& tag_setting : logging_debug_explicit_tag_settings) {
77 if (tag_setting.second) {
78 logging_debug_enabled_tags.emplace_back(tag_setting.first);
79 } else {
80 logging_debug_disabled_tags.emplace_back(tag_setting.first);
81 }
82 }
83
84 flags = flags_copy;
85 rust::Vec<rust::String> rusted_flags = rust::Vec<rust::String>();
86 while (flags != nullptr && *flags != nullptr) {
87 rusted_flags.push_back(rust::String(*flags));
88 flags++;
89 }
90 init_flags::load(std::move(rusted_flags));
91 }
92
SetAll(bool value)93 void InitFlags::SetAll(bool value) {
94 logging_debug_enabled_for_all = value;
95 logging_debug_explicit_tag_settings.clear();
96 }
97
SetAllForTesting()98 void InitFlags::SetAllForTesting() {
99 init_flags::set_all_for_testing();
100 SetAll(true);
101 }
102
103 } // namespace common
104 } // namespace bluetooth
105