blob: 3c3f49cb13bc68f9fe0f5c6825f394c30575a7de [file] [log] [blame]
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +09001// Copyright (c) 2010 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#include "base/vlog.h"
6
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +09007#include "base/logging.h"
avi@chromium.orgb45ec932013-06-29 00:14:18 +09008#include "base/time/time.h"
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +09009#include "testing/gtest/include/gtest/gtest.h"
10
11namespace logging {
12
13namespace {
14
tfarina@chromium.org6ac12282012-12-04 02:04:28 +090015TEST(VlogTest, NoVmodule) {
siggi@chromium.org25396e12010-11-05 00:50:49 +090016 int min_log_level = 0;
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +090017 EXPECT_EQ(0,
18 VlogInfo(std::string(), std::string(), &min_log_level)
19 .GetVlogLevel("test1"));
20 EXPECT_EQ(0,
21 VlogInfo("0", std::string(), &min_log_level).GetVlogLevel("test2"));
22 EXPECT_EQ(
23 0, VlogInfo("blah", std::string(), &min_log_level).GetVlogLevel("test3"));
24 EXPECT_EQ(
25 0,
26 VlogInfo("0blah1", std::string(), &min_log_level).GetVlogLevel("test4"));
27 EXPECT_EQ(1,
28 VlogInfo("1", std::string(), &min_log_level).GetVlogLevel("test5"));
29 EXPECT_EQ(5,
30 VlogInfo("5", std::string(), &min_log_level).GetVlogLevel("test6"));
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090031}
32
tfarina@chromium.org6ac12282012-12-04 02:04:28 +090033TEST(VlogTest, MatchVlogPattern) {
akalin@chromium.org55a8a812010-11-02 05:50:55 +090034 // Degenerate cases.
35 EXPECT_TRUE(MatchVlogPattern("", ""));
36 EXPECT_TRUE(MatchVlogPattern("", "****"));
37 EXPECT_FALSE(MatchVlogPattern("", "x"));
38 EXPECT_FALSE(MatchVlogPattern("x", ""));
39
40 // Basic.
41 EXPECT_TRUE(MatchVlogPattern("blah", "blah"));
42
43 // ? should match exactly one character.
44 EXPECT_TRUE(MatchVlogPattern("blah", "bl?h"));
45 EXPECT_FALSE(MatchVlogPattern("blh", "bl?h"));
46 EXPECT_FALSE(MatchVlogPattern("blaah", "bl?h"));
47 EXPECT_TRUE(MatchVlogPattern("blah", "?lah"));
48 EXPECT_FALSE(MatchVlogPattern("lah", "?lah"));
49 EXPECT_FALSE(MatchVlogPattern("bblah", "?lah"));
50
51 // * can match any number (even 0) of characters.
52 EXPECT_TRUE(MatchVlogPattern("blah", "bl*h"));
53 EXPECT_TRUE(MatchVlogPattern("blabcdefh", "bl*h"));
54 EXPECT_TRUE(MatchVlogPattern("blh", "bl*h"));
55 EXPECT_TRUE(MatchVlogPattern("blah", "*blah"));
56 EXPECT_TRUE(MatchVlogPattern("ohblah", "*blah"));
57 EXPECT_TRUE(MatchVlogPattern("blah", "blah*"));
58 EXPECT_TRUE(MatchVlogPattern("blahhhh", "blah*"));
59 EXPECT_TRUE(MatchVlogPattern("blahhhh", "blah*"));
60 EXPECT_TRUE(MatchVlogPattern("blah", "*blah*"));
61 EXPECT_TRUE(MatchVlogPattern("blahhhh", "*blah*"));
62 EXPECT_TRUE(MatchVlogPattern("bbbblahhhh", "*blah*"));
63
64 // Multiple *s should work fine.
65 EXPECT_TRUE(MatchVlogPattern("ballaah", "b*la*h"));
66 EXPECT_TRUE(MatchVlogPattern("blah", "b*la*h"));
67 EXPECT_TRUE(MatchVlogPattern("bbbblah", "b*la*h"));
68 EXPECT_TRUE(MatchVlogPattern("blaaah", "b*la*h"));
69
70 // There should be no escaping going on.
71 EXPECT_TRUE(MatchVlogPattern("bl\\ah", "bl\\?h"));
72 EXPECT_FALSE(MatchVlogPattern("bl?h", "bl\\?h"));
73 EXPECT_TRUE(MatchVlogPattern("bl\\aaaah", "bl\\*h"));
74 EXPECT_FALSE(MatchVlogPattern("bl*h", "bl\\*h"));
75
76 // Any slash matches any slash.
77 EXPECT_TRUE(MatchVlogPattern("/b\\lah", "/b\\lah"));
78 EXPECT_TRUE(MatchVlogPattern("\\b/lah", "/b\\lah"));
79}
80
tfarina@chromium.org6ac12282012-12-04 02:04:28 +090081TEST(VlogTest, VmoduleBasic) {
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090082 const char kVSwitch[] = "-1";
83 const char kVModuleSwitch[] =
akalin@chromium.org859d7d42010-10-29 09:39:48 +090084 "foo=,bar=0,baz=blah,,qux=0blah1,quux=1,corge.ext=5";
siggi@chromium.org25396e12010-11-05 00:50:49 +090085 int min_log_level = 0;
86 VlogInfo vlog_info(kVSwitch, kVModuleSwitch, &min_log_level);
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090087 EXPECT_EQ(-1, vlog_info.GetVlogLevel("/path/to/grault.cc"));
88 EXPECT_EQ(0, vlog_info.GetVlogLevel("/path/to/foo.cc"));
89 EXPECT_EQ(0, vlog_info.GetVlogLevel("D:\\Path\\To\\bar-inl.mm"));
90 EXPECT_EQ(-1, vlog_info.GetVlogLevel("D:\\path\\to what/bar_unittest.m"));
91 EXPECT_EQ(0, vlog_info.GetVlogLevel("baz.h"));
92 EXPECT_EQ(0, vlog_info.GetVlogLevel("/another/path/to/qux.h"));
93 EXPECT_EQ(1, vlog_info.GetVlogLevel("/path/to/quux"));
akalin@chromium.org859d7d42010-10-29 09:39:48 +090094 EXPECT_EQ(5, vlog_info.GetVlogLevel("c:\\path/to/corge.ext.h"));
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090095}
96
tfarina@chromium.org6ac12282012-12-04 02:04:28 +090097TEST(VlogTest, VmoduleDirs) {
akalin@chromium.org859d7d42010-10-29 09:39:48 +090098 const char kVModuleSwitch[] =
99 "foo/bar.cc=1,baz\\*\\qux.cc=2,*quux/*=3,*/*-inl.h=4";
siggi@chromium.org25396e12010-11-05 00:50:49 +0900100 int min_log_level = 0;
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +0900101 VlogInfo vlog_info(std::string(), kVModuleSwitch, &min_log_level);
akalin@chromium.org859d7d42010-10-29 09:39:48 +0900102 EXPECT_EQ(0, vlog_info.GetVlogLevel("/foo/bar.cc"));
103 EXPECT_EQ(0, vlog_info.GetVlogLevel("bar.cc"));
104 EXPECT_EQ(1, vlog_info.GetVlogLevel("foo/bar.cc"));
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +0900105
akalin@chromium.org859d7d42010-10-29 09:39:48 +0900106 EXPECT_EQ(0, vlog_info.GetVlogLevel("baz/grault/qux.h"));
107 EXPECT_EQ(0, vlog_info.GetVlogLevel("/baz/grault/qux.cc"));
akalin@chromium.org55a8a812010-11-02 05:50:55 +0900108 EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/qux.cc"));
109 EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/blah/qux.cc"));
akalin@chromium.org859d7d42010-10-29 09:39:48 +0900110 EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault\\qux.cc"));
akalin@chromium.org55a8a812010-11-02 05:50:55 +0900111 EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault//blah\\qux.cc"));
akalin@chromium.org859d7d42010-10-29 09:39:48 +0900112
113 EXPECT_EQ(0, vlog_info.GetVlogLevel("/foo/bar/baz/quux.cc"));
114 EXPECT_EQ(3, vlog_info.GetVlogLevel("/foo/bar/baz/quux/grault.cc"));
akalin@chromium.org55a8a812010-11-02 05:50:55 +0900115 EXPECT_EQ(3, vlog_info.GetVlogLevel("/foo\\bar/baz\\quux/grault.cc"));
akalin@chromium.org859d7d42010-10-29 09:39:48 +0900116
117 EXPECT_EQ(0, vlog_info.GetVlogLevel("foo/bar/test-inl.cc"));
118 EXPECT_EQ(4, vlog_info.GetVlogLevel("foo/bar/test-inl.h"));
119 EXPECT_EQ(4, vlog_info.GetVlogLevel("foo/bar/baz/blah-inl.h"));
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +0900120}
121
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +0900122} // namespace
123
124} // namespace logging