1 // 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
7 #include "base/logging.h"
8 #include "base/time/time.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace logging {
12
13 namespace {
14
TEST(VlogTest,NoVmodule)15 TEST(VlogTest, NoVmodule) {
16 int min_log_level = 0;
17 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"));
31 }
32
TEST(VlogTest,MatchVlogPattern)33 TEST(VlogTest, MatchVlogPattern) {
34 // 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
TEST(VlogTest,VmoduleBasic)81 TEST(VlogTest, VmoduleBasic) {
82 const char kVSwitch[] = "-1";
83 const char kVModuleSwitch[] =
84 "foo=,bar=0,baz=blah,,qux=0blah1,quux=1,corge.ext=5";
85 int min_log_level = 0;
86 VlogInfo vlog_info(kVSwitch, kVModuleSwitch, &min_log_level);
87 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"));
94 EXPECT_EQ(5, vlog_info.GetVlogLevel("c:\\path/to/corge.ext.h"));
95 }
96
TEST(VlogTest,VmoduleDirs)97 TEST(VlogTest, VmoduleDirs) {
98 const char kVModuleSwitch[] =
99 "foo/bar.cc=1,baz\\*\\qux.cc=2,*quux/*=3,*/*-inl.h=4";
100 int min_log_level = 0;
101 VlogInfo vlog_info(std::string(), kVModuleSwitch, &min_log_level);
102 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"));
105
106 EXPECT_EQ(0, vlog_info.GetVlogLevel("baz/grault/qux.h"));
107 EXPECT_EQ(0, vlog_info.GetVlogLevel("/baz/grault/qux.cc"));
108 EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/qux.cc"));
109 EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/blah/qux.cc"));
110 EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault\\qux.cc"));
111 EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault//blah\\qux.cc"));
112
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"));
115 EXPECT_EQ(3, vlog_info.GetVlogLevel("/foo\\bar/baz\\quux/grault.cc"));
116
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"));
120 }
121
122 } // namespace
123
124 } // namespace logging
125