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.
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"
18 const int VlogInfo::kDefaultVlogLevel
= 0;
20 struct VlogInfo::VmodulePattern
{
21 enum MatchTarget
{ MATCH_MODULE
, MATCH_FILE
};
23 explicit VmodulePattern(const std::string
& pattern
);
29 MatchTarget match_target
;
32 VlogInfo::VmodulePattern::VmodulePattern(const std::string
& 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
,
50 : min_log_level_(min_log_level
) {
51 DCHECK(min_log_level
!= NULL
);
54 if (!v_switch
.empty()) {
55 if (base::StringToInt(v_switch
, &vlog_level
)) {
56 SetMaxVlogLevel(vlog_level
);
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() {}
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
);
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] != '*')) {
134 // A slash (forward or back) must match a slash (forward or back).
137 if ((s
[0] != '/') && (s
[0] != '\\'))
141 // A '?' matches anything.
145 // Anything else must match literally.
151 p
.remove_prefix(1), s
.remove_prefix(1);
154 // An empty pattern here matches only an empty string.
158 // Coalesce runs of consecutive stars. There should be at least
160 while (!p
.empty() && (p
[0] == '*'))
163 // Since we moved past the stars, an empty pattern here matches
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.
171 if (MatchVlogPattern(s
, p
))
176 // Otherwise, we couldn't find a match.
180 } // namespace logging