• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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/macros.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   // |log_severity| 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();
44 
45   // Returns the vlog level for a given file (usually taken from
46   // __FILE__).
47   int GetVlogLevel(const base::StringPiece& file) const;
48 
49  private:
50   void SetMaxVlogLevel(int level);
51   int GetMaxVlogLevel() const;
52 
53   // VmodulePattern holds all the information for each pattern parsed
54   // from |vmodule_switch|.
55   struct VmodulePattern;
56   std::vector<VmodulePattern> vmodule_levels_;
57   int* min_log_level_;
58 
59   DISALLOW_COPY_AND_ASSIGN(VlogInfo);
60 };
61 
62 // Returns true if the string passed in matches the vlog pattern.  The
63 // vlog pattern string can contain wildcards like * and ?.  ? matches
64 // exactly one character while * matches 0 or more characters.  Also,
65 // as a special case, a / or \ character matches either / or \.
66 //
67 // Examples:
68 //   "kh?n" matches "khan" but not "khn" or "khaan"
69 //   "kh*n" matches "khn", "khan", or even "khaaaaan"
70 //   "/foo\bar" matches "/foo/bar", "\foo\bar", or "/foo\bar"
71 //     (disregarding C escaping rules)
72 BASE_EXPORT bool MatchVlogPattern(const base::StringPiece& string,
73                                   const base::StringPiece& vlog_pattern);
74 
75 }  // namespace logging
76 
77 #endif  // BASE_VLOG_H_
78