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