| 1 | /* |
| 2 | * Copyright (c) 2015, 2018, 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/logTag.hpp" |
| 26 | #include "utilities/stringUtils.hpp" |
| 27 | #include "utilities/globalDefinitions.hpp" |
| 28 | #include "utilities/ostream.hpp" |
| 29 | #include "utilities/quickSort.hpp" |
| 30 | |
| 31 | const char* LogTag::_name[] = { |
| 32 | "" , // __NO_TAG |
| 33 | #define LOG_TAG(name) #name, |
| 34 | LOG_TAG_LIST |
| 35 | #undef LOG_TAG |
| 36 | }; |
| 37 | |
| 38 | LogTagType LogTag::from_string(const char* str) { |
| 39 | for (uint i = 0; i < LogTag::Count; i++) { |
| 40 | if (strcasecmp(str, _name[i]) == 0) { |
| 41 | return static_cast<LogTagType>(i); |
| 42 | } |
| 43 | } |
| 44 | return __NO_TAG; |
| 45 | } |
| 46 | |
| 47 | LogTagType LogTag::fuzzy_match(const char *str) { |
| 48 | size_t len = strlen(str); |
| 49 | LogTagType match = LogTag::__NO_TAG; |
| 50 | double best = 0.5; // required similarity to be considered a match |
| 51 | for (size_t i = 1; i < LogTag::Count; i++) { |
| 52 | LogTagType tag = static_cast<LogTagType>(i); |
| 53 | const char* tagname = LogTag::name(tag); |
| 54 | double score = StringUtils::similarity(tagname, strlen(tagname), str, len); |
| 55 | if (score >= best) { |
| 56 | match = tag; |
| 57 | best = score; |
| 58 | } |
| 59 | } |
| 60 | return match; |
| 61 | } |
| 62 | |
| 63 | static int cmp_logtag(LogTagType a, LogTagType b) { |
| 64 | return strcmp(LogTag::name(a), LogTag::name(b)); |
| 65 | } |
| 66 | |
| 67 | static const size_t sorted_tagcount = LogTag::Count - 1; // Not counting _NO_TAG |
| 68 | static LogTagType sorted_tags[sorted_tagcount]; |
| 69 | |
| 70 | class TagSorter { |
| 71 | public: |
| 72 | TagSorter() { |
| 73 | for (size_t i = 1; i < LogTag::Count; i++) { |
| 74 | sorted_tags[i - 1] = static_cast<LogTagType>(i); |
| 75 | } |
| 76 | QuickSort::sort(sorted_tags, sorted_tagcount, cmp_logtag, true); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | static TagSorter tagsorter; // Sorts tags during static initialization |
| 81 | |
| 82 | void LogTag::list_tags(outputStream* out) { |
| 83 | for (size_t i = 0; i < sorted_tagcount; i++) { |
| 84 | out->print("%s %s" , (i == 0 ? "" : "," ), _name[sorted_tags[i]]); |
| 85 | } |
| 86 | out->cr(); |
| 87 | } |
| 88 | |