1 // 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.
13 #include "base/logging.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
20 const int VlogInfo::kDefaultVlogLevel = 0;
22 struct VlogInfo::VmodulePattern {
23 enum MatchTarget { MATCH_MODULE, MATCH_FILE };
25 explicit VmodulePattern(const std::string& pattern);
27 VmodulePattern() = default;
30 int vlog_level = VlogInfo::kDefaultVlogLevel;
31 MatchTarget match_target = MATCH_MODULE;
35 VlogInfo::VmodulePattern::VmodulePattern(const std::string& pattern)
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("\\/");
40 if (first_slash != std::string::npos)
41 match_target = MATCH_FILE;
44 VlogInfo::VlogInfo(const std::string& v_switch,
45 const std::string& vmodule_switch,
47 : min_log_level_(min_log_level) {
48 DCHECK_NE(min_log_level, nullptr);
51 if (!v_switch.empty()) {
52 if (base::StringToInt(v_switch, &vlog_level)) {
53 SetMaxVlogLevel(vlog_level);
55 DLOG(WARNING) << "Could not parse v switch \"" << v_switch << "\"";
59 base::StringPairs kv_pairs;
60 if (!base::SplitStringIntoKeyValuePairs(
61 vmodule_switch, '=', ',', &kv_pairs)) {
62 DLOG(WARNING) << "Could not fully parse vmodule switch \""
63 << vmodule_switch << "\"";
65 for (base::StringPairs::const_iterator it = kv_pairs.begin();
66 it != kv_pairs.end(); ++it) {
67 VmodulePattern pattern(it->first);
68 if (!base::StringToInt(it->second, &pattern.vlog_level)) {
69 DLOG(WARNING) << "Parsed vlog level for \""
70 << it->first << "=" << it->second
71 << "\" as " << pattern.vlog_level;
73 vmodule_levels_.push_back(pattern);
77 VlogInfo::~VlogInfo() = default;
81 // Given a path, returns the basename with the extension chopped off
82 // (and any -inl suffix). We avoid using FilePath to minimize the
83 // number of dependencies the logging system has.
84 base::StringPiece GetModule(base::StringPiece file) {
85 base::StringPiece module = file;
87 // Chop off the file extension.
88 base::StringPiece::size_type extension_start = module.rfind('.');
89 module = module.substr(0, extension_start);
91 // Chop off the -inl suffix.
92 static constexpr base::StringPiece kInlSuffix("-inl");
93 if (base::EndsWith(module, kInlSuffix))
94 module.remove_suffix(kInlSuffix.size());
96 // Chop off the path up to the start of the file name. Using single-character
97 // overload of `base::StringPiece::find_last_of` for speed; this overload does
98 // not build a lookup table.
99 base::StringPiece::size_type last_slash_pos = module.find_last_of('/');
100 if (last_slash_pos != base::StringPiece::npos) {
101 module.remove_prefix(last_slash_pos + 1);
104 last_slash_pos = module.find_last_of('\\');
105 if (last_slash_pos != base::StringPiece::npos)
106 module.remove_prefix(last_slash_pos + 1);
112 int VlogInfo::GetVlogLevel(base::StringPiece file) {
113 base::AutoLock lock(vmodule_levels_lock_);
114 if (!vmodule_levels_.empty()) {
115 base::StringPiece module(GetModule(file));
116 for (size_t i = 0; i < vmodule_levels_.size(); i++) {
117 VmodulePattern& it = vmodule_levels_[i];
119 const bool kUseFile = it.match_target == VmodulePattern::MATCH_FILE;
120 if (!MatchVlogPattern(kUseFile ? file : module, it.pattern)) {
123 const int ret = it.vlog_level;
125 // Since `it` matched, increase its score because we believe it has a
126 // higher probability of winning next time.
127 if (it.score == std::numeric_limits<size_t>::max()) {
128 for (VmodulePattern& pattern : vmodule_levels_) {
133 if (i > 0 && it.score > vmodule_levels_[i - 1].score)
134 std::swap(it, vmodule_levels_[i - 1]);
139 return GetMaxVlogLevel();
142 void VlogInfo::SetMaxVlogLevel(int level) {
143 // Log severity is the negative verbosity.
144 *min_log_level_ = -level;
147 int VlogInfo::GetMaxVlogLevel() const {
148 return -*min_log_level_;
151 bool MatchVlogPattern(base::StringPiece string,
152 base::StringPiece vlog_pattern) {
153 // The code implements the glob matching using a greedy approach described in
154 // https://research.swtch.com/glob.
155 size_t s = 0, nexts = 0;
156 size_t p = 0, nextp = 0;
157 const size_t slen = string.size(), plen = vlog_pattern.size();
158 while (s < slen || p < plen) {
160 switch (vlog_pattern[p]) {
161 // A slash (forward or back) must match a slash (forward or back).
164 if (s < slen && (string[s] == '/' || string[s] == '\\')) {
169 // A '?' matches anything.
181 // Anything else must match literally.
183 if (s < slen && string[s] == vlog_pattern[p]) {
190 // Mismatch - maybe restart.
191 if (0 < nexts && nexts <= slen) {
201 } // namespace logging