1 // Copyright 2011 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.
11 #include "base/base_export.h"
12 #include "base/memory/raw_ptr.h"
13 #include "base/strings/string_piece.h"
17 // A helper class containing all the settings for vlogging.
18 class BASE_EXPORT VlogInfo {
20 static const int kDefaultVlogLevel;
22 // |v_switch| gives the default maximal active V-logging level; 0 is
23 // the default. Normally positive values are used for V-logging
26 // |vmodule_switch| gives the per-module maximal V-logging levels to
27 // override the value given by |v_switch|.
28 // E.g. "my_module=2,foo*=3" would change the logging level for all
29 // code in source files "my_module.*" and "foo*.*" ("-inl" suffixes
30 // are also disregarded for this matching).
32 // |min_log_level| points to an int that stores the log level. If a valid
33 // |v_switch| is provided, it will set the log level, and the default
34 // vlog severity will be read from there.
36 // Any pattern containing a forward or backward slash will be tested
37 // against the whole pathname and not just the module. E.g.,
38 // "*/foo/bar/*=2" would change the logging level for all code in
39 // source files under a "foo/bar" directory.
40 VlogInfo(const std::string& v_switch,
41 const std::string& vmodule_switch,
43 VlogInfo(const VlogInfo&) = delete;
44 VlogInfo& operator=(const VlogInfo&) = delete;
47 // Returns the vlog level for a given file (usually taken from
49 int GetVlogLevel(base::StringPiece file) const;
51 // Returns a new VlogInfo based on |this| but with extra modules/levels added
52 // according to |vmodule_switch|.
53 VlogInfo* WithSwitches(const std::string& vmodule_switch) const;
56 void SetMaxVlogLevel(int level);
57 int GetMaxVlogLevel() const;
59 // VmodulePattern holds all the information for each pattern parsed
60 // from |vmodule_switch|.
61 struct VmodulePattern {
62 enum MatchTarget { MATCH_MODULE, MATCH_FILE };
64 explicit VmodulePattern(const std::string& pattern);
70 MatchTarget match_target;
73 VlogInfo(std::vector<VmodulePattern> vmodule_levels, int* min_log_level);
75 // Parses `VmodulePatterns` from a string, typically provided on the
77 static std::vector<VmodulePattern> ParseVmoduleLevels(
78 const std::string& vmodule_switch);
80 const std::vector<VmodulePattern> vmodule_levels_;
81 raw_ptr<int> const min_log_level_;
84 // Returns true if the string passed in matches the vlog pattern. The
85 // vlog pattern string can contain wildcards like * and ?. ? matches
86 // exactly one character while * matches 0 or more characters. Also,
87 // as a special case, a / or \ character matches either / or \.
90 // "kh?n" matches "khan" but not "khn" or "khaan"
91 // "kh*n" matches "khn", "khan", or even "khaaaaan"
92 // "/foo\bar" matches "/foo/bar", "\foo\bar", or "/foo\bar"
93 // (disregarding C escaping rules)
94 BASE_EXPORT bool MatchVlogPattern(base::StringPiece string,
95 base::StringPiece vlog_pattern);
97 } // namespace logging
99 #endif // BASE_VLOG_H_