Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/scope_logger.h" |
| 6 | |
| 7 | #include <vector> |
| 8 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 9 | #include <base/string_util.h> |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 10 | #include <base/strings/string_tokenizer.h> |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 11 | |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 12 | using base::StringTokenizer; |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 13 | using std::string; |
| 14 | using std::vector; |
| 15 | |
| 16 | namespace shill { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | const int kDefaultVerboseLevel = 0; |
| 21 | |
| 22 | // Scope names corresponding to the scope defined by ScopeLogger::Scope. |
| 23 | const char *const kScopeNames[] = { |
| 24 | "cellular", |
| 25 | "connection", |
| 26 | "crypto", |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 27 | "daemon", |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 28 | "dbus", |
| 29 | "device", |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 30 | "dhcp", |
| 31 | "dns", |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 32 | "ethernet", |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 33 | "http", |
| 34 | "httpproxy", |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 35 | "inet", |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 36 | "link", |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 37 | "manager", |
| 38 | "metrics", |
| 39 | "modem", |
| 40 | "portal", |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 41 | "power", |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 42 | "profile", |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 43 | "property", |
| 44 | "resolver", |
| 45 | "route", |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 46 | "rtnl", |
| 47 | "service", |
| 48 | "storage", |
| 49 | "task", |
| 50 | "vpn", |
| 51 | "wifi", |
Darin Petkov | 096b347 | 2012-05-15 10:26:22 +0200 | [diff] [blame] | 52 | "wimax", |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | COMPILE_ASSERT(arraysize(kScopeNames) == ScopeLogger::kNumScopes, |
| 56 | scope_tags_does_not_have_expected_number_of_strings); |
| 57 | |
Ben Chan | fdfdc87 | 2012-04-24 08:31:36 -0700 | [diff] [blame] | 58 | // ScopeLogger needs to be a 'leaky' singleton as it needs to survive to |
| 59 | // handle logging till the very end of the shill process. Making ScopeLogger |
| 60 | // leaky is fine as it does not need to clean up or release any resource at |
| 61 | // destruction. |
| 62 | base::LazyInstance<ScopeLogger>::Leaky g_scope_logger = |
| 63 | LAZY_INSTANCE_INITIALIZER; |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 64 | |
| 65 | } // namespace |
| 66 | |
| 67 | // static |
| 68 | ScopeLogger* ScopeLogger::GetInstance() { |
| 69 | return g_scope_logger.Pointer(); |
| 70 | } |
| 71 | |
| 72 | ScopeLogger::ScopeLogger() |
| 73 | : verbose_level_(kDefaultVerboseLevel) { |
| 74 | } |
| 75 | |
| 76 | ScopeLogger::~ScopeLogger() { |
| 77 | } |
| 78 | |
| 79 | bool ScopeLogger::IsLogEnabled(Scope scope, int verbose_level) const { |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 80 | return IsScopeEnabled(scope) && verbose_level <= verbose_level_; |
| 81 | } |
| 82 | |
| 83 | bool ScopeLogger::IsScopeEnabled(Scope scope) const { |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 84 | CHECK_GE(scope, 0); |
| 85 | CHECK_LT(scope, kNumScopes); |
| 86 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 87 | return scope_enabled_[scope]; |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | string ScopeLogger::GetAllScopeNames() const { |
| 91 | vector<string> names(kScopeNames, kScopeNames + arraysize(kScopeNames)); |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 92 | return JoinString(names, '+'); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | string ScopeLogger::GetEnabledScopeNames() const { |
| 96 | vector<string> names; |
| 97 | for (size_t i = 0; i < arraysize(kScopeNames); ++i) { |
| 98 | if (scope_enabled_[i]) |
| 99 | names.push_back(kScopeNames[i]); |
| 100 | } |
| 101 | return JoinString(names, '+'); |
| 102 | } |
| 103 | |
| 104 | void ScopeLogger::EnableScopesByName(const string &expression) { |
| 105 | if (expression.empty()) { |
| 106 | DisableAllScopes(); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // As described in the header file, if the first scope name in the |
| 111 | // sequence specified by |expression| is not prefixed by a plus or |
| 112 | // minus sign, it indicates that all scopes are first disabled before |
| 113 | // enabled by |expression|. |
| 114 | if (expression[0] != '+' && expression[0] != '-') |
| 115 | DisableAllScopes(); |
| 116 | |
| 117 | bool enable_scope = true; |
| 118 | StringTokenizer tokenizer(expression, "+-"); |
| 119 | tokenizer.set_options(StringTokenizer::RETURN_DELIMS); |
| 120 | while (tokenizer.GetNext()) { |
| 121 | if (tokenizer.token_is_delim()) { |
| 122 | enable_scope = (tokenizer.token() == "+"); |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | if (tokenizer.token().empty()) |
| 127 | continue; |
| 128 | |
| 129 | size_t i; |
| 130 | for (i = 0; i < arraysize(kScopeNames); ++i) { |
| 131 | if (tokenizer.token() == kScopeNames[i]) { |
| 132 | SetScopeEnabled(static_cast<Scope>(i), enable_scope); |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | LOG_IF(WARNING, i == arraysize(kScopeNames)) |
| 137 | << "Unknown scope '" << tokenizer.token() << "'"; |
| 138 | } |
| 139 | } |
| 140 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 141 | void ScopeLogger::RegisterScopeEnableChangedCallback( |
| 142 | Scope scope, ScopeEnableChangedCallback callback) { |
| 143 | CHECK_GE(scope, 0); |
| 144 | CHECK_LT(scope, kNumScopes); |
| 145 | log_scope_callbacks_[scope].push_back(callback); |
| 146 | } |
| 147 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 148 | void ScopeLogger::DisableAllScopes() { |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 149 | // Iterate over all scopes so the notification side-effect occurs. |
| 150 | for (size_t i = 0; i < arraysize(kScopeNames); ++i) { |
| 151 | SetScopeEnabled(static_cast<Scope>(i), false); |
| 152 | } |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void ScopeLogger::SetScopeEnabled(Scope scope, bool enabled) { |
| 156 | CHECK_GE(scope, 0); |
| 157 | CHECK_LT(scope, kNumScopes); |
| 158 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 159 | if (scope_enabled_[scope] != enabled) { |
| 160 | ScopeEnableChangedCallbacks &callbacks = log_scope_callbacks_[scope]; |
| 161 | ScopeEnableChangedCallbacks::iterator it; |
| 162 | for (it = callbacks.begin(); it != callbacks.end(); ++it) { |
| 163 | (*it).Run(enabled); |
| 164 | } |
| 165 | } |
| 166 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 167 | scope_enabled_[scope] = enabled; |
| 168 | } |
| 169 | |
| 170 | } // namespace shill |