src/hotspot/share/logging/logOutput.cpp
author mlarsson
Wed, 28 Feb 2018 22:38:53 +0100
changeset 49188 e9ba8b40ca6f
parent 49180 9637557def32
child 49190 c14c75375fa4
permissions -rw-r--r--
8168722: Unified Logging configuration output needs simplifying Reviewed-by: rehn, lfoltan, hseigel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     1
/*
49180
9637557def32 8190346: improve unified JVM logging help message and warnings
mlarsson
parents: 47765
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"
47765
b7c7428eaab9 8189610: Reconcile jvm.h and all jvm_md.h between java.base and hotspot
coleenp
parents: 47216
diff changeset
    25
#include "jvm.h"
49188
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    26
#include "logging/log.hpp"
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    27
#include "logging/logFileStreamOutput.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    28
#include "logging/logOutput.hpp"
49188
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    29
#include "logging/logSelection.hpp"
34316
4d876653d940 8142952: Unified Logging framework does not allow multiple -Xlog: arguments.
mlarsson
parents: 33097
diff changeset
    30
#include "logging/logTagSet.hpp"
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    31
#include "memory/allocation.inline.hpp"
34316
4d876653d940 8142952: Unified Logging framework does not allow multiple -Xlog: arguments.
mlarsson
parents: 33097
diff changeset
    32
#include "runtime/mutexLocker.hpp"
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    33
#include "runtime/os.inline.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    34
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    35
LogOutput::~LogOutput() {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    36
  os::free(_config_string);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    37
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    38
39707
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 37239
diff changeset
    39
void LogOutput::describe(outputStream *out) {
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 37239
diff changeset
    40
  out->print("%s ", name());
49180
9637557def32 8190346: improve unified JVM logging help message and warnings
mlarsson
parents: 47765
diff changeset
    41
  out->print_raw(config_string()); // raw printed because length might exceed O_BUFLEN
9637557def32 8190346: improve unified JVM logging help message and warnings
mlarsson
parents: 47765
diff changeset
    42
9637557def32 8190346: improve unified JVM logging help message and warnings
mlarsson
parents: 47765
diff changeset
    43
  bool has_decorator = false;
9637557def32 8190346: improve unified JVM logging help message and warnings
mlarsson
parents: 47765
diff changeset
    44
  char delimiter = ' ';
39707
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 37239
diff changeset
    45
  for (size_t d = 0; d < LogDecorators::Count; d++) {
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 37239
diff changeset
    46
    LogDecorators::Decorator decorator = static_cast<LogDecorators::Decorator>(d);
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 37239
diff changeset
    47
    if (decorators().is_decorator(decorator)) {
49180
9637557def32 8190346: improve unified JVM logging help message and warnings
mlarsson
parents: 47765
diff changeset
    48
      has_decorator = true;
9637557def32 8190346: improve unified JVM logging help message and warnings
mlarsson
parents: 47765
diff changeset
    49
      out->print("%c%s", delimiter, LogDecorators::name(decorator));
9637557def32 8190346: improve unified JVM logging help message and warnings
mlarsson
parents: 47765
diff changeset
    50
      delimiter = ',';
39707
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 37239
diff changeset
    51
    }
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 37239
diff changeset
    52
  }
49180
9637557def32 8190346: improve unified JVM logging help message and warnings
mlarsson
parents: 47765
diff changeset
    53
  if (!has_decorator) {
9637557def32 8190346: improve unified JVM logging help message and warnings
mlarsson
parents: 47765
diff changeset
    54
    out->print(" none");
9637557def32 8190346: improve unified JVM logging help message and warnings
mlarsson
parents: 47765
diff changeset
    55
  }
39707
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 37239
diff changeset
    56
}
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 37239
diff changeset
    57
49188
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    58
void LogOutput::set_config_string(const char* string) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    59
  os::free(_config_string);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    60
  _config_string = os::strdup(string, mtLogging);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    61
  _config_string_buffer_size = strlen(_config_string) + 1;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    62
}
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    63
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    64
void LogOutput::add_to_config_string(const LogSelection& selection) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    65
  if (_config_string_buffer_size < InitialConfigBufferSize) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    66
    _config_string_buffer_size = InitialConfigBufferSize;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    67
    _config_string = REALLOC_C_HEAP_ARRAY(char, _config_string, _config_string_buffer_size, mtLogging);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    68
  }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    69
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    70
  size_t offset = strlen(_config_string);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    71
  if (offset > 0) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    72
    // Add commas in-between tag and level combinations in the config string
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    73
    _config_string[offset++] = ',';
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    74
  }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    75
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    76
  for (;;) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    77
    int ret = selection.describe(_config_string + offset,
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    78
                                 _config_string_buffer_size - offset);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    79
    if (ret == -1) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    80
      // Double the buffer size and retry
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    81
      _config_string_buffer_size *= 2;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    82
      _config_string = REALLOC_C_HEAP_ARRAY(char, _config_string, _config_string_buffer_size, mtLogging);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    83
      continue;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    84
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    85
    break;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    86
  };
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    87
}
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    88
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    89
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    90
static int tag_cmp(const void *a, const void *b) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    91
  return static_cast<const LogTagType*>(a) - static_cast<const LogTagType*>(b);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    92
}
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    93
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    94
static void sort_tags(LogTagType tags[LogTag::MaxTags]) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    95
  size_t ntags = 0;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    96
  while (tags[ntags] != LogTag::__NO_TAG) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    97
    ntags++;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    98
  }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
    99
  qsort(tags, ntags, sizeof(*tags), tag_cmp);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   100
}
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   101
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   102
static const size_t MaxSubsets = 1 << LogTag::MaxTags;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   103
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   104
// Fill result with all possible subsets of the given tag set. Empty set not included.
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   105
// For example, if tags is {gc, heap} then the result is {{gc}, {heap}, {gc, heap}}.
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   106
// (Arguments with default values are intended exclusively for recursive calls.)
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   107
static void generate_all_subsets_of(LogTagType result[MaxSubsets][LogTag::MaxTags],
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   108
                                    size_t* result_size,
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   109
                                    const LogTagType tags[LogTag::MaxTags],
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   110
                                    LogTagType subset[LogTag::MaxTags] = NULL,
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   111
                                    const size_t subset_size = 0,
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   112
                                    const size_t depth = 0) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   113
  assert(subset_size <= LogTag::MaxTags, "subset must never have more than MaxTags tags");
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   114
  assert(depth <= LogTag::MaxTags, "recursion depth overflow");
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   115
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   116
  if (subset == NULL) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   117
    assert(*result_size == 0, "outer (non-recursive) call expects result_size to be 0");
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   118
    // Make subset the first element in the result array initially
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   119
    subset = result[0];
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   120
  }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   121
  assert((void*) subset >= result && (void*) subset < result + sizeof(result),
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   122
         "subset should always point to element in result");
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   123
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   124
  if (depth == LogTag::MaxTags || tags[depth] == LogTag::__NO_TAG) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   125
    if (subset_size == 0) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   126
      // Ignore empty subset
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   127
      return;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   128
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   129
    if (subset_size != LogTag::MaxTags) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   130
      subset[subset_size] = LogTag::__NO_TAG;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   131
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   132
    assert(*result_size < MaxSubsets, "subsets overflow");
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   133
    *result_size += 1;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   134
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   135
    // Bump subset and copy over current state
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   136
    memcpy(result[*result_size], subset, sizeof(*subset) * LogTag::MaxTags);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   137
    subset = result[*result_size];
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   138
    return;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   139
  }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   140
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   141
  // Recurse, excluding the tag of the current depth
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   142
  generate_all_subsets_of(result, result_size, tags, subset, subset_size, depth + 1);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   143
  // ... and with it included
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   144
  subset[subset_size] = tags[depth];
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   145
  generate_all_subsets_of(result, result_size, tags, subset, subset_size + 1, depth + 1);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   146
}
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   147
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   148
// Generate all possible selections (for the given level) based on the given tag set,
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   149
// and add them to the selections array (growing it as necessary).
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   150
static void add_selections(LogSelection** selections,
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   151
                           size_t* n_selections,
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   152
                           size_t* selections_cap,
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   153
                           const LogTagSet& tagset,
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   154
                           LogLevelType level) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   155
  LogTagType tags[LogTag::MaxTags] = { LogTag::__NO_TAG };
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   156
  for (size_t i = 0; i < tagset.ntags(); i++) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   157
    tags[i] = tagset.tag(i);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   158
  }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   159
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   160
  size_t n_subsets = 0;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   161
  LogTagType subsets[MaxSubsets][LogTag::MaxTags];
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   162
  generate_all_subsets_of(subsets, &n_subsets, tags);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   163
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   164
  for (size_t i = 0; i < n_subsets; i++) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   165
    // Always keep tags sorted
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   166
    sort_tags(subsets[i]);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   167
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   168
    // Ignore subsets already represented in selections
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   169
    bool unique = true;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   170
    for (size_t sel = 0; sel < *n_selections; sel++) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   171
      if (level == (*selections)[sel].level() && (*selections)[sel].consists_of(subsets[i])) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   172
        unique = false;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   173
        break;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   174
      }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   175
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   176
    if (!unique) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   177
      continue;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   178
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   179
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   180
    LogSelection exact_selection(subsets[i], false, level);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   181
    LogSelection wildcard_selection(subsets[i], true, level);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   182
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   183
    // Check if the two selections match any tag sets
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   184
    bool wildcard_match = false;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   185
    bool exact_match = false;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   186
    for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   187
      if (!wildcard_selection.selects(*ts)) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   188
        continue;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   189
      }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   190
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   191
      wildcard_match = true;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   192
      if (exact_selection.selects(*ts)) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   193
        exact_match = true;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   194
      }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   195
      if (exact_match) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   196
        break;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   197
      }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   198
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   199
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   200
    if (!wildcard_match && !exact_match) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   201
      continue;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   202
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   203
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   204
    // Ensure there's enough room for both wildcard_match and exact_match
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   205
    if (*n_selections + 2 > *selections_cap) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   206
      *selections_cap *= 2;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   207
      *selections = REALLOC_C_HEAP_ARRAY(LogSelection, *selections, *selections_cap, mtLogging);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   208
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   209
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   210
    // Add found matching selections to the result array
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   211
    if (exact_match) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   212
      (*selections)[(*n_selections)++] = exact_selection;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   213
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   214
    if (wildcard_match) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   215
      (*selections)[(*n_selections)++] = wildcard_selection;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   216
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   217
  }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   218
}
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   219
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   220
void LogOutput::update_config_string(const size_t on_level[LogLevel::Count]) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   221
  // Find the most common level (MCL)
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   222
  LogLevelType mcl = LogLevel::Off;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   223
  size_t max = on_level[LogLevel::Off];
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   224
  for (LogLevelType l = LogLevel::First; l <= LogLevel::Last; l = static_cast<LogLevelType>(l + 1)) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   225
     if (on_level[l] > max) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   226
       mcl = l;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   227
       max = on_level[l];
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   228
     }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   229
  }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   230
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   231
  // Always let the first part of each output's config string be "all=<MCL>"
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   232
  {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   233
    char buf[64];
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   234
    jio_snprintf(buf, sizeof(buf), "all=%s", LogLevel::name(mcl));
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   235
    set_config_string(buf);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   236
  }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   237
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   238
  // If there are no deviating tag sets, we're done
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   239
  size_t deviating_tagsets = LogTagSet::ntagsets() - max;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   240
  if (deviating_tagsets == 0) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   241
    return;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   242
  }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   243
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   244
  size_t n_selections = 0;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   245
  size_t selections_cap = 4 * MaxSubsets; // Start with some reasonably large initial capacity
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   246
  LogSelection* selections = NEW_C_HEAP_ARRAY(LogSelection, selections_cap, mtLogging);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   247
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   248
  size_t n_deviates = 0;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   249
  const LogTagSet** deviates = NEW_C_HEAP_ARRAY(const LogTagSet*, deviating_tagsets, mtLogging);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   250
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   251
  // Generate all possible selections involving the deviating tag sets
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   252
  for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   253
    LogLevelType level = ts->level_for(this);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   254
    if (level == mcl) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   255
      continue;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   256
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   257
    deviates[n_deviates++] = ts;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   258
    add_selections(&selections, &n_selections, &selections_cap, *ts, level);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   259
  }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   260
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   261
  // Reduce deviates greedily, using the "best" selection at each step to reduce the number of deviating tag sets
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   262
  while (n_deviates > 0) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   263
    size_t prev_deviates = n_deviates;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   264
    int max_score = 0;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   265
    const LogSelection* best_selection = NULL;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   266
    for (size_t i = 0; i < n_selections; i++) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   267
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   268
      // Give the selection a score based on how many deviating tag sets it selects (with correct level)
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   269
      int score = 0;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   270
      for (size_t d = 0; d < n_deviates; d++) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   271
        if (selections[i].selects(*deviates[d]) && deviates[d]->level_for(this) == selections[i].level()) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   272
          score++;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   273
        }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   274
      }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   275
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   276
      // Ignore selections with lower score than the current best even before subtracting mismatched selections
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   277
      if (score < max_score) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   278
        continue;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   279
      }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   280
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   281
      // Subtract from the score the number of tag sets it selects with an incorrect level
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   282
      for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   283
        if (selections[i].selects(*ts) && ts->level_for(this) != selections[i].level()) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   284
          score--;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   285
        }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   286
      }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   287
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   288
      // Pick the selection with the best score, or in the case of a tie, the one with fewest tags
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   289
      if (score > max_score ||
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   290
          (score == max_score && best_selection != NULL && selections[i].ntags() < best_selection->ntags())) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   291
        max_score = score;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   292
        best_selection = &selections[i];
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   293
      }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   294
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   295
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   296
    assert(best_selection != NULL, "must always find a maximal selection");
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   297
    add_to_config_string(*best_selection);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   298
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   299
    // Remove all deviates that this selection covered
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   300
    for (size_t d = 0; d < n_deviates;) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   301
      if (deviates[d]->level_for(this) == best_selection->level() && best_selection->selects(*deviates[d])) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   302
        deviates[d] = deviates[--n_deviates];
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   303
        continue;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   304
      }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   305
      d++;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   306
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   307
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   308
    // Add back any new deviates that this selection added (no array growth since removed > added)
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   309
    for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   310
      if (ts->level_for(this) == best_selection->level() || !best_selection->selects(*ts)) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   311
        continue;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   312
      }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   313
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   314
      bool already_added = false;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   315
      for (size_t dev = 0; dev < n_deviates; dev++) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   316
        if (deviates[dev] == ts) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   317
          already_added = true;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   318
          break;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   319
        }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   320
      }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   321
      if (already_added) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   322
        continue;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   323
      }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   324
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   325
      deviates[n_deviates++] = ts;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   326
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   327
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   328
    // Reset the selections and generate a new ones based on the updated deviating tag sets
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   329
    n_selections = 0;
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   330
    for (size_t d = 0; d < n_deviates; d++) {
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   331
      add_selections(&selections, &n_selections, &selections_cap, *deviates[d], deviates[d]->level_for(this));
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   332
    }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   333
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   334
    assert(n_deviates < deviating_tagsets, "deviating tag set array overflow");
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   335
    assert(prev_deviates > n_deviates, "number of deviating tag sets must never grow");
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   336
  }
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   337
  FREE_C_HEAP_ARRAY(LogTagSet*, deviates);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   338
  FREE_C_HEAP_ARRAY(Selection, selections);
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   339
}
e9ba8b40ca6f 8168722: Unified Logging configuration output needs simplifying
mlarsson
parents: 49180
diff changeset
   340