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