blob: 8da0b3f152941db996a14e535f6e06c4bb4bccbe [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
9#include <base/string_tokenizer.h>
10#include <base/string_util.h>
11
12using std::string;
13using std::vector;
14
15namespace shill {
16
17namespace {
18
19const int kDefaultVerboseLevel = 0;
20
21// Scope names corresponding to the scope defined by ScopeLogger::Scope.
22const char *const kScopeNames[] = {
23 "cellular",
24 "connection",
25 "crypto",
Ben Chanfad4a0b2012-04-18 15:49:59 -070026 "daemon",
Ben Chanbc49ac72012-04-10 19:59:45 -070027 "dbus",
28 "device",
Ben Chanfad4a0b2012-04-18 15:49:59 -070029 "dhcp",
30 "dns",
Ben Chanbc49ac72012-04-10 19:59:45 -070031 "ethernet",
Ben Chanfad4a0b2012-04-18 15:49:59 -070032 "http",
33 "httpproxy",
Ben Chanbc49ac72012-04-10 19:59:45 -070034 "inet",
35 "manager",
36 "metrics",
37 "modem",
38 "portal",
Ben Chanfad4a0b2012-04-18 15:49:59 -070039 "power",
Ben Chanbc49ac72012-04-10 19:59:45 -070040 "profile",
Ben Chanfad4a0b2012-04-18 15:49:59 -070041 "property",
42 "resolver",
43 "route",
Ben Chanbc49ac72012-04-10 19:59:45 -070044 "rtnl",
45 "service",
46 "storage",
47 "task",
48 "vpn",
49 "wifi",
50};
51
52COMPILE_ASSERT(arraysize(kScopeNames) == ScopeLogger::kNumScopes,
53 scope_tags_does_not_have_expected_number_of_strings);
54
Ben Chanfdfdc872012-04-24 08:31:36 -070055// ScopeLogger needs to be a 'leaky' singleton as it needs to survive to
56// handle logging till the very end of the shill process. Making ScopeLogger
57// leaky is fine as it does not need to clean up or release any resource at
58// destruction.
59base::LazyInstance<ScopeLogger>::Leaky g_scope_logger =
60 LAZY_INSTANCE_INITIALIZER;
Ben Chanbc49ac72012-04-10 19:59:45 -070061
62} // namespace
63
64// static
65ScopeLogger* ScopeLogger::GetInstance() {
66 return g_scope_logger.Pointer();
67}
68
69ScopeLogger::ScopeLogger()
70 : verbose_level_(kDefaultVerboseLevel) {
71}
72
73ScopeLogger::~ScopeLogger() {
74}
75
76bool ScopeLogger::IsLogEnabled(Scope scope, int verbose_level) const {
77 CHECK_GE(scope, 0);
78 CHECK_LT(scope, kNumScopes);
79
80 return scope_enabled_[scope] && verbose_level <= verbose_level_;
81}
82
83string ScopeLogger::GetAllScopeNames() const {
84 vector<string> names(kScopeNames, kScopeNames + arraysize(kScopeNames));
Ben Chan1c722602012-04-17 17:37:35 -070085 return JoinString(names, '+');
Ben Chanbc49ac72012-04-10 19:59:45 -070086}
87
88string ScopeLogger::GetEnabledScopeNames() const {
89 vector<string> names;
90 for (size_t i = 0; i < arraysize(kScopeNames); ++i) {
91 if (scope_enabled_[i])
92 names.push_back(kScopeNames[i]);
93 }
94 return JoinString(names, '+');
95}
96
97void ScopeLogger::EnableScopesByName(const string &expression) {
98 if (expression.empty()) {
99 DisableAllScopes();
100 return;
101 }
102
103 // As described in the header file, if the first scope name in the
104 // sequence specified by |expression| is not prefixed by a plus or
105 // minus sign, it indicates that all scopes are first disabled before
106 // enabled by |expression|.
107 if (expression[0] != '+' && expression[0] != '-')
108 DisableAllScopes();
109
110 bool enable_scope = true;
111 StringTokenizer tokenizer(expression, "+-");
112 tokenizer.set_options(StringTokenizer::RETURN_DELIMS);
113 while (tokenizer.GetNext()) {
114 if (tokenizer.token_is_delim()) {
115 enable_scope = (tokenizer.token() == "+");
116 continue;
117 }
118
119 if (tokenizer.token().empty())
120 continue;
121
122 size_t i;
123 for (i = 0; i < arraysize(kScopeNames); ++i) {
124 if (tokenizer.token() == kScopeNames[i]) {
125 SetScopeEnabled(static_cast<Scope>(i), enable_scope);
126 break;
127 }
128 }
129 LOG_IF(WARNING, i == arraysize(kScopeNames))
130 << "Unknown scope '" << tokenizer.token() << "'";
131 }
132}
133
134void ScopeLogger::DisableAllScopes() {
135 scope_enabled_.reset();
136}
137
138void ScopeLogger::SetScopeEnabled(Scope scope, bool enabled) {
139 CHECK_GE(scope, 0);
140 CHECK_LT(scope, kNumScopes);
141
142 scope_enabled_[scope] = enabled;
143}
144
145} // namespace shill