2
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
24
#include "precompiled.hpp"
26
#include "logging/logLevel.hpp"
27
#include "logging/logSelection.hpp"
28
#include "logging/logTagSet.hpp"
29
#include "utilities/globalDefinitions.hpp"
30
#include "utilities/ostream.hpp"
31
#include "logTestUtils.inline.hpp"
32
#include "unittest.hpp"
34
// These tests can only run in debug VMs because they rely on the (debug-only) LogTag::_test
37
#define NON_EXISTING_TAG_SET "logging+test+start+exit+safepoint"
39
// let google test know how to print LogSelection nicely for better error messages
40
void PrintTo(const LogSelection& sel, ::std::ostream* os) {
41
if (sel == LogSelection::Invalid) {
42
*os << "LogSelection::Invalid";
50
TEST(LogSelection, sanity) {
51
LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };
52
LogSelection selection(tags, false, LogLevel::Trace);
54
EXPECT_EQ(2u, selection.ntags());
55
EXPECT_EQ(LogLevel::Trace, selection.level());
57
// Verify that copying the selection also works as expected
58
LogSelection copy = selection;
59
EXPECT_EQ(2u, copy.ntags());
60
EXPECT_EQ(LogLevel::Trace, copy.level());
62
tags[0] = PREFIX_LOG_TAG(gc);
63
tags[1] = PREFIX_LOG_TAG(_NO_TAG);
64
LogSelection copy2(tags, true, LogLevel::Off); // start with a completely different selection
65
copy2 = selection; // and test copy assignment
66
EXPECT_EQ(2u, copy2.ntags());
67
EXPECT_EQ(LogLevel::Trace, copy2.level());
70
TEST(LogSelection, tag_sets_selected) {
71
LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };
72
LogSelection selection(tags, false, LogLevel::Trace);
74
EXPECT_EQ(1u, selection.tag_sets_selected()) << "there should be a single (it's not a wildcard selection) "
75
"tag set selected by this (in gtest libjvm)";
77
EXPECT_EQ(LogTagSet::ntagsets(), LogSelection::parse("all").tag_sets_selected()) << "all should select every tag set";
78
EXPECT_EQ(0u, LogSelection::parse(NON_EXISTING_TAG_SET).tag_sets_selected()) <<
79
"(assuming the tag set doesn't exist) the selection shouldn't select any tag sets";
82
static const char* valid_expression[] = {
83
"all", "gc", "gc+logging", "logging+gc", "logging+gc*", "gc=trace",
84
"logging+gc=trace", "logging*", "logging*=info", "gc+logging*=error"
87
TEST(LogSelection, parse) {
88
LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };
89
LogSelection selection(tags, true, LogLevel::Off);
90
LogSelection parsed = LogSelection::parse("logging+test*=off");
91
EXPECT_EQ(selection, parsed) << "parsed selection not equal to programmatically constructed";
93
// Verify valid expressions parse without problems
94
for (size_t i = 0; i < ARRAY_SIZE(valid_expression); i++) {
95
EXPECT_NE(LogSelection::Invalid, LogSelection::parse(valid_expression[i])) <<
96
"Valid expression '" << valid_expression[i] << "' did not parse";
99
// Test 'all' with each level
100
for (LogLevelType level = LogLevel::First; level <= LogLevel::Last; level = static_cast<LogLevelType>(level + 1)) {
102
int ret = jio_snprintf(buf, sizeof(buf), "all=%s", LogLevel::name(level));
105
LogSelection sel = LogSelection::parse(buf);
106
EXPECT_EQ(LogTagSet::ntagsets(), sel.tag_sets_selected()) << "'all' should select all tag sets";
107
EXPECT_EQ(level, sel.level());
111
LogTagType expected_tags[] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(start),
112
PREFIX_LOG_TAG(exit), PREFIX_LOG_TAG(safepoint) };
113
LogSelection expected(expected_tags, false, LogLevel::Debug);
114
LogSelection five_tag_selection = LogSelection::parse("logging+test+start+exit+safepoint=debug");
115
EXPECT_EQ(5u, five_tag_selection.ntags()) << "parsed wrong number of tags";
116
EXPECT_EQ(expected, five_tag_selection);
117
EXPECT_EQ(LogLevel::Debug, five_tag_selection.level());
119
// Test implicit level
120
selection = LogSelection::parse("logging");
121
EXPECT_EQ(LogLevel::Unspecified, selection.level()) << "parsed implicit level incorrectly";
122
EXPECT_EQ(1u, selection.ntags());
125
TEST(LogSelection, parse_invalid) {
127
// Attempt to parse an expression with too many tags
128
EXPECT_EQ(LogSelection::Invalid, LogSelection::parse(NON_EXISTING_TAG_SET "+gc"));
130
// Construct a bunch of invalid expressions and verify that they don't parse
131
for (size_t i = 0; i < ARRAY_SIZE(valid_expression); i++) {
133
for (size_t j = 0; j < ARRAY_SIZE(invalid_selection_substr); j++) {
134
// Prefix with invalid substr
135
jio_snprintf(buf, sizeof(buf), "%s%s", invalid_selection_substr[j], valid_expression[i]);
136
EXPECT_EQ(LogSelection::Invalid, LogSelection::parse(buf)) << "'" << buf << "'" << " considered legal";
138
// Suffix with invalid substr
139
jio_snprintf(buf, sizeof(buf), "%s%s", valid_expression[i], invalid_selection_substr[j]);
140
EXPECT_EQ(LogSelection::Invalid, LogSelection::parse(buf)) << "'" << buf << "'" << " considered legal";
142
// Use only the invalid substr
143
EXPECT_EQ(LogSelection::Invalid, LogSelection::parse(invalid_selection_substr[j])) <<
144
"'" << invalid_selection_substr[j] << "'" << " considered legal";
147
// Suffix/prefix with some unique invalid prefixes/suffixes
148
jio_snprintf(buf, sizeof(buf), "*%s", valid_expression[i]);
149
EXPECT_EQ(LogSelection::Invalid, LogSelection::parse(buf)) << "'" << buf << "'" << " considered legal";
151
jio_snprintf(buf, sizeof(buf), "logging*%s", valid_expression[i]);
152
EXPECT_EQ(LogSelection::Invalid, LogSelection::parse(buf)) << "'" << buf << "'" << " considered legal";
156
TEST(LogSelection, equals) {
157
LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };
158
LogSelection selection(tags, true, LogLevel::Info);
159
LogSelection copy(tags, true, LogLevel::Info);
160
EXPECT_EQ(selection, selection);
161
EXPECT_EQ(selection, copy);
163
tags[0] = PREFIX_LOG_TAG(gc);
164
LogSelection other_tags(tags, true, LogLevel::Info);
165
EXPECT_NE(selection, other_tags);
167
tags[0] = PREFIX_LOG_TAG(test);
168
tags[1] = PREFIX_LOG_TAG(logging);
169
LogSelection reversed(tags, true, LogLevel::Info);
170
EXPECT_NE(selection, reversed);
172
LogSelection no_wildcard(tags, false, LogLevel::Info);
173
EXPECT_NE(selection, no_wildcard);
175
LogSelection different_level(tags, true, LogLevel::Warning);
176
EXPECT_NE(selection, different_level);
178
tags[2] = PREFIX_LOG_TAG(gc);
179
tags[3] = PREFIX_LOG_TAG(_NO_TAG);
180
LogSelection more_tags(tags, true, LogLevel::Info);
181
EXPECT_NE(selection, more_tags);
183
tags[1] = PREFIX_LOG_TAG(_NO_TAG);
184
LogSelection fewer_tags(tags, true, LogLevel::Info);
185
EXPECT_NE(selection, fewer_tags);
188
TEST(LogSelection, consists_of) {
189
LogTagType tags[LogTag::MaxTags] = {
190
PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG)
192
LogSelection s(tags, false, LogLevel::Off);
193
EXPECT_TRUE(s.consists_of(tags));
195
tags[2] = PREFIX_LOG_TAG(safepoint);
196
EXPECT_FALSE(s.consists_of(tags));
198
s = LogSelection(tags, true, LogLevel::Info);
199
EXPECT_TRUE(s.consists_of(tags));
202
TEST(LogSelection, describe_tags) {
204
LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };
205
LogSelection selection(tags, true, LogLevel::Off);
206
selection.describe_tags_on(&ss);
207
EXPECT_STREQ("logging+test*", ss.freeze());
210
TEST(LogSelection, describe) {
212
LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };
213
LogSelection selection(tags, true, LogLevel::Off);
214
selection.describe_on(&ss);
215
EXPECT_STREQ("logging+test*=off", ss.freeze());