Home | History | Annotate | Download | only in base
      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/basictypes.h"
      8 #include "base/logging.h"
      9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
     10 #include "base/time.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 namespace logging {
     14 
     15 namespace {
     16 
     17 class VlogTest : public testing::Test {
     18 };
     19 
     20 TEST_F(VlogTest, NoVmodule) {
     21   int min_log_level = 0;
     22   EXPECT_EQ(0, VlogInfo("", "", &min_log_level).GetVlogLevel("test1"));
     23   EXPECT_EQ(0, VlogInfo("0", "", &min_log_level).GetVlogLevel("test2"));
     24   EXPECT_EQ(0, VlogInfo("blah", "", &min_log_level).GetVlogLevel("test3"));
     25   EXPECT_EQ(0, VlogInfo("0blah1", "", &min_log_level).GetVlogLevel("test4"));
     26   EXPECT_EQ(1, VlogInfo("1", "", &min_log_level).GetVlogLevel("test5"));
     27   EXPECT_EQ(5, VlogInfo("5", "", &min_log_level).GetVlogLevel("test6"));
     28 }
     29 
     30 TEST_F(VlogTest, MatchVlogPattern) {
     31   // Degenerate cases.
     32   EXPECT_TRUE(MatchVlogPattern("", ""));
     33   EXPECT_TRUE(MatchVlogPattern("", "****"));
     34   EXPECT_FALSE(MatchVlogPattern("", "x"));
     35   EXPECT_FALSE(MatchVlogPattern("x", ""));
     36 
     37   // Basic.
     38   EXPECT_TRUE(MatchVlogPattern("blah", "blah"));
     39 
     40   // ? should match exactly one character.
     41   EXPECT_TRUE(MatchVlogPattern("blah", "bl?h"));
     42   EXPECT_FALSE(MatchVlogPattern("blh", "bl?h"));
     43   EXPECT_FALSE(MatchVlogPattern("blaah", "bl?h"));
     44   EXPECT_TRUE(MatchVlogPattern("blah", "?lah"));
     45   EXPECT_FALSE(MatchVlogPattern("lah", "?lah"));
     46   EXPECT_FALSE(MatchVlogPattern("bblah", "?lah"));
     47 
     48   // * can match any number (even 0) of characters.
     49   EXPECT_TRUE(MatchVlogPattern("blah", "bl*h"));
     50   EXPECT_TRUE(MatchVlogPattern("blabcdefh", "bl*h"));
     51   EXPECT_TRUE(MatchVlogPattern("blh", "bl*h"));
     52   EXPECT_TRUE(MatchVlogPattern("blah", "*blah"));
     53   EXPECT_TRUE(MatchVlogPattern("ohblah", "*blah"));
     54   EXPECT_TRUE(MatchVlogPattern("blah", "blah*"));
     55   EXPECT_TRUE(MatchVlogPattern("blahhhh", "blah*"));
     56   EXPECT_TRUE(MatchVlogPattern("blahhhh", "blah*"));
     57   EXPECT_TRUE(MatchVlogPattern("blah", "*blah*"));
     58   EXPECT_TRUE(MatchVlogPattern("blahhhh", "*blah*"));
     59   EXPECT_TRUE(MatchVlogPattern("bbbblahhhh", "*blah*"));
     60 
     61   // Multiple *s should work fine.
     62   EXPECT_TRUE(MatchVlogPattern("ballaah", "b*la*h"));
     63   EXPECT_TRUE(MatchVlogPattern("blah", "b*la*h"));
     64   EXPECT_TRUE(MatchVlogPattern("bbbblah", "b*la*h"));
     65   EXPECT_TRUE(MatchVlogPattern("blaaah", "b*la*h"));
     66 
     67   // There should be no escaping going on.
     68   EXPECT_TRUE(MatchVlogPattern("bl\\ah", "bl\\?h"));
     69   EXPECT_FALSE(MatchVlogPattern("bl?h", "bl\\?h"));
     70   EXPECT_TRUE(MatchVlogPattern("bl\\aaaah", "bl\\*h"));
     71   EXPECT_FALSE(MatchVlogPattern("bl*h", "bl\\*h"));
     72 
     73   // Any slash matches any slash.
     74   EXPECT_TRUE(MatchVlogPattern("/b\\lah", "/b\\lah"));
     75   EXPECT_TRUE(MatchVlogPattern("\\b/lah", "/b\\lah"));
     76 }
     77 
     78 TEST_F(VlogTest, VmoduleBasic) {
     79   const char kVSwitch[] = "-1";
     80   const char kVModuleSwitch[] =
     81       "foo=,bar=0,baz=blah,,qux=0blah1,quux=1,corge.ext=5";
     82   int min_log_level = 0;
     83   VlogInfo vlog_info(kVSwitch, kVModuleSwitch, &min_log_level);
     84   EXPECT_EQ(-1, vlog_info.GetVlogLevel("/path/to/grault.cc"));
     85   EXPECT_EQ(0, vlog_info.GetVlogLevel("/path/to/foo.cc"));
     86   EXPECT_EQ(0, vlog_info.GetVlogLevel("D:\\Path\\To\\bar-inl.mm"));
     87   EXPECT_EQ(-1, vlog_info.GetVlogLevel("D:\\path\\to what/bar_unittest.m"));
     88   EXPECT_EQ(0, vlog_info.GetVlogLevel("baz.h"));
     89   EXPECT_EQ(0, vlog_info.GetVlogLevel("/another/path/to/qux.h"));
     90   EXPECT_EQ(1, vlog_info.GetVlogLevel("/path/to/quux"));
     91   EXPECT_EQ(5, vlog_info.GetVlogLevel("c:\\path/to/corge.ext.h"));
     92 }
     93 
     94 TEST_F(VlogTest, VmoduleDirs) {
     95   const char kVModuleSwitch[] =
     96       "foo/bar.cc=1,baz\\*\\qux.cc=2,*quux/*=3,*/*-inl.h=4";
     97   int min_log_level = 0;
     98   VlogInfo vlog_info("", kVModuleSwitch, &min_log_level);
     99   EXPECT_EQ(0, vlog_info.GetVlogLevel("/foo/bar.cc"));
    100   EXPECT_EQ(0, vlog_info.GetVlogLevel("bar.cc"));
    101   EXPECT_EQ(1, vlog_info.GetVlogLevel("foo/bar.cc"));
    102 
    103   EXPECT_EQ(0, vlog_info.GetVlogLevel("baz/grault/qux.h"));
    104   EXPECT_EQ(0, vlog_info.GetVlogLevel("/baz/grault/qux.cc"));
    105   EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/qux.cc"));
    106   EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/blah/qux.cc"));
    107   EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault\\qux.cc"));
    108   EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault//blah\\qux.cc"));
    109 
    110   EXPECT_EQ(0, vlog_info.GetVlogLevel("/foo/bar/baz/quux.cc"));
    111   EXPECT_EQ(3, vlog_info.GetVlogLevel("/foo/bar/baz/quux/grault.cc"));
    112   EXPECT_EQ(3, vlog_info.GetVlogLevel("/foo\\bar/baz\\quux/grault.cc"));
    113 
    114   EXPECT_EQ(0, vlog_info.GetVlogLevel("foo/bar/test-inl.cc"));
    115   EXPECT_EQ(4, vlog_info.GetVlogLevel("foo/bar/test-inl.h"));
    116   EXPECT_EQ(4, vlog_info.GetVlogLevel("foo/bar/baz/blah-inl.h"));
    117 }
    118 
    119 }  // namespace
    120 
    121 }  // namespace logging
    122