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