1/* 2 * Copyright (c) 2015, 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/logTagLevelExpression.hpp" 26#include "logging/logTagSet.hpp" 27#include "runtime/arguments.hpp" 28#include "runtime/os.inline.hpp" 29 30const char* LogTagLevelExpression::DefaultExpressionString = "all"; 31 32static bool matches_tagset(const LogTagType tags[], 33 bool allow_other_tags, 34 const LogTagSet& ts) { 35 bool contains_all = true; 36 size_t tag_idx; 37 for (tag_idx = 0; tag_idx < LogTag::MaxTags && tags[tag_idx] != LogTag::__NO_TAG; tag_idx++) { 38 if (!ts.contains(tags[tag_idx])) { 39 contains_all = false; 40 break; 41 } 42 } 43 // All tags in the expression must be part of the tagset, 44 // and either the expression allows other tags (has a wildcard), 45 // or the number of tags in the expression and tagset must match. 46 return contains_all && (allow_other_tags || tag_idx == ts.ntags()); 47} 48 49bool LogTagLevelExpression::verify_tagsets(outputStream* out) const { 50 bool valid = true; 51 52 for (size_t i = 0; i < _ncombinations; i++) { 53 bool matched = false; 54 for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) { 55 if (matches_tagset(_tags[i], _allow_other_tags[i], *ts)) { 56 matched = true; 57 break; 58 } 59 } 60 61 if (!matched) { 62 // If this was the first invalid combination, write the message header 63 if (valid && out != NULL) { 64 out->print("No tag set matches selection(s): "); 65 } 66 valid = false; 67 68 // Break as soon as possible unless listing all invalid combinations 69 if (out == NULL) { 70 break; 71 } 72 73 // List the combination on the outputStream 74 for (size_t t = 0; t < LogTag::MaxTags && _tags[i][t] != LogTag::__NO_TAG; t++) { 75 out->print("%s%s", (t == 0 ? "" : "+"), LogTag::name(_tags[i][t])); 76 } 77 if (_allow_other_tags[i]) { 78 out->print("*"); 79 } 80 out->print(" "); 81 } 82 } 83 84 if (!valid && out != NULL) { 85 out->cr(); 86 } 87 88 return valid; 89} 90 91bool LogTagLevelExpression::parse(const char* str, outputStream* errstream) { 92 bool success = true; 93 if (str == NULL || strcmp(str, "") == 0) { 94 str = DefaultExpressionString; 95 } 96 char* copy = os::strdup_check_oom(str, mtLogging); 97 // Split string on commas 98 for (char *comma_pos = copy, *cur = copy; success && comma_pos != NULL; cur = comma_pos + 1) { 99 if (_ncombinations == MaxCombinations) { 100 if (errstream != NULL) { 101 errstream->print_cr("Can not have more than " SIZE_FORMAT " tag combinations in a what-expression.", 102 MaxCombinations); 103 } 104 success = false; 105 break; 106 } 107 108 comma_pos = strchr(cur, ','); 109 if (comma_pos != NULL) { 110 *comma_pos = '\0'; 111 } 112 113 // Parse the level, if specified 114 LogLevelType level = LogLevel::Unspecified; 115 char* equals = strchr(cur, '='); 116 if (equals != NULL) { 117 level = LogLevel::from_string(equals + 1); 118 if (level == LogLevel::Invalid) { 119 if (errstream != NULL) { 120 errstream->print_cr("Invalid level '%s' in what-expression.", equals + 1); 121 } 122 success = false; 123 break; 124 } 125 *equals = '\0'; // now ignore "=level" part of substr 126 } 127 set_level(level); 128 129 // Parse special tags such as 'all' 130 if (strcmp(cur, "all") == 0) { 131 set_allow_other_tags(); 132 new_combination(); 133 continue; 134 } 135 136 // Check for '*' suffix 137 char* asterisk_pos = strchr(cur, '*'); 138 if (asterisk_pos != NULL && asterisk_pos[1] == '\0') { 139 set_allow_other_tags(); 140 *asterisk_pos = '\0'; 141 } 142 143 // Parse the tag expression (t1+t2+...+tn) 144 char* plus_pos; 145 char* cur_tag = cur; 146 do { 147 plus_pos = strchr(cur_tag, '+'); 148 if (plus_pos != NULL) { 149 *plus_pos = '\0'; 150 } 151 LogTagType tag = LogTag::from_string(cur_tag); 152 if (tag == LogTag::__NO_TAG) { 153 if (errstream != NULL) { 154 errstream->print_cr("Invalid tag '%s' in what-expression.", cur_tag); 155 } 156 success = false; 157 break; 158 } 159 if (_ntags == LogTag::MaxTags) { 160 if (errstream != NULL) { 161 errstream->print_cr("Tag combination exceeds the maximum of " SIZE_FORMAT " tags.", 162 LogTag::MaxTags); 163 } 164 success = false; 165 break; 166 } 167 if (!add_tag(tag)) { 168 if (errstream != NULL) { 169 errstream->print_cr("Tag combination have duplicate tag '%s' in what-expression.", 170 cur_tag); 171 } 172 success = false; 173 break; 174 } 175 cur_tag = plus_pos + 1; 176 } while (plus_pos != NULL); 177 178 new_combination(); 179 } 180 181 os::free(copy); 182 return success; 183} 184 185LogLevelType LogTagLevelExpression::level_for(const LogTagSet& ts) const { 186 // Return NotMentioned if the given tagset isn't covered by this expression. 187 LogLevelType level = LogLevel::NotMentioned; 188 for (size_t combination = 0; combination < _ncombinations; combination++) { 189 if (matches_tagset(_tags[combination], _allow_other_tags[combination], ts)) { 190 level = _level[combination]; 191 } 192 } 193 return level; 194} 195 196