test_logTagLevelExpression.cpp revision 12158:0fe2815ffa74
1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24#include "precompiled.hpp"
25#include "logging/logLevel.hpp"
26#include "logging/logTagLevelExpression.hpp"
27#include "logging/logTagSet.hpp"
28#include "unittest.hpp"
29#include "utilities/globalDefinitions.hpp"
30
31TEST(LogTagLevelExpression, combination_limit) {
32  size_t max_combinations = LogTagLevelExpression::MaxCombinations;
33  EXPECT_GT(max_combinations, LogTagSet::ntagsets())
34      << "Combination limit not sufficient for configuring all available tag sets";
35}
36
37TEST(LogTagLevelExpression, parse) {
38  char buf[256];
39  const char* invalid_substr[] = {
40    "=", "+", " ", "+=", "+=*", "*+", " +", "**", "++", ".", ",", ",," ",+",
41    " *", "all+", "all*", "+all", "+all=Warning", "==Info", "=InfoWarning",
42    "BadTag+", "logging++", "logging*+", ",=", "gc+gc+"
43  };
44  const char* valid_expression[] = {
45    "all", "gc", "gc,logging", "gc+logging", "logging+gc", "logging+gc,gc", "logging+gc*", "gc=trace",
46    "gc=trace,logging=info", "logging+gc=trace", "logging+gc=trace,gc+logging=warning,logging",
47    "gc,all=info", "logging*", "logging*=info", "gc+logging*=error", "logging*,gc=info"
48  };
49
50  // Verify valid expressions parse without problems
51  for (size_t i = 0; i < ARRAY_SIZE(valid_expression); i++) {
52    LogTagLevelExpression expr;
53    EXPECT_TRUE(expr.parse(valid_expression[i])) << "Valid expression '" << valid_expression[i] << "' did not parse";
54  }
55
56  // Verify we can use 'all' with each available level
57  for (uint level = LogLevel::First; level <= LogLevel::Last; level++) {
58    char buf[32];
59    int ret = jio_snprintf(buf, sizeof(buf), "all=%s", LogLevel::name(static_cast<LogLevelType>(level)));
60    ASSERT_NE(ret, -1);
61
62    LogTagLevelExpression expr;
63    EXPECT_TRUE(expr.parse(buf));
64  }
65
66  // Verify invalid expressions do not parse
67  for (size_t i = 0; i < ARRAY_SIZE(valid_expression); i++) {
68    for (size_t j = 0; j < ARRAY_SIZE(invalid_substr); j++) {
69      // Prefix with invalid substr
70      LogTagLevelExpression expr;
71      jio_snprintf(buf, sizeof(buf), "%s%s", invalid_substr[j], valid_expression[i]);
72      EXPECT_FALSE(expr.parse(buf)) << "'" << buf << "'" << " considered legal";
73
74      // Suffix with invalid substr
75      LogTagLevelExpression expr1;
76      jio_snprintf(buf, sizeof(buf), "%s%s", valid_expression[i], invalid_substr[j]);
77      EXPECT_FALSE(expr1.parse(buf)) << "'" << buf << "'" << " considered legal";
78
79      // Use only the invalid substr
80      LogTagLevelExpression expr2;
81      EXPECT_FALSE(expr2.parse(invalid_substr[j])) << "'" << invalid_substr[j] << "'" << " considered legal";
82    }
83
84    // Suffix/prefix with some unique invalid prefixes/suffixes
85    LogTagLevelExpression expr;
86    jio_snprintf(buf, sizeof(buf), "*%s", valid_expression[i]);
87    EXPECT_FALSE(expr.parse(buf)) << "'" << buf << "'" << " considered legal";
88
89    LogTagLevelExpression expr1;
90    jio_snprintf(buf, sizeof(buf), "logging*%s", valid_expression[i]);
91    EXPECT_FALSE(expr1.parse(buf)) << "'" << buf << "'" << " considered legal";
92  }
93}
94
95// Test the level_for() function for an empty expression
96TEST(LogTagLevelExpression, level_for_empty) {
97  LogTagLevelExpression emptyexpr;
98  ASSERT_TRUE(emptyexpr.parse(""));
99  // All tagsets should be unspecified since the expression doesn't involve any tagset
100  for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) {
101    EXPECT_EQ(LogLevel::Unspecified, emptyexpr.level_for(*ts));
102  }
103}
104
105// Test level_for() with "all" without any specified level
106TEST(LogTagLevelExpression, level_for_all) {
107  LogTagLevelExpression allexpr;
108  ASSERT_TRUE(allexpr.parse("all"));
109  // Level will be unspecified since no level was given
110  for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) {
111    EXPECT_EQ(LogLevel::Unspecified, allexpr.level_for(*ts));
112  }
113}
114
115// Test level_for() with "all=debug"
116TEST(LogTagLevelExpression, level_for_all_debug) {
117  LogTagLevelExpression alldebugexpr;
118  ASSERT_TRUE(alldebugexpr.parse("all=debug"));
119  // All tagsets should report debug level
120  for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) {
121    EXPECT_EQ(LogLevel::Debug, alldebugexpr.level_for(*ts));
122  }
123}
124
125// Test level_for() with "all=off"
126TEST(LogTagLevelExpression, level_for_all_off) {
127  LogTagLevelExpression alloffexpr;
128  ASSERT_TRUE(alloffexpr.parse("all=off"));
129  for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) {
130    EXPECT_EQ(LogLevel::Off, alloffexpr.level_for(*ts));
131  }
132}
133
134// Test level_for() with an expression that has overlap (last subexpression should be used)
135TEST(LogTagLevelExpression, level_for_overlap) {
136  LogTagLevelExpression overlapexpr;
137  // The all=warning will be overridden with gc=info and/or logging+safepoint*=trace
138  ASSERT_TRUE(overlapexpr.parse("all=warning,gc=info,logging+safepoint*=trace"));
139  for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) {
140    if (ts->contains(PREFIX_LOG_TAG(gc)) && ts->ntags() == 1) {
141      EXPECT_EQ(LogLevel::Info, overlapexpr.level_for(*ts));
142    } else if (ts->contains(PREFIX_LOG_TAG(logging)) && ts->contains(PREFIX_LOG_TAG(safepoint))) {
143      EXPECT_EQ(LogLevel::Trace, overlapexpr.level_for(*ts));
144    } else {
145      EXPECT_EQ(LogLevel::Warning, overlapexpr.level_for(*ts));
146    }
147  }
148  EXPECT_EQ(LogLevel::Warning, overlapexpr.level_for(LogTagSetMapping<LOG_TAGS(class)>::tagset()));
149  EXPECT_EQ(LogLevel::Info, overlapexpr.level_for(LogTagSetMapping<LOG_TAGS(gc)>::tagset()));
150  EXPECT_EQ(LogLevel::Trace, overlapexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, safepoint)>::tagset()));
151  EXPECT_EQ(LogLevel::Trace,
152            overlapexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, gc, class, safepoint, heap)>::tagset()));
153}
154
155// Test level_for() with an expression containing two independent subexpressions
156TEST(LogTagLevelExpression, level_for_disjoint) {
157  LogTagLevelExpression reducedexpr;
158  ASSERT_TRUE(reducedexpr.parse("gc+logging=trace,class*=error"));
159  EXPECT_EQ(LogLevel::Error, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(class)>::tagset()));
160  EXPECT_EQ(LogLevel::Error, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(safepoint, class)>::tagset()));
161  EXPECT_EQ(LogLevel::NotMentioned, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(safepoint)>::tagset()));
162  EXPECT_EQ(LogLevel::NotMentioned, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging)>::tagset()));
163  EXPECT_EQ(LogLevel::NotMentioned, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(gc)>::tagset()));
164  EXPECT_EQ(LogLevel::Trace, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, gc)>::tagset()));
165}
166
167// Test level_for() with an expression that is completely overridden in the last part of the expression
168TEST(LogTagLevelExpression, level_for_override) {
169  LogTagLevelExpression overrideexpr;
170  // No matter what, everything should be set to error level because of the last part
171  ASSERT_TRUE(overrideexpr.parse("logging,gc*=trace,all=error"));
172  EXPECT_EQ(LogLevel::Error, overrideexpr.level_for(LogTagSetMapping<LOG_TAGS(class)>::tagset()));
173  EXPECT_EQ(LogLevel::Error, overrideexpr.level_for(LogTagSetMapping<LOG_TAGS(logging)>::tagset()));
174  EXPECT_EQ(LogLevel::Error, overrideexpr.level_for(LogTagSetMapping<LOG_TAGS(gc)>::tagset()));
175  EXPECT_EQ(LogLevel::Error, overrideexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, gc)>::tagset()));
176}
177
178// Test level_for() with a mixed expression with a bit of everything
179TEST(LogTagLevelExpression, level_for_mixed) {
180  LogTagLevelExpression mixedexpr;
181  ASSERT_TRUE(mixedexpr.parse("all=warning,gc*=debug,gc=trace,safepoint*=off"));
182  EXPECT_EQ(LogLevel::Warning, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging)>::tagset()));
183  EXPECT_EQ(LogLevel::Warning, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, class)>::tagset()));
184  EXPECT_EQ(LogLevel::Debug, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(gc, class)>::tagset()));
185  EXPECT_EQ(LogLevel::Off, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(gc, safepoint, logging)>::tagset()));
186  EXPECT_EQ(LogLevel::Off, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(safepoint)>::tagset()));
187  EXPECT_EQ(LogLevel::Debug, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, gc)>::tagset()));
188  EXPECT_EQ(LogLevel::Trace, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(gc)>::tagset()));
189}
190