akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 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 | |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 7 | #include "base/logging.h" |
avi@chromium.org | b45ec93 | 2013-06-29 00:14:18 +0900 | [diff] [blame] | 8 | #include "base/time/time.h" |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
| 11 | namespace logging { |
| 12 | |
| 13 | namespace { |
| 14 | |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 15 | TEST(VlogTest, NoVmodule) { |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 16 | int min_log_level = 0; |
dcheng@chromium.org | 8164c2c | 2013-04-09 17:46:45 +0900 | [diff] [blame] | 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")); |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 31 | } |
| 32 | |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 33 | TEST(VlogTest, MatchVlogPattern) { |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame] | 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 | |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 81 | TEST(VlogTest, VmoduleBasic) { |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 82 | const char kVSwitch[] = "-1"; |
| 83 | const char kVModuleSwitch[] = |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 84 | "foo=,bar=0,baz=blah,,qux=0blah1,quux=1,corge.ext=5"; |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 85 | int min_log_level = 0; |
| 86 | VlogInfo vlog_info(kVSwitch, kVModuleSwitch, &min_log_level); |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 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")); |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 94 | EXPECT_EQ(5, vlog_info.GetVlogLevel("c:\\path/to/corge.ext.h")); |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 95 | } |
| 96 | |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 97 | TEST(VlogTest, VmoduleDirs) { |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 98 | const char kVModuleSwitch[] = |
| 99 | "foo/bar.cc=1,baz\\*\\qux.cc=2,*quux/*=3,*/*-inl.h=4"; |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 100 | int min_log_level = 0; |
dcheng@chromium.org | 8164c2c | 2013-04-09 17:46:45 +0900 | [diff] [blame] | 101 | VlogInfo vlog_info(std::string(), kVModuleSwitch, &min_log_level); |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 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")); |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 105 | |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 106 | EXPECT_EQ(0, vlog_info.GetVlogLevel("baz/grault/qux.h")); |
| 107 | EXPECT_EQ(0, vlog_info.GetVlogLevel("/baz/grault/qux.cc")); |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame] | 108 | EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/qux.cc")); |
| 109 | EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/blah/qux.cc")); |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 110 | EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault\\qux.cc")); |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame] | 111 | EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault//blah\\qux.cc")); |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 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")); |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame] | 115 | EXPECT_EQ(3, vlog_info.GetVlogLevel("/foo\\bar/baz\\quux/grault.cc")); |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 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")); |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 120 | } |
| 121 | |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 122 | } // namespace |
| 123 | |
| 124 | } // namespace logging |