vlog.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "base/vlog.h"
  5. #include <stddef.h>
  6. #include <ostream>
  7. #include <utility>
  8. #include "base/check_op.h"
  9. #include "base/logging.h"
  10. #include "base/strings/string_number_conversions.h"
  11. #include "base/strings/string_split.h"
  12. #include "base/strings/string_util.h"
  13. namespace logging {
  14. const int VlogInfo::kDefaultVlogLevel = 0;
  15. VlogInfo::VmodulePattern::VmodulePattern(const std::string& pattern)
  16. : pattern(pattern),
  17. vlog_level(VlogInfo::kDefaultVlogLevel),
  18. match_target(MATCH_MODULE) {
  19. // If the pattern contains a {forward,back} slash, we assume that
  20. // it's meant to be tested against the entire __FILE__ string.
  21. std::string::size_type first_slash = pattern.find_first_of("\\/");
  22. if (first_slash != std::string::npos)
  23. match_target = MATCH_FILE;
  24. }
  25. VlogInfo::VmodulePattern::VmodulePattern()
  26. : vlog_level(VlogInfo::kDefaultVlogLevel), match_target(MATCH_MODULE) {}
  27. // static
  28. std::vector<VlogInfo::VmodulePattern> VlogInfo::ParseVmoduleLevels(
  29. const std::string& vmodule_switch) {
  30. std::vector<VmodulePattern> vmodule_levels;
  31. base::StringPairs kv_pairs;
  32. if (!base::SplitStringIntoKeyValuePairs(vmodule_switch, '=', ',',
  33. &kv_pairs)) {
  34. DLOG(WARNING) << "Could not fully parse vmodule switch \"" << vmodule_switch
  35. << "\"";
  36. }
  37. for (const auto& pair : kv_pairs) {
  38. VmodulePattern pattern(pair.first);
  39. if (!base::StringToInt(pair.second, &pattern.vlog_level)) {
  40. DLOG(WARNING) << "Parsed vlog level for \"" << pair.first << "="
  41. << pair.second << "\" as " << pattern.vlog_level;
  42. }
  43. vmodule_levels.push_back(pattern);
  44. }
  45. return vmodule_levels;
  46. }
  47. VlogInfo::VlogInfo(const std::string& v_switch,
  48. const std::string& vmodule_switch,
  49. int* min_log_level)
  50. : vmodule_levels_(ParseVmoduleLevels(vmodule_switch)),
  51. min_log_level_(min_log_level) {
  52. DCHECK_NE(min_log_level, nullptr);
  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. VlogInfo::~VlogInfo() = default;
  63. namespace {
  64. // Given a path, returns the basename with the extension chopped off
  65. // (and any -inl suffix). We avoid using FilePath to minimize the
  66. // number of dependencies the logging system has.
  67. base::StringPiece GetModule(base::StringPiece file) {
  68. base::StringPiece module(file);
  69. base::StringPiece::size_type last_slash_pos = module.find_last_of("\\/");
  70. if (last_slash_pos != base::StringPiece::npos)
  71. module.remove_prefix(last_slash_pos + 1);
  72. base::StringPiece::size_type extension_start = module.rfind('.');
  73. module = module.substr(0, extension_start);
  74. static const char kInlSuffix[] = "-inl";
  75. static const int kInlSuffixLen = std::size(kInlSuffix) - 1;
  76. if (base::EndsWith(module, kInlSuffix))
  77. module.remove_suffix(kInlSuffixLen);
  78. return module;
  79. }
  80. } // namespace
  81. int VlogInfo::GetVlogLevel(base::StringPiece file) const {
  82. if (!vmodule_levels_.empty()) {
  83. base::StringPiece module(GetModule(file));
  84. for (const auto& it : vmodule_levels_) {
  85. base::StringPiece target(
  86. (it.match_target == VmodulePattern::MATCH_FILE) ? file : module);
  87. if (MatchVlogPattern(target, it.pattern))
  88. return it.vlog_level;
  89. }
  90. }
  91. return GetMaxVlogLevel();
  92. }
  93. void VlogInfo::SetMaxVlogLevel(int level) {
  94. // Log severity is the negative verbosity.
  95. *min_log_level_ = -level;
  96. }
  97. int VlogInfo::GetMaxVlogLevel() const {
  98. return -*min_log_level_;
  99. }
  100. VlogInfo::VlogInfo(std::vector<VmodulePattern> vmodule_levels,
  101. int* min_log_level)
  102. : vmodule_levels_(std::move(vmodule_levels)),
  103. min_log_level_(min_log_level) {}
  104. VlogInfo* VlogInfo::WithSwitches(const std::string& vmodule_switch) const {
  105. std::vector<VmodulePattern> vmodule_levels = vmodule_levels_;
  106. std::vector<VmodulePattern> additional_vmodule_levels =
  107. ParseVmoduleLevels(vmodule_switch);
  108. vmodule_levels.insert(vmodule_levels.end(), additional_vmodule_levels.begin(),
  109. additional_vmodule_levels.end());
  110. return new VlogInfo(std::move(vmodule_levels), min_log_level_);
  111. }
  112. bool MatchVlogPattern(base::StringPiece string,
  113. base::StringPiece vlog_pattern) {
  114. // The code implements the glob matching using a greedy approach described in
  115. // https://research.swtch.com/glob.
  116. size_t s = 0, nexts = 0;
  117. size_t p = 0, nextp = 0;
  118. size_t slen = string.size(), plen = vlog_pattern.size();
  119. while (s < slen || p < plen) {
  120. if (p < plen) {
  121. switch (vlog_pattern[p]) {
  122. // A slash (forward or back) must match a slash (forward or back).
  123. case '/':
  124. case '\\':
  125. if (s < slen && (string[s] == '/' || string[s] == '\\')) {
  126. p++, s++;
  127. continue;
  128. }
  129. break;
  130. // A '?' matches anything.
  131. case '?':
  132. if (s < slen) {
  133. p++, s++;
  134. continue;
  135. }
  136. break;
  137. case '*':
  138. nextp = p;
  139. nexts = s + 1;
  140. p++;
  141. continue;
  142. // Anything else must match literally.
  143. default:
  144. if (s < slen && string[s] == vlog_pattern[p]) {
  145. p++, s++;
  146. continue;
  147. }
  148. break;
  149. }
  150. }
  151. // Mismatch - maybe restart.
  152. if (0 < nexts && nexts <= slen) {
  153. p = nextp;
  154. s = nexts;
  155. continue;
  156. }
  157. return false;
  158. }
  159. return true;
  160. }
  161. } // namespace logging