Upload upstream chromium 94.0.4606.31
[platform/framework/web/chromium-efl.git] / base / vlog.cc
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 <stddef.h>
8 #include <algorithm>
9 #include <limits>
10 #include <ostream>
11 #include <utility>
12
13 #include "base/logging.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
17
18 namespace logging {
19
20 const int VlogInfo::kDefaultVlogLevel = 0;
21
22 struct VlogInfo::VmodulePattern {
23   enum MatchTarget { MATCH_MODULE, MATCH_FILE };
24
25   explicit VmodulePattern(const std::string& pattern);
26
27   VmodulePattern() = default;
28
29   std::string pattern;
30   int vlog_level = VlogInfo::kDefaultVlogLevel;
31   MatchTarget match_target = MATCH_MODULE;
32   size_t score = 0;
33 };
34
35 VlogInfo::VmodulePattern::VmodulePattern(const std::string& pattern)
36     : pattern(pattern) {
37   // If the pattern contains a {forward,back} slash, we assume that
38   // it's meant to be tested against the entire __FILE__ string.
39   std::string::size_type first_slash = pattern.find_first_of("\\/");
40   if (first_slash != std::string::npos)
41     match_target = MATCH_FILE;
42 }
43
44 VlogInfo::VlogInfo(const std::string& v_switch,
45                    const std::string& vmodule_switch,
46                    int* min_log_level)
47     : min_log_level_(min_log_level) {
48   DCHECK_NE(min_log_level, nullptr);
49
50   int vlog_level = 0;
51   if (!v_switch.empty()) {
52     if (base::StringToInt(v_switch, &vlog_level)) {
53       SetMaxVlogLevel(vlog_level);
54     } else {
55       DLOG(WARNING) << "Could not parse v switch \"" << v_switch << "\"";
56     }
57   }
58
59   base::StringPairs kv_pairs;
60   if (!base::SplitStringIntoKeyValuePairs(
61           vmodule_switch, '=', ',', &kv_pairs)) {
62     DLOG(WARNING) << "Could not fully parse vmodule switch \""
63                   << vmodule_switch << "\"";
64   }
65   for (base::StringPairs::const_iterator it = kv_pairs.begin();
66        it != kv_pairs.end(); ++it) {
67     VmodulePattern pattern(it->first);
68     if (!base::StringToInt(it->second, &pattern.vlog_level)) {
69       DLOG(WARNING) << "Parsed vlog level for \""
70                     << it->first << "=" << it->second
71                     << "\" as " << pattern.vlog_level;
72     }
73     vmodule_levels_.push_back(pattern);
74   }
75 }
76
77 VlogInfo::~VlogInfo() = default;
78
79 namespace {
80
81 // Given a path, returns the basename with the extension chopped off
82 // (and any -inl suffix).  We avoid using FilePath to minimize the
83 // number of dependencies the logging system has.
84 base::StringPiece GetModule(base::StringPiece file) {
85   base::StringPiece module = file;
86
87   // Chop off the file extension.
88   base::StringPiece::size_type extension_start = module.rfind('.');
89   module = module.substr(0, extension_start);
90
91   // Chop off the -inl suffix.
92   static constexpr base::StringPiece kInlSuffix("-inl");
93   if (base::EndsWith(module, kInlSuffix))
94     module.remove_suffix(kInlSuffix.size());
95
96   // Chop off the path up to the start of the file name. Using single-character
97   // overload of `base::StringPiece::find_last_of` for speed; this overload does
98   // not build a lookup table.
99   base::StringPiece::size_type last_slash_pos = module.find_last_of('/');
100   if (last_slash_pos != base::StringPiece::npos) {
101     module.remove_prefix(last_slash_pos + 1);
102     return module;
103   }
104   last_slash_pos = module.find_last_of('\\');
105   if (last_slash_pos != base::StringPiece::npos)
106     module.remove_prefix(last_slash_pos + 1);
107   return module;
108 }
109
110 }  // namespace
111
112 int VlogInfo::GetVlogLevel(base::StringPiece file) {
113   base::AutoLock lock(vmodule_levels_lock_);
114   if (!vmodule_levels_.empty()) {
115     base::StringPiece module(GetModule(file));
116     for (size_t i = 0; i < vmodule_levels_.size(); i++) {
117       VmodulePattern& it = vmodule_levels_[i];
118
119       const bool kUseFile = it.match_target == VmodulePattern::MATCH_FILE;
120       if (!MatchVlogPattern(kUseFile ? file : module, it.pattern)) {
121         continue;
122       }
123       const int ret = it.vlog_level;
124
125       // Since `it` matched, increase its score because we believe it has a
126       // higher probability of winning next time.
127       if (it.score == std::numeric_limits<size_t>::max()) {
128         for (VmodulePattern& pattern : vmodule_levels_) {
129           pattern.score = 0;
130         }
131       }
132       ++it.score;
133       if (i > 0 && it.score > vmodule_levels_[i - 1].score)
134         std::swap(it, vmodule_levels_[i - 1]);
135
136       return ret;
137     }
138   }
139   return GetMaxVlogLevel();
140 }
141
142 void VlogInfo::SetMaxVlogLevel(int level) {
143   // Log severity is the negative verbosity.
144   *min_log_level_ = -level;
145 }
146
147 int VlogInfo::GetMaxVlogLevel() const {
148   return -*min_log_level_;
149 }
150
151 bool MatchVlogPattern(base::StringPiece string,
152                       base::StringPiece vlog_pattern) {
153   // The code implements the glob matching using a greedy approach described in
154   // https://research.swtch.com/glob.
155   size_t s = 0, nexts = 0;
156   size_t p = 0, nextp = 0;
157   const size_t slen = string.size(), plen = vlog_pattern.size();
158   while (s < slen || p < plen) {
159     if (p < plen) {
160       switch (vlog_pattern[p]) {
161         // A slash (forward or back) must match a slash (forward or back).
162         case '/':
163         case '\\':
164           if (s < slen && (string[s] == '/' || string[s] == '\\')) {
165             p++, s++;
166             continue;
167           }
168           break;
169         // A '?' matches anything.
170         case '?':
171           if (s < slen) {
172             p++, s++;
173             continue;
174           }
175           break;
176         case '*':
177           nextp = p;
178           nexts = s + 1;
179           p++;
180           continue;
181         // Anything else must match literally.
182         default:
183           if (s < slen && string[s] == vlog_pattern[p]) {
184             p++, s++;
185             continue;
186           }
187           break;
188       }
189     }
190     // Mismatch - maybe restart.
191     if (0 < nexts && nexts <= slen) {
192       p = nextp;
193       s = nexts;
194       continue;
195     }
196     return false;
197   }
198   return true;
199 }
200
201 }  // namespace logging