1 // Copyright 2010 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
12 #include "base/check_op.h"
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 VlogInfo::VmodulePattern::VmodulePattern(const std::string& pattern)
24 vlog_level(VlogInfo::kDefaultVlogLevel),
25 match_target(MATCH_MODULE) {
26 // If the pattern contains a {forward,back} slash, we assume that
27 // it's meant to be tested against the entire __FILE__ string.
28 std::string::size_type first_slash = pattern.find_first_of("\\/");
29 if (first_slash != std::string::npos)
30 match_target = MATCH_FILE;
33 VlogInfo::VmodulePattern::VmodulePattern()
34 : vlog_level(VlogInfo::kDefaultVlogLevel), match_target(MATCH_MODULE) {}
37 std::vector<VlogInfo::VmodulePattern> VlogInfo::ParseVmoduleLevels(
38 const std::string& vmodule_switch) {
39 std::vector<VmodulePattern> vmodule_levels;
40 base::StringPairs kv_pairs;
41 if (!base::SplitStringIntoKeyValuePairs(vmodule_switch, '=', ',',
43 DLOG(WARNING) << "Could not fully parse vmodule switch \"" << vmodule_switch
46 for (const auto& pair : kv_pairs) {
47 VmodulePattern pattern(pair.first);
48 if (!base::StringToInt(pair.second, &pattern.vlog_level)) {
49 DLOG(WARNING) << "Parsed vlog level for \"" << pair.first << "="
50 << pair.second << "\" as " << pattern.vlog_level;
52 vmodule_levels.push_back(pattern);
54 return vmodule_levels;
57 VlogInfo::VlogInfo(const std::string& v_switch,
58 const std::string& vmodule_switch,
60 : vmodule_levels_(ParseVmoduleLevels(vmodule_switch)),
61 min_log_level_(min_log_level) {
62 DCHECK_NE(min_log_level, nullptr);
65 if (!v_switch.empty()) {
66 if (base::StringToInt(v_switch, &vlog_level)) {
67 SetMaxVlogLevel(vlog_level);
69 DLOG(WARNING) << "Could not parse v switch \"" << v_switch << "\"";
74 VlogInfo::~VlogInfo() = default;
78 // Given a path, returns the basename with the extension chopped off
79 // (and any -inl suffix). We avoid using FilePath to minimize the
80 // number of dependencies the logging system has.
81 base::StringPiece GetModule(base::StringPiece file) {
82 base::StringPiece module(file);
83 base::StringPiece::size_type last_slash_pos = module.find_last_of("\\/");
84 if (last_slash_pos != base::StringPiece::npos)
85 module.remove_prefix(last_slash_pos + 1);
86 base::StringPiece::size_type extension_start = module.rfind('.');
87 module = module.substr(0, extension_start);
88 static const char kInlSuffix[] = "-inl";
89 static const int kInlSuffixLen = std::size(kInlSuffix) - 1;
90 if (base::EndsWith(module, kInlSuffix))
91 module.remove_suffix(kInlSuffixLen);
97 int VlogInfo::GetVlogLevel(base::StringPiece file) const {
98 if (!vmodule_levels_.empty()) {
99 base::StringPiece module(GetModule(file));
100 for (const auto& it : vmodule_levels_) {
101 base::StringPiece target(
102 (it.match_target == VmodulePattern::MATCH_FILE) ? file : module);
103 if (MatchVlogPattern(target, it.pattern))
104 return it.vlog_level;
107 return GetMaxVlogLevel();
110 void VlogInfo::SetMaxVlogLevel(int level) {
111 // Log severity is the negative verbosity.
112 *min_log_level_ = -level;
115 int VlogInfo::GetMaxVlogLevel() const {
116 return -*min_log_level_;
119 VlogInfo::VlogInfo(std::vector<VmodulePattern> vmodule_levels,
121 : vmodule_levels_(std::move(vmodule_levels)),
122 min_log_level_(min_log_level) {}
124 VlogInfo* VlogInfo::WithSwitches(const std::string& vmodule_switch) const {
125 std::vector<VmodulePattern> vmodule_levels = vmodule_levels_;
126 std::vector<VmodulePattern> additional_vmodule_levels =
127 ParseVmoduleLevels(vmodule_switch);
128 vmodule_levels.insert(vmodule_levels.end(), additional_vmodule_levels.begin(),
129 additional_vmodule_levels.end());
130 return new VlogInfo(std::move(vmodule_levels), min_log_level_);
133 bool MatchVlogPattern(base::StringPiece string,
134 base::StringPiece vlog_pattern) {
135 // The code implements the glob matching using a greedy approach described in
136 // https://research.swtch.com/glob.
137 size_t s = 0, nexts = 0;
138 size_t p = 0, nextp = 0;
139 size_t slen = string.size(), plen = vlog_pattern.size();
140 while (s < slen || p < plen) {
142 switch (vlog_pattern[p]) {
143 // A slash (forward or back) must match a slash (forward or back).
146 if (s < slen && (string[s] == '/' || string[s] == '\\')) {
151 // A '?' matches anything.
163 // Anything else must match literally.
165 if (s < slen && string[s] == vlog_pattern[p]) {
172 // Mismatch - maybe restart.
173 if (0 < nexts && nexts <= slen) {
183 } // namespace logging