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. 4 5 #ifndef BASE_VLOG_H_ 6 #define BASE_VLOG_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/base_export.h" 12 #include "base/memory/raw_ptr.h" 13 #include "base/strings/string_piece.h" 14 15 namespace logging { 16 17 // A helper class containing all the settings for vlogging. 18 class BASE_EXPORT VlogInfo { 19 public: 20 static const int kDefaultVlogLevel; 21 22 // |v_switch| gives the default maximal active V-logging level; 0 is 23 // the default. Normally positive values are used for V-logging 24 // levels. 25 // 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). 31 // 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. 35 // 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, 42 int* min_log_level); 43 VlogInfo(const VlogInfo&) = delete; 44 VlogInfo& operator=(const VlogInfo&) = delete; 45 ~VlogInfo(); 46 47 // Returns the vlog level for a given file (usually taken from 48 // __FILE__). 49 int GetVlogLevel(base::StringPiece file) const; 50 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; 54 55 private: 56 void SetMaxVlogLevel(int level); 57 int GetMaxVlogLevel() const; 58 59 // VmodulePattern holds all the information for each pattern parsed 60 // from |vmodule_switch|. 61 struct VmodulePattern { 62 enum MatchTarget { MATCH_MODULE, MATCH_FILE }; 63 64 explicit VmodulePattern(const std::string& pattern); 65 66 VmodulePattern(); 67 68 std::string pattern; 69 int vlog_level; 70 MatchTarget match_target; 71 }; 72 73 VlogInfo(std::vector<VmodulePattern> vmodule_levels, int* min_log_level); 74 75 // Parses `VmodulePatterns` from a string, typically provided on the 76 // commandline. 77 static std::vector<VmodulePattern> ParseVmoduleLevels( 78 const std::string& vmodule_switch); 79 80 const std::vector<VmodulePattern> vmodule_levels_; 81 raw_ptr<int> const min_log_level_; 82 }; 83 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 \. 88 // 89 // Examples: 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); 96 97 } // namespace logging 98 99 #endif // BASE_VLOG_H_ 100