akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "base/vlog.h" |
| 6 | |
| 7 | #include "base/basictypes.h" |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame] | 8 | #include "base/logging.h" |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 9 | #include "base/string_number_conversions.h" |
| 10 | #include "base/string_split.h" |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 11 | |
| 12 | namespace logging { |
| 13 | |
| 14 | const int VlogInfo::kDefaultVlogLevel = 0; |
| 15 | |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 16 | VlogInfo::VmodulePattern::VmodulePattern(const std::string& pattern) |
| 17 | : pattern(pattern), |
| 18 | vlog_level(VlogInfo::kDefaultVlogLevel), |
| 19 | match_target(MATCH_MODULE) { |
| 20 | // If the pattern contains a {forward,back} slash, we assume that |
| 21 | // it's meant to be tested against the entire __FILE__ string. |
| 22 | std::string::size_type first_slash = pattern.find_first_of("\\/"); |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame] | 23 | if (first_slash != std::string::npos) |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 24 | match_target = MATCH_FILE; |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | VlogInfo::VmodulePattern::VmodulePattern() |
| 28 | : vlog_level(VlogInfo::kDefaultVlogLevel), |
| 29 | match_target(MATCH_MODULE) {} |
| 30 | |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 31 | VlogInfo::VlogInfo(const std::string& v_switch, |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 32 | const std::string& vmodule_switch, |
| 33 | int* min_log_level) |
| 34 | : min_log_level_(min_log_level) { |
| 35 | DCHECK(min_log_level != NULL); |
| 36 | |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 37 | typedef std::pair<std::string, std::string> KVPair; |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 38 | int vlog_level = 0; |
akalin@chromium.org | e313b34 | 2010-11-25 09:48:25 +0900 | [diff] [blame^] | 39 | if (!v_switch.empty()) { |
| 40 | if (base::StringToInt(v_switch, &vlog_level)) { |
| 41 | SetMaxVlogLevel(vlog_level); |
| 42 | } else { |
| 43 | LOG(WARNING) << "Could not parse v switch \"" << v_switch << "\""; |
| 44 | } |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 45 | } |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 46 | |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 47 | std::vector<KVPair> kv_pairs; |
| 48 | if (!base::SplitStringIntoKeyValuePairs( |
| 49 | vmodule_switch, '=', ',', &kv_pairs)) { |
| 50 | LOG(WARNING) << "Could not fully parse vmodule switch \"" |
| 51 | << vmodule_switch << "\""; |
| 52 | } |
| 53 | for (std::vector<KVPair>::const_iterator it = kv_pairs.begin(); |
| 54 | it != kv_pairs.end(); ++it) { |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 55 | VmodulePattern pattern(it->first); |
| 56 | if (!base::StringToInt(it->second, &pattern.vlog_level)) { |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 57 | LOG(WARNING) << "Parsed vlog level for \"" |
| 58 | << it->first << "=" << it->second |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 59 | << "\" as " << pattern.vlog_level; |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 60 | } |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 61 | vmodule_levels_.push_back(pattern); |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
erg@google.com | 0313169 | 2010-10-15 07:03:13 +0900 | [diff] [blame] | 65 | VlogInfo::~VlogInfo() {} |
| 66 | |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 67 | void VlogInfo::SetMaxVlogLevel(int level) { |
| 68 | // Log severity is the negative verbosity. |
| 69 | *min_log_level_ = -level; |
| 70 | } |
| 71 | |
| 72 | int VlogInfo::GetMaxVlogLevel() const { |
| 73 | return -*min_log_level_; |
| 74 | } |
| 75 | |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 76 | namespace { |
| 77 | |
| 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(const base::StringPiece& file) { |
| 82 | base::StringPiece module(file); |
| 83 | base::StringPiece::size_type last_slash_pos = |
| 84 | module.find_last_of("\\/"); |
| 85 | if (last_slash_pos != base::StringPiece::npos) |
| 86 | module.remove_prefix(last_slash_pos + 1); |
| 87 | base::StringPiece::size_type extension_start = module.rfind('.'); |
| 88 | module = module.substr(0, extension_start); |
| 89 | static const char kInlSuffix[] = "-inl"; |
| 90 | static const int kInlSuffixLen = arraysize(kInlSuffix) - 1; |
| 91 | if (module.ends_with(kInlSuffix)) |
| 92 | module.remove_suffix(kInlSuffixLen); |
| 93 | return module; |
| 94 | } |
| 95 | |
| 96 | } // namespace |
| 97 | |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 98 | int VlogInfo::GetVlogLevel(const base::StringPiece& file) const { |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 99 | if (!vmodule_levels_.empty()) { |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 100 | base::StringPiece module(GetModule(file)); |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 101 | for (std::vector<VmodulePattern>::const_iterator it = |
| 102 | vmodule_levels_.begin(); it != vmodule_levels_.end(); ++it) { |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 103 | base::StringPiece target( |
| 104 | (it->match_target == VmodulePattern::MATCH_FILE) ? file : module); |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame] | 105 | if (MatchVlogPattern(target, it->pattern)) |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 106 | return it->vlog_level; |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 107 | } |
| 108 | } |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 109 | return GetMaxVlogLevel(); |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 110 | } |
| 111 | |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame] | 112 | bool MatchVlogPattern(const base::StringPiece& string, |
| 113 | const base::StringPiece& vlog_pattern) { |
| 114 | base::StringPiece p(vlog_pattern); |
| 115 | base::StringPiece s(string); |
| 116 | // Consume characters until the next star. |
| 117 | while (!p.empty() && !s.empty() && (p[0] != '*')) { |
| 118 | switch (p[0]) { |
| 119 | // A slash (forward or back) must match a slash (forward or back). |
| 120 | case '/': |
| 121 | case '\\': |
| 122 | if ((s[0] != '/') && (s[0] != '\\')) |
| 123 | return false; |
| 124 | break; |
| 125 | |
| 126 | // A '?' matches anything. |
| 127 | case '?': |
| 128 | break; |
| 129 | |
| 130 | // Anything else must match literally. |
| 131 | default: |
| 132 | if (p[0] != s[0]) |
| 133 | return false; |
| 134 | break; |
| 135 | } |
| 136 | p.remove_prefix(1), s.remove_prefix(1); |
| 137 | } |
| 138 | |
| 139 | // An empty pattern here matches only an empty string. |
| 140 | if (p.empty()) |
| 141 | return s.empty(); |
| 142 | |
| 143 | // Coalesce runs of consecutive stars. There should be at least |
| 144 | // one. |
| 145 | while (!p.empty() && (p[0] == '*')) |
| 146 | p.remove_prefix(1); |
| 147 | |
| 148 | // Since we moved past the stars, an empty pattern here matches |
| 149 | // anything. |
| 150 | if (p.empty()) |
| 151 | return true; |
| 152 | |
| 153 | // Since we moved past the stars and p is non-empty, if some |
| 154 | // non-empty substring of s matches p, then we ourselves match. |
| 155 | while (!s.empty()) { |
| 156 | if (MatchVlogPattern(s, p)) |
| 157 | return true; |
| 158 | s.remove_prefix(1); |
| 159 | } |
| 160 | |
| 161 | // Otherwise, we couldn't find a match. |
| 162 | return false; |
| 163 | } |
| 164 | |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 165 | } // namespace |