Update V8 to version 4.7.47.
[chromium-blink-merge.git] / base / vlog.cc
blob519ceff10c012455b18cd1e891e9590c969ad1ad
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.
5 #include "base/vlog.h"
7 #include <cstddef>
8 #include <ostream>
9 #include <utility>
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"
16 namespace logging {
18 const int VlogInfo::kDefaultVlogLevel = 0;
20 struct VlogInfo::VmodulePattern {
21 enum MatchTarget { MATCH_MODULE, MATCH_FILE };
23 explicit VmodulePattern(const std::string& pattern);
25 VmodulePattern();
27 std::string pattern;
28 int vlog_level;
29 MatchTarget match_target;
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;
43 VlogInfo::VmodulePattern::VmodulePattern()
44 : vlog_level(VlogInfo::kDefaultVlogLevel),
45 match_target(MATCH_MODULE) {}
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);
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 << "\"";
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 << "\"";
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;
76 vmodule_levels_.push_back(pattern);
80 VlogInfo::~VlogInfo() {}
82 namespace {
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;
102 } // namespace
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;
115 return GetMaxVlogLevel();
118 void VlogInfo::SetMaxVlogLevel(int level) {
119 // Log severity is the negative verbosity.
120 *min_log_level_ = -level;
123 int VlogInfo::GetMaxVlogLevel() const {
124 return -*min_log_level_;
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;
141 // A '?' matches anything.
142 case '?':
143 break;
145 // Anything else must match literally.
146 default:
147 if (p[0] != s[0])
148 return false;
149 break;
151 p.remove_prefix(1), s.remove_prefix(1);
154 // An empty pattern here matches only an empty string.
155 if (p.empty())
156 return s.empty();
158 // Coalesce runs of consecutive stars. There should be at least
159 // one.
160 while (!p.empty() && (p[0] == '*'))
161 p.remove_prefix(1);
163 // Since we moved past the stars, an empty pattern here matches
164 // anything.
165 if (p.empty())
166 return true;
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);
176 // Otherwise, we couldn't find a match.
177 return false;
180 } // namespace logging