| 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" |
| 11 | 11 |
| 12 namespace logging { | 12 namespace logging { |
| 13 | 13 |
| 14 const int VlogInfo::kDefaultVlogLevel = 0; | 14 const int VlogInfo::kDefaultVlogLevel = 0; |
| 15 | 15 |
| 16 struct VlogInfo::VmodulePattern { |
| 17 enum MatchTarget { MATCH_MODULE, MATCH_FILE }; |
| 18 |
| 19 explicit VmodulePattern(const std::string& pattern); |
| 20 |
| 21 VmodulePattern(); |
| 22 |
| 23 std::string pattern; |
| 24 int vlog_level; |
| 25 MatchTarget match_target; |
| 26 }; |
| 27 |
| 16 VlogInfo::VmodulePattern::VmodulePattern(const std::string& pattern) | 28 VlogInfo::VmodulePattern::VmodulePattern(const std::string& pattern) |
| 17 : pattern(pattern), | 29 : pattern(pattern), |
| 18 vlog_level(VlogInfo::kDefaultVlogLevel), | 30 vlog_level(VlogInfo::kDefaultVlogLevel), |
| 19 match_target(MATCH_MODULE) { | 31 match_target(MATCH_MODULE) { |
| 20 // If the pattern contains a {forward,back} slash, we assume that | 32 // If the pattern contains a {forward,back} slash, we assume that |
| 21 // it's meant to be tested against the entire __FILE__ string. | 33 // it's meant to be tested against the entire __FILE__ string. |
| 22 std::string::size_type first_slash = pattern.find_first_of("\\/"); | 34 std::string::size_type first_slash = pattern.find_first_of("\\/"); |
| 23 if (first_slash != std::string::npos) | 35 if (first_slash != std::string::npos) |
| 24 match_target = MATCH_FILE; | 36 match_target = MATCH_FILE; |
| 25 } | 37 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 LOG(WARNING) << "Parsed vlog level for \"" | 69 LOG(WARNING) << "Parsed vlog level for \"" |
| 58 << it->first << "=" << it->second | 70 << it->first << "=" << it->second |
| 59 << "\" as " << pattern.vlog_level; | 71 << "\" as " << pattern.vlog_level; |
| 60 } | 72 } |
| 61 vmodule_levels_.push_back(pattern); | 73 vmodule_levels_.push_back(pattern); |
| 62 } | 74 } |
| 63 } | 75 } |
| 64 | 76 |
| 65 VlogInfo::~VlogInfo() {} | 77 VlogInfo::~VlogInfo() {} |
| 66 | 78 |
| 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 | |
| 76 namespace { | 79 namespace { |
| 77 | 80 |
| 78 // Given a path, returns the basename with the extension chopped off | 81 // Given a path, returns the basename with the extension chopped off |
| 79 // (and any -inl suffix). We avoid using FilePath to minimize the | 82 // (and any -inl suffix). We avoid using FilePath to minimize the |
| 80 // number of dependencies the logging system has. | 83 // number of dependencies the logging system has. |
| 81 base::StringPiece GetModule(const base::StringPiece& file) { | 84 base::StringPiece GetModule(const base::StringPiece& file) { |
| 82 base::StringPiece module(file); | 85 base::StringPiece module(file); |
| 83 base::StringPiece::size_type last_slash_pos = | 86 base::StringPiece::size_type last_slash_pos = |
| 84 module.find_last_of("\\/"); | 87 module.find_last_of("\\/"); |
| 85 if (last_slash_pos != base::StringPiece::npos) | 88 if (last_slash_pos != base::StringPiece::npos) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 102 vmodule_levels_.begin(); it != vmodule_levels_.end(); ++it) { | 105 vmodule_levels_.begin(); it != vmodule_levels_.end(); ++it) { |
| 103 base::StringPiece target( | 106 base::StringPiece target( |
| 104 (it->match_target == VmodulePattern::MATCH_FILE) ? file : module); | 107 (it->match_target == VmodulePattern::MATCH_FILE) ? file : module); |
| 105 if (MatchVlogPattern(target, it->pattern)) | 108 if (MatchVlogPattern(target, it->pattern)) |
| 106 return it->vlog_level; | 109 return it->vlog_level; |
| 107 } | 110 } |
| 108 } | 111 } |
| 109 return GetMaxVlogLevel(); | 112 return GetMaxVlogLevel(); |
| 110 } | 113 } |
| 111 | 114 |
| 115 void VlogInfo::SetMaxVlogLevel(int level) { |
| 116 // Log severity is the negative verbosity. |
| 117 *min_log_level_ = -level; |
| 118 } |
| 119 |
| 120 int VlogInfo::GetMaxVlogLevel() const { |
| 121 return -*min_log_level_; |
| 122 } |
| 123 |
| 112 bool MatchVlogPattern(const base::StringPiece& string, | 124 bool MatchVlogPattern(const base::StringPiece& string, |
| 113 const base::StringPiece& vlog_pattern) { | 125 const base::StringPiece& vlog_pattern) { |
| 114 base::StringPiece p(vlog_pattern); | 126 base::StringPiece p(vlog_pattern); |
| 115 base::StringPiece s(string); | 127 base::StringPiece s(string); |
| 116 // Consume characters until the next star. | 128 // Consume characters until the next star. |
| 117 while (!p.empty() && !s.empty() && (p[0] != '*')) { | 129 while (!p.empty() && !s.empty() && (p[0] != '*')) { |
| 118 switch (p[0]) { | 130 switch (p[0]) { |
| 119 // A slash (forward or back) must match a slash (forward or back). | 131 // A slash (forward or back) must match a slash (forward or back). |
| 120 case '/': | 132 case '/': |
| 121 case '\\': | 133 case '\\': |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 if (MatchVlogPattern(s, p)) | 168 if (MatchVlogPattern(s, p)) |
| 157 return true; | 169 return true; |
| 158 s.remove_prefix(1); | 170 s.remove_prefix(1); |
| 159 } | 171 } |
| 160 | 172 |
| 161 // Otherwise, we couldn't find a match. | 173 // Otherwise, we couldn't find a match. |
| 162 return false; | 174 return false; |
| 163 } | 175 } |
| 164 | 176 |
| 165 } // namespace | 177 } // namespace |
| OLD | NEW |