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/logLevel.hpp"
26 #include "utilities/globalDefinitions.hpp"
27 #include "utilities/stringUtils.hpp"
28
29 const char* LogLevel::_name[] = {
30 "off",
31 #define LOG_LEVEL(name, printname) #printname,
32 LOG_LEVEL_LIST
33 #undef LOG_LEVEL
34 };
35
from_string(const char * str)36 LogLevelType LogLevel::from_string(const char* str) {
37 for (uint i = 0; i < Count; i++) {
38 if (strcasecmp(str, _name[i]) == 0) {
39 return static_cast<LogLevelType>(i);
40 }
41 }
42 return Invalid;
43 }
44
fuzzy_match(const char * level)45 LogLevelType LogLevel::fuzzy_match(const char *level) {
46 size_t len = strlen(level);
47 LogLevelType match = LogLevel::Invalid;
48 double best = 0.4; // required similarity to be considered a match
49 for (uint i = 1; i < Count; i++) {
50 LogLevelType cur = static_cast<LogLevelType>(i);
51 const char* levelname = LogLevel::name(cur);
52 double score = StringUtils::similarity(level, len, levelname, strlen(levelname));
53 if (score >= best) {
54 match = cur;
55 best= score;
56 }
57 }
58 return match;
59 }
60