| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/vlog.h" | 5 #include "base/vlog.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_split.h" | 10 #include "base/string_split.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 std::string::size_type first_slash = pattern.find_first_of("\\/"); | 22 std::string::size_type first_slash = pattern.find_first_of("\\/"); |
| 23 if (first_slash != std::string::npos) | 23 if (first_slash != std::string::npos) |
| 24 match_target = MATCH_FILE; | 24 match_target = MATCH_FILE; |
| 25 } | 25 } |
| 26 | 26 |
| 27 VlogInfo::VmodulePattern::VmodulePattern() | 27 VlogInfo::VmodulePattern::VmodulePattern() |
| 28 : vlog_level(VlogInfo::kDefaultVlogLevel), | 28 : vlog_level(VlogInfo::kDefaultVlogLevel), |
| 29 match_target(MATCH_MODULE) {} | 29 match_target(MATCH_MODULE) {} |
| 30 | 30 |
| 31 VlogInfo::VlogInfo(const std::string& v_switch, | 31 VlogInfo::VlogInfo(const std::string& v_switch, |
| 32 const std::string& vmodule_switch) | 32 const std::string& vmodule_switch, |
| 33 : max_vlog_level_(kDefaultVlogLevel) { | 33 int* min_log_level) |
| 34 : min_log_level_(min_log_level) { |
| 35 DCHECK(min_log_level != NULL); |
| 36 |
| 34 typedef std::pair<std::string, std::string> KVPair; | 37 typedef std::pair<std::string, std::string> KVPair; |
| 35 if (!v_switch.empty() && | 38 int vlog_level = 0; |
| 36 !base::StringToInt(v_switch, &max_vlog_level_)) { | 39 if (base::StringToInt(v_switch, &vlog_level)) { |
| 40 SetMaxVlogLevel(vlog_level); |
| 41 } else { |
| 37 LOG(WARNING) << "Parsed v switch \"" | 42 LOG(WARNING) << "Parsed v switch \"" |
| 38 << v_switch << "\" as " << max_vlog_level_; | 43 << v_switch << "\" as " << vlog_level; |
| 39 } | 44 } |
| 45 |
| 40 std::vector<KVPair> kv_pairs; | 46 std::vector<KVPair> kv_pairs; |
| 41 if (!base::SplitStringIntoKeyValuePairs( | 47 if (!base::SplitStringIntoKeyValuePairs( |
| 42 vmodule_switch, '=', ',', &kv_pairs)) { | 48 vmodule_switch, '=', ',', &kv_pairs)) { |
| 43 LOG(WARNING) << "Could not fully parse vmodule switch \"" | 49 LOG(WARNING) << "Could not fully parse vmodule switch \"" |
| 44 << vmodule_switch << "\""; | 50 << vmodule_switch << "\""; |
| 45 } | 51 } |
| 46 for (std::vector<KVPair>::const_iterator it = kv_pairs.begin(); | 52 for (std::vector<KVPair>::const_iterator it = kv_pairs.begin(); |
| 47 it != kv_pairs.end(); ++it) { | 53 it != kv_pairs.end(); ++it) { |
| 48 VmodulePattern pattern(it->first); | 54 VmodulePattern pattern(it->first); |
| 49 if (!base::StringToInt(it->second, &pattern.vlog_level)) { | 55 if (!base::StringToInt(it->second, &pattern.vlog_level)) { |
| 50 LOG(WARNING) << "Parsed vlog level for \"" | 56 LOG(WARNING) << "Parsed vlog level for \"" |
| 51 << it->first << "=" << it->second | 57 << it->first << "=" << it->second |
| 52 << "\" as " << pattern.vlog_level; | 58 << "\" as " << pattern.vlog_level; |
| 53 } | 59 } |
| 54 vmodule_levels_.push_back(pattern); | 60 vmodule_levels_.push_back(pattern); |
| 55 } | 61 } |
| 56 } | 62 } |
| 57 | 63 |
| 58 VlogInfo::~VlogInfo() {} | 64 VlogInfo::~VlogInfo() {} |
| 59 | 65 |
| 66 void VlogInfo::SetMaxVlogLevel(int level) { |
| 67 // Log severity is the negative verbosity. |
| 68 *min_log_level_ = -level; |
| 69 } |
| 70 |
| 71 int VlogInfo::GetMaxVlogLevel() const { |
| 72 return -*min_log_level_; |
| 73 } |
| 74 |
| 60 namespace { | 75 namespace { |
| 61 | 76 |
| 62 // Given a path, returns the basename with the extension chopped off | 77 // Given a path, returns the basename with the extension chopped off |
| 63 // (and any -inl suffix). We avoid using FilePath to minimize the | 78 // (and any -inl suffix). We avoid using FilePath to minimize the |
| 64 // number of dependencies the logging system has. | 79 // number of dependencies the logging system has. |
| 65 base::StringPiece GetModule(const base::StringPiece& file) { | 80 base::StringPiece GetModule(const base::StringPiece& file) { |
| 66 base::StringPiece module(file); | 81 base::StringPiece module(file); |
| 67 base::StringPiece::size_type last_slash_pos = | 82 base::StringPiece::size_type last_slash_pos = |
| 68 module.find_last_of("\\/"); | 83 module.find_last_of("\\/"); |
| 69 if (last_slash_pos != base::StringPiece::npos) | 84 if (last_slash_pos != base::StringPiece::npos) |
| 70 module.remove_prefix(last_slash_pos + 1); | 85 module.remove_prefix(last_slash_pos + 1); |
| 71 base::StringPiece::size_type extension_start = module.rfind('.'); | 86 base::StringPiece::size_type extension_start = module.rfind('.'); |
| 72 module = module.substr(0, extension_start); | 87 module = module.substr(0, extension_start); |
| 73 static const char kInlSuffix[] = "-inl"; | 88 static const char kInlSuffix[] = "-inl"; |
| 74 static const int kInlSuffixLen = arraysize(kInlSuffix) - 1; | 89 static const int kInlSuffixLen = arraysize(kInlSuffix) - 1; |
| 75 if (module.ends_with(kInlSuffix)) | 90 if (module.ends_with(kInlSuffix)) |
| 76 module.remove_suffix(kInlSuffixLen); | 91 module.remove_suffix(kInlSuffixLen); |
| 77 return module; | 92 return module; |
| 78 } | 93 } |
| 79 | 94 |
| 80 } // namespace | 95 } // namespace |
| 81 | 96 |
| 82 int VlogInfo::GetVlogLevel(const base::StringPiece& file) { | 97 int VlogInfo::GetVlogLevel(const base::StringPiece& file) const { |
| 83 if (!vmodule_levels_.empty()) { | 98 if (!vmodule_levels_.empty()) { |
| 84 base::StringPiece module(GetModule(file)); | 99 base::StringPiece module(GetModule(file)); |
| 85 for (std::vector<VmodulePattern>::const_iterator it = | 100 for (std::vector<VmodulePattern>::const_iterator it = |
| 86 vmodule_levels_.begin(); it != vmodule_levels_.end(); ++it) { | 101 vmodule_levels_.begin(); it != vmodule_levels_.end(); ++it) { |
| 87 base::StringPiece target( | 102 base::StringPiece target( |
| 88 (it->match_target == VmodulePattern::MATCH_FILE) ? file : module); | 103 (it->match_target == VmodulePattern::MATCH_FILE) ? file : module); |
| 89 if (MatchVlogPattern(target, it->pattern)) | 104 if (MatchVlogPattern(target, it->pattern)) |
| 90 return it->vlog_level; | 105 return it->vlog_level; |
| 91 } | 106 } |
| 92 } | 107 } |
| 93 return max_vlog_level_; | 108 return GetMaxVlogLevel(); |
| 94 } | 109 } |
| 95 | 110 |
| 96 bool MatchVlogPattern(const base::StringPiece& string, | 111 bool MatchVlogPattern(const base::StringPiece& string, |
| 97 const base::StringPiece& vlog_pattern) { | 112 const base::StringPiece& vlog_pattern) { |
| 98 base::StringPiece p(vlog_pattern); | 113 base::StringPiece p(vlog_pattern); |
| 99 base::StringPiece s(string); | 114 base::StringPiece s(string); |
| 100 // Consume characters until the next star. | 115 // Consume characters until the next star. |
| 101 while (!p.empty() && !s.empty() && (p[0] != '*')) { | 116 while (!p.empty() && !s.empty() && (p[0] != '*')) { |
| 102 switch (p[0]) { | 117 switch (p[0]) { |
| 103 // A slash (forward or back) must match a slash (forward or back). | 118 // A slash (forward or back) must match a slash (forward or back). |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 if (MatchVlogPattern(s, p)) | 155 if (MatchVlogPattern(s, p)) |
| 141 return true; | 156 return true; |
| 142 s.remove_prefix(1); | 157 s.remove_prefix(1); |
| 143 } | 158 } |
| 144 | 159 |
| 145 // Otherwise, we couldn't find a match. | 160 // Otherwise, we couldn't find a match. |
| 146 return false; | 161 return false; |
| 147 } | 162 } |
| 148 | 163 |
| 149 } // namespace | 164 } // namespace |
| OLD | NEW |