blob: 24a5d122e857483ab5c79d6b9797d2e3a7e833e3 [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",
Darin Petkov096b3472012-05-15 10:26:22 +020050 "wimax",
Ben Chanbc49ac72012-04-10 19:59:45 -070051};
52
53COMPILE_ASSERT(arraysize(kScopeNames) == ScopeLogger::kNumScopes,
54 scope_tags_does_not_have_expected_number_of_strings);
55
Ben Chanfdfdc872012-04-24 08:31:36 -070056// ScopeLogger needs to be a 'leaky' singleton as it needs to survive to
57// handle logging till the very end of the shill process. Making ScopeLogger
58// leaky is fine as it does not need to clean up or release any resource at
59// destruction.
60base::LazyInstance<ScopeLogger>::Leaky g_scope_logger =
61 LAZY_INSTANCE_INITIALIZER;
Ben Chanbc49ac72012-04-10 19:59:45 -070062
63} // namespace
64
65// static
66ScopeLogger* ScopeLogger::GetInstance() {
67 return g_scope_logger.Pointer();
68}
69
70ScopeLogger::ScopeLogger()
71 : verbose_level_(kDefaultVerboseLevel) {
72}
73
74ScopeLogger::~ScopeLogger() {
75}
76
77bool ScopeLogger::IsLogEnabled(Scope scope, int verbose_level) const {
78 CHECK_GE(scope, 0);
79 CHECK_LT(scope, kNumScopes);
80
81 return scope_enabled_[scope] && verbose_level <= verbose_level_;
82}
83
84string ScopeLogger::GetAllScopeNames() const {
85 vector<string> names(kScopeNames, kScopeNames + arraysize(kScopeNames));
Ben Chan1c722602012-04-17 17:37:35 -070086 return JoinString(names, '+');
Ben Chanbc49ac72012-04-10 19:59:45 -070087}
88
89string ScopeLogger::GetEnabledScopeNames() const {
90 vector<string> names;
91 for (size_t i = 0; i < arraysize(kScopeNames); ++i) {
92 if (scope_enabled_[i])
93 names.push_back(kScopeNames[i]);
94 }
95 return JoinString(names, '+');
96}
97
98void ScopeLogger::EnableScopesByName(const string &expression) {
99 if (expression.empty()) {
100 DisableAllScopes();
101 return;
102 }
103
104 // As described in the header file, if the first scope name in the
105 // sequence specified by |expression| is not prefixed by a plus or
106 // minus sign, it indicates that all scopes are first disabled before
107 // enabled by |expression|.
108 if (expression[0] != '+' && expression[0] != '-')
109 DisableAllScopes();
110
111 bool enable_scope = true;
112 StringTokenizer tokenizer(expression, "+-");
113 tokenizer.set_options(StringTokenizer::RETURN_DELIMS);
114 while (tokenizer.GetNext()) {
115 if (tokenizer.token_is_delim()) {
116 enable_scope = (tokenizer.token() == "+");
117 continue;
118 }
119
120 if (tokenizer.token().empty())
121 continue;
122
123 size_t i;
124 for (i = 0; i < arraysize(kScopeNames); ++i) {
125 if (tokenizer.token() == kScopeNames[i]) {
126 SetScopeEnabled(static_cast<Scope>(i), enable_scope);
127 break;
128 }
129 }
130 LOG_IF(WARNING, i == arraysize(kScopeNames))
131 << "Unknown scope '" << tokenizer.token() << "'";
132 }
133}
134
135void ScopeLogger::DisableAllScopes() {
136 scope_enabled_.reset();
137}
138
139void ScopeLogger::SetScopeEnabled(Scope scope, bool enabled) {
140 CHECK_GE(scope, 0);
141 CHECK_LT(scope, kNumScopes);
142
143 scope_enabled_[scope] = enabled;
144}
145
146} // namespace shill