blob: f3a6b0633c687b10f5311e10ae505d576b4af303 [file] [log] [blame]
Ben Chanbc49ac72012-04-10 19:59:45 -07001// 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 Chanbc49ac72012-04-10 19:59:45 -07009#include <base/string_util.h>
Paul Stewart5ad16062013-02-21 18:10:48 -080010#include <base/strings/string_tokenizer.h>
Ben Chanbc49ac72012-04-10 19:59:45 -070011
Paul Stewart5ad16062013-02-21 18:10:48 -080012using base::StringTokenizer;
Ben Chanbc49ac72012-04-10 19:59:45 -070013using std::string;
14using std::vector;
15
16namespace shill {
17
18namespace {
19
20const int kDefaultVerboseLevel = 0;
21
22// Scope names corresponding to the scope defined by ScopeLogger::Scope.
23const char *const kScopeNames[] = {
24 "cellular",
25 "connection",
26 "crypto",
Ben Chanfad4a0b2012-04-18 15:49:59 -070027 "daemon",
Ben Chanbc49ac72012-04-10 19:59:45 -070028 "dbus",
29 "device",
Ben Chanfad4a0b2012-04-18 15:49:59 -070030 "dhcp",
31 "dns",
Ben Chanbc49ac72012-04-10 19:59:45 -070032 "ethernet",
Ben Chanfad4a0b2012-04-18 15:49:59 -070033 "http",
34 "httpproxy",
Ben Chanbc49ac72012-04-10 19:59:45 -070035 "inet",
Paul Stewart6c72c972012-07-27 11:29:20 -070036 "link",
Ben Chanbc49ac72012-04-10 19:59:45 -070037 "manager",
38 "metrics",
39 "modem",
40 "portal",
Ben Chanfad4a0b2012-04-18 15:49:59 -070041 "power",
Ben Chanbc49ac72012-04-10 19:59:45 -070042 "profile",
Ben Chanfad4a0b2012-04-18 15:49:59 -070043 "property",
44 "resolver",
45 "route",
Ben Chanbc49ac72012-04-10 19:59:45 -070046 "rtnl",
47 "service",
48 "storage",
49 "task",
50 "vpn",
51 "wifi",
Darin Petkov096b3472012-05-15 10:26:22 +020052 "wimax",
Ben Chanbc49ac72012-04-10 19:59:45 -070053};
54
55COMPILE_ASSERT(arraysize(kScopeNames) == ScopeLogger::kNumScopes,
56 scope_tags_does_not_have_expected_number_of_strings);
57
Ben Chanfdfdc872012-04-24 08:31:36 -070058// 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.
62base::LazyInstance<ScopeLogger>::Leaky g_scope_logger =
63 LAZY_INSTANCE_INITIALIZER;
Ben Chanbc49ac72012-04-10 19:59:45 -070064
65} // namespace
66
67// static
68ScopeLogger* ScopeLogger::GetInstance() {
69 return g_scope_logger.Pointer();
70}
71
72ScopeLogger::ScopeLogger()
73 : verbose_level_(kDefaultVerboseLevel) {
74}
75
76ScopeLogger::~ScopeLogger() {
77}
78
79bool ScopeLogger::IsLogEnabled(Scope scope, int verbose_level) const {
Paul Stewart5581d072012-12-17 17:30:20 -080080 return IsScopeEnabled(scope) && verbose_level <= verbose_level_;
81}
82
83bool ScopeLogger::IsScopeEnabled(Scope scope) const {
Ben Chanbc49ac72012-04-10 19:59:45 -070084 CHECK_GE(scope, 0);
85 CHECK_LT(scope, kNumScopes);
86
Paul Stewart5581d072012-12-17 17:30:20 -080087 return scope_enabled_[scope];
Ben Chanbc49ac72012-04-10 19:59:45 -070088}
89
90string ScopeLogger::GetAllScopeNames() const {
91 vector<string> names(kScopeNames, kScopeNames + arraysize(kScopeNames));
Ben Chan1c722602012-04-17 17:37:35 -070092 return JoinString(names, '+');
Ben Chanbc49ac72012-04-10 19:59:45 -070093}
94
95string 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
104void 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 Stewart5581d072012-12-17 17:30:20 -0800141void 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 Chanbc49ac72012-04-10 19:59:45 -0700148void ScopeLogger::DisableAllScopes() {
Paul Stewart5581d072012-12-17 17:30:20 -0800149 // 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 Chanbc49ac72012-04-10 19:59:45 -0700153}
154
155void ScopeLogger::SetScopeEnabled(Scope scope, bool enabled) {
156 CHECK_GE(scope, 0);
157 CHECK_LT(scope, kNumScopes);
158
Paul Stewart5581d072012-12-17 17:30:20 -0800159 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 Chanbc49ac72012-04-10 19:59:45 -0700167 scope_enabled_[scope] = enabled;
168}
169
170} // namespace shill