blob: 946505497cc687602225e31cd86371a9b2c6d1ed [file] [log] [blame]
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +09001// 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 "base/basictypes.h"
akalin@chromium.org55a8a812010-11-02 05:50:55 +09008#include "base/logging.h"
brettw@chromium.orgabcde5c2013-02-07 11:57:22 +09009#include "base/strings/string_number_conversions.h"
tfarina@chromium.org63aaf3f2013-02-08 08:01:39 +090010#include "base/strings/string_split.h"
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090011
12namespace logging {
13
14const int VlogInfo::kDefaultVlogLevel = 0;
15
erg@google.com37c078e2011-01-11 09:50:59 +090016struct VlogInfo::VmodulePattern {
17 enum MatchTarget { MATCH_MODULE, MATCH_FILE };
18
19 explicit VmodulePattern(const std::string& pattern);
20
21 VmodulePattern();
22
23 std::string pattern;
24 int vlog_level;
25 MatchTarget match_target;
26};
27
akalin@chromium.org859d7d42010-10-29 09:39:48 +090028VlogInfo::VmodulePattern::VmodulePattern(const std::string& pattern)
29 : pattern(pattern),
30 vlog_level(VlogInfo::kDefaultVlogLevel),
31 match_target(MATCH_MODULE) {
32 // If the pattern contains a {forward,back} slash, we assume that
33 // it's meant to be tested against the entire __FILE__ string.
34 std::string::size_type first_slash = pattern.find_first_of("\\/");
akalin@chromium.org55a8a812010-11-02 05:50:55 +090035 if (first_slash != std::string::npos)
akalin@chromium.org859d7d42010-10-29 09:39:48 +090036 match_target = MATCH_FILE;
akalin@chromium.org859d7d42010-10-29 09:39:48 +090037}
38
39VlogInfo::VmodulePattern::VmodulePattern()
40 : vlog_level(VlogInfo::kDefaultVlogLevel),
41 match_target(MATCH_MODULE) {}
42
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090043VlogInfo::VlogInfo(const std::string& v_switch,
siggi@chromium.org25396e12010-11-05 00:50:49 +090044 const std::string& vmodule_switch,
45 int* min_log_level)
46 : min_log_level_(min_log_level) {
47 DCHECK(min_log_level != NULL);
48
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090049 typedef std::pair<std::string, std::string> KVPair;
siggi@chromium.org25396e12010-11-05 00:50:49 +090050 int vlog_level = 0;
akalin@chromium.orge313b342010-11-25 09:48:25 +090051 if (!v_switch.empty()) {
52 if (base::StringToInt(v_switch, &vlog_level)) {
53 SetMaxVlogLevel(vlog_level);
54 } else {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +090055 DLOG(WARNING) << "Could not parse v switch \"" << v_switch << "\"";
akalin@chromium.orge313b342010-11-25 09:48:25 +090056 }
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090057 }
siggi@chromium.org25396e12010-11-05 00:50:49 +090058
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090059 std::vector<KVPair> kv_pairs;
60 if (!base::SplitStringIntoKeyValuePairs(
61 vmodule_switch, '=', ',', &kv_pairs)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +090062 DLOG(WARNING) << "Could not fully parse vmodule switch \""
63 << vmodule_switch << "\"";
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090064 }
65 for (std::vector<KVPair>::const_iterator it = kv_pairs.begin();
66 it != kv_pairs.end(); ++it) {
akalin@chromium.org859d7d42010-10-29 09:39:48 +090067 VmodulePattern pattern(it->first);
68 if (!base::StringToInt(it->second, &pattern.vlog_level)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +090069 DLOG(WARNING) << "Parsed vlog level for \""
70 << it->first << "=" << it->second
71 << "\" as " << pattern.vlog_level;
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090072 }
akalin@chromium.org859d7d42010-10-29 09:39:48 +090073 vmodule_levels_.push_back(pattern);
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090074 }
75}
76
erg@google.com03131692010-10-15 07:03:13 +090077VlogInfo::~VlogInfo() {}
78
akalin@chromium.org859d7d42010-10-29 09:39:48 +090079namespace {
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.
84base::StringPiece GetModule(const base::StringPiece& file) {
85 base::StringPiece module(file);
86 base::StringPiece::size_type last_slash_pos =
87 module.find_last_of("\\/");
88 if (last_slash_pos != base::StringPiece::npos)
89 module.remove_prefix(last_slash_pos + 1);
90 base::StringPiece::size_type extension_start = module.rfind('.');
91 module = module.substr(0, extension_start);
92 static const char kInlSuffix[] = "-inl";
93 static const int kInlSuffixLen = arraysize(kInlSuffix) - 1;
94 if (module.ends_with(kInlSuffix))
95 module.remove_suffix(kInlSuffixLen);
96 return module;
97}
98
99} // namespace
100
siggi@chromium.org25396e12010-11-05 00:50:49 +0900101int VlogInfo::GetVlogLevel(const base::StringPiece& file) const {
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +0900102 if (!vmodule_levels_.empty()) {
akalin@chromium.org859d7d42010-10-29 09:39:48 +0900103 base::StringPiece module(GetModule(file));
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +0900104 for (std::vector<VmodulePattern>::const_iterator it =
105 vmodule_levels_.begin(); it != vmodule_levels_.end(); ++it) {
akalin@chromium.org859d7d42010-10-29 09:39:48 +0900106 base::StringPiece target(
107 (it->match_target == VmodulePattern::MATCH_FILE) ? file : module);
akalin@chromium.org55a8a812010-11-02 05:50:55 +0900108 if (MatchVlogPattern(target, it->pattern))
akalin@chromium.org859d7d42010-10-29 09:39:48 +0900109 return it->vlog_level;
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +0900110 }
111 }
siggi@chromium.org25396e12010-11-05 00:50:49 +0900112 return GetMaxVlogLevel();
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +0900113}
114
erg@google.com37c078e2011-01-11 09:50:59 +0900115void VlogInfo::SetMaxVlogLevel(int level) {
116 // Log severity is the negative verbosity.
117 *min_log_level_ = -level;
118}
119
120int VlogInfo::GetMaxVlogLevel() const {
121 return -*min_log_level_;
122}
123
akalin@chromium.org55a8a812010-11-02 05:50:55 +0900124bool MatchVlogPattern(const base::StringPiece& string,
125 const base::StringPiece& vlog_pattern) {
126 base::StringPiece p(vlog_pattern);
127 base::StringPiece s(string);
128 // Consume characters until the next star.
129 while (!p.empty() && !s.empty() && (p[0] != '*')) {
130 switch (p[0]) {
131 // A slash (forward or back) must match a slash (forward or back).
132 case '/':
133 case '\\':
134 if ((s[0] != '/') && (s[0] != '\\'))
135 return false;
136 break;
137
138 // A '?' matches anything.
139 case '?':
140 break;
141
142 // Anything else must match literally.
143 default:
144 if (p[0] != s[0])
145 return false;
146 break;
147 }
148 p.remove_prefix(1), s.remove_prefix(1);
149 }
150
151 // An empty pattern here matches only an empty string.
152 if (p.empty())
153 return s.empty();
154
155 // Coalesce runs of consecutive stars. There should be at least
156 // one.
157 while (!p.empty() && (p[0] == '*'))
158 p.remove_prefix(1);
159
160 // Since we moved past the stars, an empty pattern here matches
161 // anything.
162 if (p.empty())
163 return true;
164
165 // Since we moved past the stars and p is non-empty, if some
166 // non-empty substring of s matches p, then we ourselves match.
167 while (!s.empty()) {
168 if (MatchVlogPattern(s, p))
169 return true;
170 s.remove_prefix(1);
171 }
172
173 // Otherwise, we couldn't find a match.
174 return false;
175}
176
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +0900177} // namespace