blob: a32ed14c7dd1c0a94a09cc54b5982a062063d8ec [file] [log] [blame]
rvargas@google.come106ef62011-03-26 03:48:03 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +09002// 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_
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +09007
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +09008#include <string>
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +09009#include <vector>
10
darin@chromium.orge585bed2011-08-06 00:34:00 +090011#include "base/base_export.h"
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090012#include "base/basictypes.h"
tfarina@chromium.orgb6d49112013-03-30 23:29:00 +090013#include "base/strings/string_piece.h"
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090014
15namespace logging {
16
17// A helper class containing all the settings for vlogging.
darin@chromium.orge585bed2011-08-06 00:34:00 +090018class BASE_EXPORT VlogInfo {
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090019 public:
erg@google.com37c078e2011-01-11 09:50:59 +090020 static const int kDefaultVlogLevel;
21
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090022 // |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).
akalin@chromium.org859d7d42010-10-29 09:39:48 +090031 //
siggi@chromium.org25396e12010-11-05 00:50:49 +090032 // |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 //
akalin@chromium.org859d7d42010-10-29 09:39:48 +090036 // 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.
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090040 VlogInfo(const std::string& v_switch,
siggi@chromium.org25396e12010-11-05 00:50:49 +090041 const std::string& vmodule_switch,
42 int* min_log_level);
erg@google.com03131692010-10-15 07:03:13 +090043 ~VlogInfo();
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090044
45 // Returns the vlog level for a given file (usually taken from
46 // __FILE__).
siggi@chromium.org25396e12010-11-05 00:50:49 +090047 int GetVlogLevel(const base::StringPiece& file) const;
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090048
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090049 private:
siggi@chromium.org25396e12010-11-05 00:50:49 +090050 void SetMaxVlogLevel(int level);
51 int GetMaxVlogLevel() const;
52
akalin@chromium.org859d7d42010-10-29 09:39:48 +090053 // VmodulePattern holds all the information for each pattern parsed
54 // from |vmodule_switch|.
erg@google.com37c078e2011-01-11 09:50:59 +090055 struct VmodulePattern;
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090056 std::vector<VmodulePattern> vmodule_levels_;
siggi@chromium.org25396e12010-11-05 00:50:49 +090057 int* min_log_level_;
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090058
59 DISALLOW_COPY_AND_ASSIGN(VlogInfo);
60};
61
akalin@chromium.org55a8a812010-11-02 05:50:55 +090062// 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)
darin@chromium.orge585bed2011-08-06 00:34:00 +090072BASE_EXPORT bool MatchVlogPattern(const base::StringPiece& string,
73 const base::StringPiece& vlog_pattern);
akalin@chromium.org55a8a812010-11-02 05:50:55 +090074
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090075} // namespace logging
76
77#endif // BASE_VLOG_H_