blob: 8fcc085b928c6035b439dc9308e933297f507d69 [file] [log] [blame]
[email protected]99b7c57f2010-09-29 19:26:361// Copyright (c) 2010 The Chromium 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 "base/vlog.h"
6
avi9b6f42932015-12-26 22:15:147#include <stddef.h>
8
[email protected]c014f2b32013-09-03 23:29:129#include <ostream>
10#include <utility>
11
[email protected]e11de722010-11-01 20:50:5512#include "base/logging.h"
avi9b6f42932015-12-26 22:15:1413#include "base/macros.h"
[email protected]dfa049e2013-02-07 02:57:2214#include "base/strings/string_number_conversions.h"
[email protected]5ae0b763e2013-02-07 23:01:3915#include "base/strings/string_split.h"
[email protected]99b7c57f2010-09-29 19:26:3616
17namespace logging {
18
19const int VlogInfo::kDefaultVlogLevel = 0;
20
[email protected]eae9c062011-01-11 00:50:5921struct VlogInfo::VmodulePattern {
22 enum MatchTarget { MATCH_MODULE, MATCH_FILE };
23
24 explicit VmodulePattern(const std::string& pattern);
25
26 VmodulePattern();
27
28 std::string pattern;
29 int vlog_level;
30 MatchTarget match_target;
31};
32
[email protected]b0d38d4c2010-10-29 00:39:4833VlogInfo::VmodulePattern::VmodulePattern(const std::string& pattern)
34 : pattern(pattern),
35 vlog_level(VlogInfo::kDefaultVlogLevel),
36 match_target(MATCH_MODULE) {
37 // If the pattern contains a {forward,back} slash, we assume that
38 // it's meant to be tested against the entire __FILE__ string.
39 std::string::size_type first_slash = pattern.find_first_of("\\/");
[email protected]e11de722010-11-01 20:50:5540 if (first_slash != std::string::npos)
[email protected]b0d38d4c2010-10-29 00:39:4841 match_target = MATCH_FILE;
[email protected]b0d38d4c2010-10-29 00:39:4842}
43
44VlogInfo::VmodulePattern::VmodulePattern()
45 : vlog_level(VlogInfo::kDefaultVlogLevel),
46 match_target(MATCH_MODULE) {}
47
[email protected]99b7c57f2010-09-29 19:26:3648VlogInfo::VlogInfo(const std::string& v_switch,
[email protected]162ac0f2010-11-04 15:50:4949 const std::string& vmodule_switch,
50 int* min_log_level)
51 : min_log_level_(min_log_level) {
Ivan Kotenkova16212a52017-11-08 12:37:3352 DCHECK_NE(min_log_level, nullptr);
[email protected]162ac0f2010-11-04 15:50:4953
[email protected]162ac0f2010-11-04 15:50:4954 int vlog_level = 0;
[email protected]163285302010-11-25 00:48:2555 if (!v_switch.empty()) {
56 if (base::StringToInt(v_switch, &vlog_level)) {
57 SetMaxVlogLevel(vlog_level);
58 } else {
[email protected]a42d4632011-10-26 21:48:0059 DLOG(WARNING) << "Could not parse v switch \"" << v_switch << "\"";
[email protected]163285302010-11-25 00:48:2560 }
[email protected]99b7c57f2010-09-29 19:26:3661 }
[email protected]162ac0f2010-11-04 15:50:4962
patrikacklandb7936172015-04-29 09:24:4363 base::StringPairs kv_pairs;
[email protected]99b7c57f2010-09-29 19:26:3664 if (!base::SplitStringIntoKeyValuePairs(
65 vmodule_switch, '=', ',', &kv_pairs)) {
[email protected]a42d4632011-10-26 21:48:0066 DLOG(WARNING) << "Could not fully parse vmodule switch \""
67 << vmodule_switch << "\"";
[email protected]99b7c57f2010-09-29 19:26:3668 }
patrikacklandb7936172015-04-29 09:24:4369 for (base::StringPairs::const_iterator it = kv_pairs.begin();
[email protected]99b7c57f2010-09-29 19:26:3670 it != kv_pairs.end(); ++it) {
[email protected]b0d38d4c2010-10-29 00:39:4871 VmodulePattern pattern(it->first);
72 if (!base::StringToInt(it->second, &pattern.vlog_level)) {
[email protected]a42d4632011-10-26 21:48:0073 DLOG(WARNING) << "Parsed vlog level for \""
74 << it->first << "=" << it->second
75 << "\" as " << pattern.vlog_level;
[email protected]99b7c57f2010-09-29 19:26:3676 }
[email protected]b0d38d4c2010-10-29 00:39:4877 vmodule_levels_.push_back(pattern);
[email protected]99b7c57f2010-09-29 19:26:3678 }
79}
80
Chris Watkinsbb7211c2017-11-29 07:16:3881VlogInfo::~VlogInfo() = default;
[email protected]1889dc1b2010-10-14 22:03:1382
[email protected]b0d38d4c2010-10-29 00:39:4883namespace {
84
85// Given a path, returns the basename with the extension chopped off
86// (and any -inl suffix). We avoid using FilePath to minimize the
87// number of dependencies the logging system has.
88base::StringPiece GetModule(const base::StringPiece& file) {
89 base::StringPiece module(file);
90 base::StringPiece::size_type last_slash_pos =
91 module.find_last_of("\\/");
92 if (last_slash_pos != base::StringPiece::npos)
93 module.remove_prefix(last_slash_pos + 1);
94 base::StringPiece::size_type extension_start = module.rfind('.');
95 module = module.substr(0, extension_start);
96 static const char kInlSuffix[] = "-inl";
97 static const int kInlSuffixLen = arraysize(kInlSuffix) - 1;
98 if (module.ends_with(kInlSuffix))
99 module.remove_suffix(kInlSuffixLen);
100 return module;
101}
102
103} // namespace
104
[email protected]162ac0f2010-11-04 15:50:49105int VlogInfo::GetVlogLevel(const base::StringPiece& file) const {
[email protected]99b7c57f2010-09-29 19:26:36106 if (!vmodule_levels_.empty()) {
[email protected]b0d38d4c2010-10-29 00:39:48107 base::StringPiece module(GetModule(file));
jdoerrie6c6229352018-10-22 15:55:43108 for (const auto& it : vmodule_levels_) {
[email protected]b0d38d4c2010-10-29 00:39:48109 base::StringPiece target(
jdoerrie6c6229352018-10-22 15:55:43110 (it.match_target == VmodulePattern::MATCH_FILE) ? file : module);
111 if (MatchVlogPattern(target, it.pattern))
112 return it.vlog_level;
[email protected]99b7c57f2010-09-29 19:26:36113 }
114 }
[email protected]162ac0f2010-11-04 15:50:49115 return GetMaxVlogLevel();
[email protected]99b7c57f2010-09-29 19:26:36116}
117
[email protected]eae9c062011-01-11 00:50:59118void VlogInfo::SetMaxVlogLevel(int level) {
119 // Log severity is the negative verbosity.
120 *min_log_level_ = -level;
121}
122
123int VlogInfo::GetMaxVlogLevel() const {
124 return -*min_log_level_;
125}
126
[email protected]e11de722010-11-01 20:50:55127bool MatchVlogPattern(const base::StringPiece& string,
128 const base::StringPiece& vlog_pattern) {
129 base::StringPiece p(vlog_pattern);
130 base::StringPiece s(string);
131 // Consume characters until the next star.
132 while (!p.empty() && !s.empty() && (p[0] != '*')) {
133 switch (p[0]) {
134 // A slash (forward or back) must match a slash (forward or back).
135 case '/':
136 case '\\':
137 if ((s[0] != '/') && (s[0] != '\\'))
138 return false;
139 break;
140
141 // A '?' matches anything.
142 case '?':
143 break;
144
145 // Anything else must match literally.
146 default:
147 if (p[0] != s[0])
148 return false;
149 break;
150 }
151 p.remove_prefix(1), s.remove_prefix(1);
152 }
153
154 // An empty pattern here matches only an empty string.
155 if (p.empty())
156 return s.empty();
157
158 // Coalesce runs of consecutive stars. There should be at least
159 // one.
160 while (!p.empty() && (p[0] == '*'))
161 p.remove_prefix(1);
162
163 // Since we moved past the stars, an empty pattern here matches
164 // anything.
165 if (p.empty())
166 return true;
167
168 // Since we moved past the stars and p is non-empty, if some
169 // non-empty substring of s matches p, then we ourselves match.
170 while (!s.empty()) {
171 if (MatchVlogPattern(s, p))
172 return true;
173 s.remove_prefix(1);
174 }
175
176 // Otherwise, we couldn't find a match.
177 return false;
178}
179
danakjc3762b92015-03-07 01:51:42180} // namespace logging