xref: /aosp_15_r20/external/libchrome/base/vlog.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/vlog.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
8*635a8641SAndroid Build Coastguard Worker 
9*635a8641SAndroid Build Coastguard Worker #include <ostream>
10*635a8641SAndroid Build Coastguard Worker #include <utility>
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_number_conversions.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_split.h"
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker namespace logging {
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker const int VlogInfo::kDefaultVlogLevel = 0;
20*635a8641SAndroid Build Coastguard Worker 
21*635a8641SAndroid Build Coastguard Worker struct VlogInfo::VmodulePattern {
22*635a8641SAndroid Build Coastguard Worker   enum MatchTarget { MATCH_MODULE, MATCH_FILE };
23*635a8641SAndroid Build Coastguard Worker 
24*635a8641SAndroid Build Coastguard Worker   explicit VmodulePattern(const std::string& pattern);
25*635a8641SAndroid Build Coastguard Worker 
26*635a8641SAndroid Build Coastguard Worker   VmodulePattern();
27*635a8641SAndroid Build Coastguard Worker 
28*635a8641SAndroid Build Coastguard Worker   std::string pattern;
29*635a8641SAndroid Build Coastguard Worker   int vlog_level;
30*635a8641SAndroid Build Coastguard Worker   MatchTarget match_target;
31*635a8641SAndroid Build Coastguard Worker };
32*635a8641SAndroid Build Coastguard Worker 
VmodulePattern(const std::string & pattern)33*635a8641SAndroid Build Coastguard Worker VlogInfo::VmodulePattern::VmodulePattern(const std::string& pattern)
34*635a8641SAndroid Build Coastguard Worker     : pattern(pattern),
35*635a8641SAndroid Build Coastguard Worker       vlog_level(VlogInfo::kDefaultVlogLevel),
36*635a8641SAndroid Build Coastguard Worker       match_target(MATCH_MODULE) {
37*635a8641SAndroid Build Coastguard Worker   // If the pattern contains a {forward,back} slash, we assume that
38*635a8641SAndroid Build Coastguard Worker   // it's meant to be tested against the entire __FILE__ string.
39*635a8641SAndroid Build Coastguard Worker   std::string::size_type first_slash = pattern.find_first_of("\\/");
40*635a8641SAndroid Build Coastguard Worker   if (first_slash != std::string::npos)
41*635a8641SAndroid Build Coastguard Worker     match_target = MATCH_FILE;
42*635a8641SAndroid Build Coastguard Worker }
43*635a8641SAndroid Build Coastguard Worker 
VmodulePattern()44*635a8641SAndroid Build Coastguard Worker VlogInfo::VmodulePattern::VmodulePattern()
45*635a8641SAndroid Build Coastguard Worker     : vlog_level(VlogInfo::kDefaultVlogLevel),
46*635a8641SAndroid Build Coastguard Worker       match_target(MATCH_MODULE) {}
47*635a8641SAndroid Build Coastguard Worker 
VlogInfo(const std::string & v_switch,const std::string & vmodule_switch,int * min_log_level)48*635a8641SAndroid Build Coastguard Worker VlogInfo::VlogInfo(const std::string& v_switch,
49*635a8641SAndroid Build Coastguard Worker                    const std::string& vmodule_switch,
50*635a8641SAndroid Build Coastguard Worker                    int* min_log_level)
51*635a8641SAndroid Build Coastguard Worker     : min_log_level_(min_log_level) {
52*635a8641SAndroid Build Coastguard Worker   DCHECK_NE(min_log_level, nullptr);
53*635a8641SAndroid Build Coastguard Worker 
54*635a8641SAndroid Build Coastguard Worker   int vlog_level = 0;
55*635a8641SAndroid Build Coastguard Worker   if (!v_switch.empty()) {
56*635a8641SAndroid Build Coastguard Worker     if (base::StringToInt(v_switch, &vlog_level)) {
57*635a8641SAndroid Build Coastguard Worker       SetMaxVlogLevel(vlog_level);
58*635a8641SAndroid Build Coastguard Worker     } else {
59*635a8641SAndroid Build Coastguard Worker       DLOG(WARNING) << "Could not parse v switch \"" << v_switch << "\"";
60*635a8641SAndroid Build Coastguard Worker     }
61*635a8641SAndroid Build Coastguard Worker   }
62*635a8641SAndroid Build Coastguard Worker 
63*635a8641SAndroid Build Coastguard Worker   base::StringPairs kv_pairs;
64*635a8641SAndroid Build Coastguard Worker   if (!base::SplitStringIntoKeyValuePairs(
65*635a8641SAndroid Build Coastguard Worker           vmodule_switch, '=', ',', &kv_pairs)) {
66*635a8641SAndroid Build Coastguard Worker     DLOG(WARNING) << "Could not fully parse vmodule switch \""
67*635a8641SAndroid Build Coastguard Worker                   << vmodule_switch << "\"";
68*635a8641SAndroid Build Coastguard Worker   }
69*635a8641SAndroid Build Coastguard Worker   for (base::StringPairs::const_iterator it = kv_pairs.begin();
70*635a8641SAndroid Build Coastguard Worker        it != kv_pairs.end(); ++it) {
71*635a8641SAndroid Build Coastguard Worker     VmodulePattern pattern(it->first);
72*635a8641SAndroid Build Coastguard Worker     if (!base::StringToInt(it->second, &pattern.vlog_level)) {
73*635a8641SAndroid Build Coastguard Worker       DLOG(WARNING) << "Parsed vlog level for \""
74*635a8641SAndroid Build Coastguard Worker                     << it->first << "=" << it->second
75*635a8641SAndroid Build Coastguard Worker                     << "\" as " << pattern.vlog_level;
76*635a8641SAndroid Build Coastguard Worker     }
77*635a8641SAndroid Build Coastguard Worker     vmodule_levels_.push_back(pattern);
78*635a8641SAndroid Build Coastguard Worker   }
79*635a8641SAndroid Build Coastguard Worker }
80*635a8641SAndroid Build Coastguard Worker 
81*635a8641SAndroid Build Coastguard Worker VlogInfo::~VlogInfo() = default;
82*635a8641SAndroid Build Coastguard Worker 
83*635a8641SAndroid Build Coastguard Worker namespace {
84*635a8641SAndroid Build Coastguard Worker 
85*635a8641SAndroid Build Coastguard Worker // Given a path, returns the basename with the extension chopped off
86*635a8641SAndroid Build Coastguard Worker // (and any -inl suffix).  We avoid using FilePath to minimize the
87*635a8641SAndroid Build Coastguard Worker // number of dependencies the logging system has.
GetModule(const base::StringPiece & file)88*635a8641SAndroid Build Coastguard Worker base::StringPiece GetModule(const base::StringPiece& file) {
89*635a8641SAndroid Build Coastguard Worker   base::StringPiece module(file);
90*635a8641SAndroid Build Coastguard Worker   base::StringPiece::size_type last_slash_pos =
91*635a8641SAndroid Build Coastguard Worker       module.find_last_of("\\/");
92*635a8641SAndroid Build Coastguard Worker   if (last_slash_pos != base::StringPiece::npos)
93*635a8641SAndroid Build Coastguard Worker     module.remove_prefix(last_slash_pos + 1);
94*635a8641SAndroid Build Coastguard Worker   base::StringPiece::size_type extension_start = module.rfind('.');
95*635a8641SAndroid Build Coastguard Worker   module = module.substr(0, extension_start);
96*635a8641SAndroid Build Coastguard Worker   static const char kInlSuffix[] = "-inl";
97*635a8641SAndroid Build Coastguard Worker   static const int kInlSuffixLen = arraysize(kInlSuffix) - 1;
98*635a8641SAndroid Build Coastguard Worker   if (module.ends_with(kInlSuffix))
99*635a8641SAndroid Build Coastguard Worker     module.remove_suffix(kInlSuffixLen);
100*635a8641SAndroid Build Coastguard Worker   return module;
101*635a8641SAndroid Build Coastguard Worker }
102*635a8641SAndroid Build Coastguard Worker 
103*635a8641SAndroid Build Coastguard Worker }  // namespace
104*635a8641SAndroid Build Coastguard Worker 
GetVlogLevel(const base::StringPiece & file) const105*635a8641SAndroid Build Coastguard Worker int VlogInfo::GetVlogLevel(const base::StringPiece& file) const {
106*635a8641SAndroid Build Coastguard Worker   if (!vmodule_levels_.empty()) {
107*635a8641SAndroid Build Coastguard Worker     base::StringPiece module(GetModule(file));
108*635a8641SAndroid Build Coastguard Worker     for (std::vector<VmodulePattern>::const_iterator it =
109*635a8641SAndroid Build Coastguard Worker              vmodule_levels_.begin(); it != vmodule_levels_.end(); ++it) {
110*635a8641SAndroid Build Coastguard Worker       base::StringPiece target(
111*635a8641SAndroid Build Coastguard Worker           (it->match_target == VmodulePattern::MATCH_FILE) ? file : module);
112*635a8641SAndroid Build Coastguard Worker       if (MatchVlogPattern(target, it->pattern))
113*635a8641SAndroid Build Coastguard Worker         return it->vlog_level;
114*635a8641SAndroid Build Coastguard Worker     }
115*635a8641SAndroid Build Coastguard Worker   }
116*635a8641SAndroid Build Coastguard Worker   return GetMaxVlogLevel();
117*635a8641SAndroid Build Coastguard Worker }
118*635a8641SAndroid Build Coastguard Worker 
SetMaxVlogLevel(int level)119*635a8641SAndroid Build Coastguard Worker void VlogInfo::SetMaxVlogLevel(int level) {
120*635a8641SAndroid Build Coastguard Worker   // Log severity is the negative verbosity.
121*635a8641SAndroid Build Coastguard Worker   *min_log_level_ = -level;
122*635a8641SAndroid Build Coastguard Worker }
123*635a8641SAndroid Build Coastguard Worker 
GetMaxVlogLevel() const124*635a8641SAndroid Build Coastguard Worker int VlogInfo::GetMaxVlogLevel() const {
125*635a8641SAndroid Build Coastguard Worker   return -*min_log_level_;
126*635a8641SAndroid Build Coastguard Worker }
127*635a8641SAndroid Build Coastguard Worker 
MatchVlogPattern(const base::StringPiece & string,const base::StringPiece & vlog_pattern)128*635a8641SAndroid Build Coastguard Worker bool MatchVlogPattern(const base::StringPiece& string,
129*635a8641SAndroid Build Coastguard Worker                       const base::StringPiece& vlog_pattern) {
130*635a8641SAndroid Build Coastguard Worker   base::StringPiece p(vlog_pattern);
131*635a8641SAndroid Build Coastguard Worker   base::StringPiece s(string);
132*635a8641SAndroid Build Coastguard Worker   // Consume characters until the next star.
133*635a8641SAndroid Build Coastguard Worker   while (!p.empty() && !s.empty() && (p[0] != '*')) {
134*635a8641SAndroid Build Coastguard Worker     switch (p[0]) {
135*635a8641SAndroid Build Coastguard Worker       // A slash (forward or back) must match a slash (forward or back).
136*635a8641SAndroid Build Coastguard Worker       case '/':
137*635a8641SAndroid Build Coastguard Worker       case '\\':
138*635a8641SAndroid Build Coastguard Worker         if ((s[0] != '/') && (s[0] != '\\'))
139*635a8641SAndroid Build Coastguard Worker           return false;
140*635a8641SAndroid Build Coastguard Worker         break;
141*635a8641SAndroid Build Coastguard Worker 
142*635a8641SAndroid Build Coastguard Worker       // A '?' matches anything.
143*635a8641SAndroid Build Coastguard Worker       case '?':
144*635a8641SAndroid Build Coastguard Worker         break;
145*635a8641SAndroid Build Coastguard Worker 
146*635a8641SAndroid Build Coastguard Worker       // Anything else must match literally.
147*635a8641SAndroid Build Coastguard Worker       default:
148*635a8641SAndroid Build Coastguard Worker         if (p[0] != s[0])
149*635a8641SAndroid Build Coastguard Worker           return false;
150*635a8641SAndroid Build Coastguard Worker         break;
151*635a8641SAndroid Build Coastguard Worker     }
152*635a8641SAndroid Build Coastguard Worker     p.remove_prefix(1), s.remove_prefix(1);
153*635a8641SAndroid Build Coastguard Worker   }
154*635a8641SAndroid Build Coastguard Worker 
155*635a8641SAndroid Build Coastguard Worker   // An empty pattern here matches only an empty string.
156*635a8641SAndroid Build Coastguard Worker   if (p.empty())
157*635a8641SAndroid Build Coastguard Worker     return s.empty();
158*635a8641SAndroid Build Coastguard Worker 
159*635a8641SAndroid Build Coastguard Worker   // Coalesce runs of consecutive stars.  There should be at least
160*635a8641SAndroid Build Coastguard Worker   // one.
161*635a8641SAndroid Build Coastguard Worker   while (!p.empty() && (p[0] == '*'))
162*635a8641SAndroid Build Coastguard Worker     p.remove_prefix(1);
163*635a8641SAndroid Build Coastguard Worker 
164*635a8641SAndroid Build Coastguard Worker   // Since we moved past the stars, an empty pattern here matches
165*635a8641SAndroid Build Coastguard Worker   // anything.
166*635a8641SAndroid Build Coastguard Worker   if (p.empty())
167*635a8641SAndroid Build Coastguard Worker     return true;
168*635a8641SAndroid Build Coastguard Worker 
169*635a8641SAndroid Build Coastguard Worker   // Since we moved past the stars and p is non-empty, if some
170*635a8641SAndroid Build Coastguard Worker   // non-empty substring of s matches p, then we ourselves match.
171*635a8641SAndroid Build Coastguard Worker   while (!s.empty()) {
172*635a8641SAndroid Build Coastguard Worker     if (MatchVlogPattern(s, p))
173*635a8641SAndroid Build Coastguard Worker       return true;
174*635a8641SAndroid Build Coastguard Worker     s.remove_prefix(1);
175*635a8641SAndroid Build Coastguard Worker   }
176*635a8641SAndroid Build Coastguard Worker 
177*635a8641SAndroid Build Coastguard Worker   // Otherwise, we couldn't find a match.
178*635a8641SAndroid Build Coastguard Worker   return false;
179*635a8641SAndroid Build Coastguard Worker }
180*635a8641SAndroid Build Coastguard Worker 
181*635a8641SAndroid Build Coastguard Worker }  // namespace logging
182