1 /* 2 * Copyright (c) 2016, 2017, 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 "prims/jvm.h" 29 #include "unittest.hpp" 30 #include "utilities/globalDefinitions.hpp" 31 32 TEST(LogTagLevelExpression, combination_limit) { 33 size_t max_combinations = LogTagLevelExpression::MaxCombinations; 34 EXPECT_GT(max_combinations, LogTagSet::ntagsets()) 35 << "Combination limit not sufficient for configuring all available tag sets"; 36 } 37 38 TEST(LogTagLevelExpression, parse) { 39 char buf[256]; 40 const char* invalid_substr[] = { 41 "=", "+", " ", "+=", "+=*", "*+", " +", "**", "++", ".", ",", ",," ",+", 42 " *", "all+", "all*", "+all", "+all=Warning", "==Info", "=InfoWarning", 43 "BadTag+", "logging++", "logging*+", ",=", "gc+gc+" 44 }; 45 const char* valid_expression[] = { 46 "all", "gc", "gc,logging", "gc+logging", "logging+gc", "logging+gc,gc", "logging+gc*", "gc=trace", 47 "gc=trace,logging=info", "logging+gc=trace", "logging+gc=trace,gc+logging=warning,logging", 48 "gc,all=info", "logging*", "logging*=info", "gc+logging*=error", "logging*,gc=info" 49 }; 50 51 // Verify valid expressions parse without problems 52 for (size_t i = 0; i < ARRAY_SIZE(valid_expression); i++) { 53 LogTagLevelExpression expr; 54 EXPECT_TRUE(expr.parse(valid_expression[i])) << "Valid expression '" << valid_expression[i] << "' did not parse"; 55 } 56 57 // Verify we can use 'all' with each available level 58 for (uint level = LogLevel::First; level <= LogLevel::Last; level++) { 59 char buf[32]; 60 int ret = jio_snprintf(buf, sizeof(buf), "all=%s", LogLevel::name(static_cast<LogLevelType>(level))); 61 ASSERT_NE(ret, -1); 62 63 LogTagLevelExpression expr; 64 EXPECT_TRUE(expr.parse(buf)); 65 } 66 67 // Verify invalid expressions do not parse 68 for (size_t i = 0; i < ARRAY_SIZE(valid_expression); i++) { 69 for (size_t j = 0; j < ARRAY_SIZE(invalid_substr); j++) { 70 // Prefix with invalid substr 71 LogTagLevelExpression expr; 72 jio_snprintf(buf, sizeof(buf), "%s%s", invalid_substr[j], valid_expression[i]); 73 EXPECT_FALSE(expr.parse(buf)) << "'" << buf << "'" << " considered legal"; 74 75 // Suffix with invalid substr 76 LogTagLevelExpression expr1; 77 jio_snprintf(buf, sizeof(buf), "%s%s", valid_expression[i], invalid_substr[j]); 78 EXPECT_FALSE(expr1.parse(buf)) << "'" << buf << "'" << " considered legal"; 79 80 // Use only the invalid substr 81 LogTagLevelExpression expr2; 82 EXPECT_FALSE(expr2.parse(invalid_substr[j])) << "'" << invalid_substr[j] << "'" << " considered legal"; 83 } 84 85 // Suffix/prefix with some unique invalid prefixes/suffixes 86 LogTagLevelExpression expr; 87 jio_snprintf(buf, sizeof(buf), "*%s", valid_expression[i]); 88 EXPECT_FALSE(expr.parse(buf)) << "'" << buf << "'" << " considered legal"; 89 90 LogTagLevelExpression expr1; 91 jio_snprintf(buf, sizeof(buf), "logging*%s", valid_expression[i]); 92 EXPECT_FALSE(expr1.parse(buf)) << "'" << buf << "'" << " considered legal"; 93 } 94 } 95 96 // Test the level_for() function for an empty expression 97 TEST(LogTagLevelExpression, level_for_empty) { 98 LogTagLevelExpression emptyexpr; 99 ASSERT_TRUE(emptyexpr.parse("")); 100 // All tagsets should be unspecified since the expression doesn't involve any tagset 101 for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) { 102 EXPECT_EQ(LogLevel::Unspecified, emptyexpr.level_for(*ts)); 103 } 104 } 105 106 // Test level_for() with "all" without any specified level 107 TEST(LogTagLevelExpression, level_for_all) { 108 LogTagLevelExpression allexpr; 109 ASSERT_TRUE(allexpr.parse("all")); 110 // Level will be unspecified since no level was given 111 for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) { 112 EXPECT_EQ(LogLevel::Unspecified, allexpr.level_for(*ts)); 113 } 114 } 115 116 // Test level_for() with "all=debug" 117 TEST(LogTagLevelExpression, level_for_all_debug) { 118 LogTagLevelExpression alldebugexpr; 119 ASSERT_TRUE(alldebugexpr.parse("all=debug")); 120 // All tagsets should report debug level 121 for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) { 122 EXPECT_EQ(LogLevel::Debug, alldebugexpr.level_for(*ts)); 123 } 124 } 125 126 // Test level_for() with "all=off" 127 TEST(LogTagLevelExpression, level_for_all_off) { 128 LogTagLevelExpression alloffexpr; 129 ASSERT_TRUE(alloffexpr.parse("all=off")); 130 for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) { 131 EXPECT_EQ(LogLevel::Off, alloffexpr.level_for(*ts)); 132 } 133 } 134 135 // Test level_for() with an expression that has overlap (last subexpression should be used) 136 TEST(LogTagLevelExpression, level_for_overlap) { 137 LogTagLevelExpression overlapexpr; 138 // The all=warning will be overridden with gc=info and/or logging+safepoint*=trace 139 ASSERT_TRUE(overlapexpr.parse("all=warning,gc=info,logging+safepoint*=trace")); 140 for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) { 141 if (ts->contains(PREFIX_LOG_TAG(gc)) && ts->ntags() == 1) { 142 EXPECT_EQ(LogLevel::Info, overlapexpr.level_for(*ts)); 143 } else if (ts->contains(PREFIX_LOG_TAG(logging)) && ts->contains(PREFIX_LOG_TAG(safepoint))) { 144 EXPECT_EQ(LogLevel::Trace, overlapexpr.level_for(*ts)); 145 } else { 146 EXPECT_EQ(LogLevel::Warning, overlapexpr.level_for(*ts)); 147 } 148 } 149 EXPECT_EQ(LogLevel::Warning, overlapexpr.level_for(LogTagSetMapping<LOG_TAGS(class)>::tagset())); 150 EXPECT_EQ(LogLevel::Info, overlapexpr.level_for(LogTagSetMapping<LOG_TAGS(gc)>::tagset())); 151 EXPECT_EQ(LogLevel::Trace, overlapexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, safepoint)>::tagset())); 152 EXPECT_EQ(LogLevel::Trace, 153 overlapexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, gc, class, safepoint, heap)>::tagset())); 154 } 155 156 // Test level_for() with an expression containing two independent subexpressions 157 TEST(LogTagLevelExpression, level_for_disjoint) { 158 LogTagLevelExpression reducedexpr; 159 ASSERT_TRUE(reducedexpr.parse("gc+logging=trace,class*=error")); 160 EXPECT_EQ(LogLevel::Error, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(class)>::tagset())); 161 EXPECT_EQ(LogLevel::Error, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(safepoint, class)>::tagset())); 162 EXPECT_EQ(LogLevel::NotMentioned, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(safepoint)>::tagset())); 163 EXPECT_EQ(LogLevel::NotMentioned, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging)>::tagset())); 164 EXPECT_EQ(LogLevel::NotMentioned, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(gc)>::tagset())); 165 EXPECT_EQ(LogLevel::Trace, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, gc)>::tagset())); 166 } 167 168 // Test level_for() with an expression that is completely overridden in the last part of the expression 169 TEST(LogTagLevelExpression, level_for_override) { 170 LogTagLevelExpression overrideexpr; 171 // No matter what, everything should be set to error level because of the last part 172 ASSERT_TRUE(overrideexpr.parse("logging,gc*=trace,all=error")); 173 EXPECT_EQ(LogLevel::Error, overrideexpr.level_for(LogTagSetMapping<LOG_TAGS(class)>::tagset())); 174 EXPECT_EQ(LogLevel::Error, overrideexpr.level_for(LogTagSetMapping<LOG_TAGS(logging)>::tagset())); 175 EXPECT_EQ(LogLevel::Error, overrideexpr.level_for(LogTagSetMapping<LOG_TAGS(gc)>::tagset())); 176 EXPECT_EQ(LogLevel::Error, overrideexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, gc)>::tagset())); 177 } 178 179 // Test level_for() with a mixed expression with a bit of everything 180 TEST(LogTagLevelExpression, level_for_mixed) { 181 LogTagLevelExpression mixedexpr; 182 ASSERT_TRUE(mixedexpr.parse("all=warning,gc*=debug,gc=trace,safepoint*=off")); 183 EXPECT_EQ(LogLevel::Warning, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging)>::tagset())); 184 EXPECT_EQ(LogLevel::Warning, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, class)>::tagset())); 185 EXPECT_EQ(LogLevel::Debug, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(gc, class)>::tagset())); 186 EXPECT_EQ(LogLevel::Off, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(gc, safepoint, logging)>::tagset())); 187 EXPECT_EQ(LogLevel::Off, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(safepoint)>::tagset())); 188 EXPECT_EQ(LogLevel::Debug, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, gc)>::tagset())); 189 EXPECT_EQ(LogLevel::Trace, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(gc)>::tagset())); 190 }