src/hotspot/share/logging/logTag.cpp
author mlarsson
Mon, 26 Feb 2018 09:34:20 +0100
changeset 49163 580bb0b85f63
parent 49015 a12c9536d8a6
permissions -rw-r--r--
8198554: Add fuzzy matching for log levels and tags when parsing -Xlog Reviewed-by: hseigel, coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     1
/*
49015
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     4
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     7
 * published by the Free Software Foundation.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     8
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    13
 * accompanied this code).
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    14
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    18
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    21
 * questions.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    22
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    23
 */
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    24
#include "precompiled.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    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
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    27
#include "utilities/globalDefinitions.hpp"
49015
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    28
#include "utilities/ostream.hpp"
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    29
#include "utilities/quickSort.hpp"
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    30
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    31
const char* LogTag::_name[] = {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    32
  "", // __NO_TAG
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    33
#define LOG_TAG(name) #name,
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    34
  LOG_TAG_LIST
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    35
#undef LOG_TAG
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    36
};
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    37
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    38
LogTagType LogTag::from_string(const char* str) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    39
  for (uint i = 0; i < LogTag::Count; i++) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    40
    if (strcasecmp(str, _name[i]) == 0) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    41
      return static_cast<LogTagType>(i);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    42
    }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    43
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    44
  return __NO_TAG;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    45
}
49015
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    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
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    63
static int cmp_logtag(LogTagType a, LogTagType b) {
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    64
  return strcmp(LogTag::name(a), LogTag::name(b));
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    65
}
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    66
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    67
static const size_t sorted_tagcount = LogTag::Count - 1; // Not counting _NO_TAG
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    68
static LogTagType sorted_tags[sorted_tagcount];
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    69
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    70
class TagSorter {
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    71
 public:
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    72
  TagSorter() {
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    73
    for (size_t i = 1; i < LogTag::Count; i++) {
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    74
      sorted_tags[i - 1] = static_cast<LogTagType>(i);
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    75
    }
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    76
    QuickSort::sort(sorted_tags, sorted_tagcount, cmp_logtag, true);
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    77
  }
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    78
};
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    79
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    80
static TagSorter tagsorter; // Sorts tags during static initialization
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    81
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    82
void LogTag::list_tags(outputStream* out) {
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    83
  for (size_t i = 0; i < sorted_tagcount; i++) {
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    84
    out->print("%s %s", (i == 0 ? "" : ","), _name[sorted_tags[i]]);
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    85
  }
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    86
  out->cr();
a12c9536d8a6 8176298: Log tags in -Xlog:help not sorted
mlarsson
parents: 47216
diff changeset
    87
}