| OLD | NEW |
| (Empty) |
| 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 <cstddef> | |
| 8 #include <ostream> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "base/strings/string_split.h" | |
| 15 | |
| 16 namespace logging { | |
| 17 | |
| 18 const int VlogInfo::kDefaultVlogLevel = 0; | |
| 19 | |
| 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 | |
| 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("\\/"); | |
| 39 if (first_slash != std::string::npos) | |
| 40 match_target = MATCH_FILE; | |
| 41 } | |
| 42 | |
| 43 VlogInfo::VmodulePattern::VmodulePattern() | |
| 44 : vlog_level(VlogInfo::kDefaultVlogLevel), | |
| 45 match_target(MATCH_MODULE) {} | |
| 46 | |
| 47 VlogInfo::VlogInfo(const std::string& v_switch, | |
| 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 | |
| 53 int vlog_level = 0; | |
| 54 if (!v_switch.empty()) { | |
| 55 if (base::StringToInt(v_switch, &vlog_level)) { | |
| 56 SetMaxVlogLevel(vlog_level); | |
| 57 } else { | |
| 58 DLOG(WARNING) << "Could not parse v switch \"" << v_switch << "\""; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 base::StringPairs kv_pairs; | |
| 63 if (!base::SplitStringIntoKeyValuePairs( | |
| 64 vmodule_switch, '=', ',', &kv_pairs)) { | |
| 65 DLOG(WARNING) << "Could not fully parse vmodule switch \"" | |
| 66 << vmodule_switch << "\""; | |
| 67 } | |
| 68 for (base::StringPairs::const_iterator it = kv_pairs.begin(); | |
| 69 it != kv_pairs.end(); ++it) { | |
| 70 VmodulePattern pattern(it->first); | |
| 71 if (!base::StringToInt(it->second, &pattern.vlog_level)) { | |
| 72 DLOG(WARNING) << "Parsed vlog level for \"" | |
| 73 << it->first << "=" << it->second | |
| 74 << "\" as " << pattern.vlog_level; | |
| 75 } | |
| 76 vmodule_levels_.push_back(pattern); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 VlogInfo::~VlogInfo() {} | |
| 81 | |
| 82 namespace { | |
| 83 | |
| 84 // Given a path, returns the basename with the extension chopped off | |
| 85 // (and any -inl suffix). We avoid using FilePath to minimize the | |
| 86 // number of dependencies the logging system has. | |
| 87 base::StringPiece GetModule(const base::StringPiece& file) { | |
| 88 base::StringPiece module(file); | |
| 89 base::StringPiece::size_type last_slash_pos = | |
| 90 module.find_last_of("\\/"); | |
| 91 if (last_slash_pos != base::StringPiece::npos) | |
| 92 module.remove_prefix(last_slash_pos + 1); | |
| 93 base::StringPiece::size_type extension_start = module.rfind('.'); | |
| 94 module = module.substr(0, extension_start); | |
| 95 static const char kInlSuffix[] = "-inl"; | |
| 96 static const int kInlSuffixLen = arraysize(kInlSuffix) - 1; | |
| 97 if (module.ends_with(kInlSuffix)) | |
| 98 module.remove_suffix(kInlSuffixLen); | |
| 99 return module; | |
| 100 } | |
| 101 | |
| 102 } // namespace | |
| 103 | |
| 104 int VlogInfo::GetVlogLevel(const base::StringPiece& file) const { | |
| 105 if (!vmodule_levels_.empty()) { | |
| 106 base::StringPiece module(GetModule(file)); | |
| 107 for (std::vector<VmodulePattern>::const_iterator it = | |
| 108 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; | |
| 113 } | |
| 114 } | |
| 115 return GetMaxVlogLevel(); | |
| 116 } | |
| 117 | |
| 118 void VlogInfo::SetMaxVlogLevel(int level) { | |
| 119 // Log severity is the negative verbosity. | |
| 120 *min_log_level_ = -level; | |
| 121 } | |
| 122 | |
| 123 int VlogInfo::GetMaxVlogLevel() const { | |
| 124 return -*min_log_level_; | |
| 125 } | |
| 126 | |
| 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] != '*')) { | |
| 133 switch (p[0]) { | |
| 134 // A slash (forward or back) must match a slash (forward or back). | |
| 135 case '/': | |
| 136 case '\\': | |
| 137 if ((s[0] != '/') && (s[0] != '\\')) | |
| 138 return false; | |
| 139 break; | |
| 140 | |
| 141 // A '?' matches anything. | |
| 142 case '?': | |
| 143 break; | |
| 144 | |
| 145 // Anything else must match literally. | |
| 146 default: | |
| 147 if (p[0] != s[0]) | |
| 148 return false; | |
| 149 break; | |
| 150 } | |
| 151 p.remove_prefix(1), s.remove_prefix(1); | |
| 152 } | |
| 153 | |
| 154 // An empty pattern here matches only an empty string. | |
| 155 if (p.empty()) | |
| 156 return s.empty(); | |
| 157 | |
| 158 // Coalesce runs of consecutive stars. There should be at least | |
| 159 // one. | |
| 160 while (!p.empty() && (p[0] == '*')) | |
| 161 p.remove_prefix(1); | |
| 162 | |
| 163 // Since we moved past the stars, an empty pattern here matches | |
| 164 // anything. | |
| 165 if (p.empty()) | |
| 166 return true; | |
| 167 | |
| 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. | |
| 170 while (!s.empty()) { | |
| 171 if (MatchVlogPattern(s, p)) | |
| 172 return true; | |
| 173 s.remove_prefix(1); | |
| 174 } | |
| 175 | |
| 176 // Otherwise, we couldn't find a match. | |
| 177 return false; | |
| 178 } | |
| 179 | |
| 180 } // namespace logging | |
| OLD | NEW |