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.
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
19 const int VlogInfo::kDefaultVlogLevel = 0;
21 struct VlogInfo::VmodulePattern {
22 enum MatchTarget { MATCH_MODULE, MATCH_FILE };
24 explicit VmodulePattern(const std::string& pattern);
30 MatchTarget match_target;
33 VlogInfo::VmodulePattern::VmodulePattern(const std::string& 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("\\/");
40 if (first_slash != std::string::npos)
41 match_target = MATCH_FILE;
44 VlogInfo::VmodulePattern::VmodulePattern()
45 : vlog_level(VlogInfo::kDefaultVlogLevel),
46 match_target(MATCH_MODULE) {}
48 VlogInfo::VlogInfo(const std::string& v_switch,
49 const std::string& vmodule_switch,
51 : min_log_level_(min_log_level) {
52 DCHECK_NE(min_log_level, nullptr);
55 if (!v_switch.empty()) {
56 if (base::StringToInt(v_switch, &vlog_level)) {
57 SetMaxVlogLevel(vlog_level);
59 DLOG(WARNING) << "Could not parse v switch \"" << v_switch << "\"";
63 base::StringPairs kv_pairs;
64 if (!base::SplitStringIntoKeyValuePairs(
65 vmodule_switch, '=', ',', &kv_pairs)) {
66 DLOG(WARNING) << "Could not fully parse vmodule switch \""
67 << vmodule_switch << "\"";
69 for (base::StringPairs::const_iterator it = kv_pairs.begin();
70 it != kv_pairs.end(); ++it) {
71 VmodulePattern pattern(it->first);
72 if (!base::StringToInt(it->second, &pattern.vlog_level)) {
73 DLOG(WARNING) << "Parsed vlog level for \""
74 << it->first << "=" << it->second
75 << "\" as " << pattern.vlog_level;
77 vmodule_levels_.push_back(pattern);
81 VlogInfo::~VlogInfo() = default;
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.
88 base::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);
105 int VlogInfo::GetVlogLevel(const base::StringPiece& file) const {
106 if (!vmodule_levels_.empty()) {
107 base::StringPiece module(GetModule(file));
108 for (auto it = vmodule_levels_.begin(); it != vmodule_levels_.end(); ++it) {
109 base::StringPiece target(
110 (it->match_target == VmodulePattern::MATCH_FILE) ? file : module);
111 if (MatchVlogPattern(target, it->pattern))
112 return it->vlog_level;
115 return GetMaxVlogLevel();
118 void VlogInfo::SetMaxVlogLevel(int level) {
119 // Log severity is the negative verbosity.
120 *min_log_level_ = -level;
123 int VlogInfo::GetMaxVlogLevel() const {
124 return -*min_log_level_;
127 bool 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] != '*')) {
134 // A slash (forward or back) must match a slash (forward or back).
137 if ((s[0] != '/') && (s[0] != '\\'))
141 // A '?' matches anything.
145 // Anything else must match literally.
151 p.remove_prefix(1), s.remove_prefix(1);
154 // An empty pattern here matches only an empty string.
158 // Coalesce runs of consecutive stars. There should be at least
160 while (!p.empty() && (p[0] == '*'))
163 // Since we moved past the stars, an empty pattern here matches
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.
171 if (MatchVlogPattern(s, p))
176 // Otherwise, we couldn't find a match.
180 } // namespace logging