vlog.h 3.3 KB

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