author | naoto |
Tue, 09 Jul 2019 08:05:38 -0700 | |
changeset 55627 | 9c1885fb2a42 |
parent 49163 | 580bb0b85f63 |
permissions | -rw-r--r-- |
33097 | 1 |
/* |
49015 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
33097 | 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" |
|
49163
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
26 |
#include "utilities/stringUtils.hpp" |
33097 | 27 |
#include "utilities/globalDefinitions.hpp" |
49015 | 28 |
#include "utilities/ostream.hpp" |
29 |
#include "utilities/quickSort.hpp" |
|
33097 | 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 |
} |
|
49015 | 46 |
|
49163
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
47 |
LogTagType LogTag::fuzzy_match(const char *str) { |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
48 |
size_t len = strlen(str); |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
49 |
LogTagType match = LogTag::__NO_TAG; |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
50 |
double best = 0.5; // required similarity to be considered a match |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
51 |
for (size_t i = 1; i < LogTag::Count; i++) { |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
52 |
LogTagType tag = static_cast<LogTagType>(i); |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
53 |
const char* tagname = LogTag::name(tag); |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
54 |
double score = StringUtils::similarity(tagname, strlen(tagname), str, len); |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
55 |
if (score >= best) { |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
56 |
match = tag; |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
57 |
best = score; |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
58 |
} |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
59 |
} |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
60 |
return match; |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
61 |
} |
580bb0b85f63
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog
mlarsson
parents:
49015
diff
changeset
|
62 |
|
49015 | 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 |
} |