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