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