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