hotspot/src/share/vm/logging/logTagSet.cpp
author rehn
Mon, 04 Apr 2016 15:05:52 +0200
changeset 37247 bdbfe42632cb
parent 37241 b9961c99c356
child 37464 5373bbadb433
permissions -rw-r--r--
8153254: Delegate (v)write from Log to LogTagSet Reviewed-by: stefank, kbarrett
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     1
/*
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
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/logDecorations.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    26
#include "logging/logLevel.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    27
#include "logging/logOutput.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    28
#include "logging/logTag.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    29
#include "logging/logTagSet.hpp"
37247
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
    30
#include "memory/allocation.inline.hpp"
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    31
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    32
LogTagSet*  LogTagSet::_list      = NULL;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    33
size_t      LogTagSet::_ntagsets  = 0;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    34
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    35
// This constructor is called only during static initialization.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    36
// See the declaration in logTagSet.hpp for more information.
37247
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
    37
LogTagSet::LogTagSet(PrefixWriter prefix_writer, LogTagType t0, LogTagType t1, LogTagType t2, LogTagType t3, LogTagType t4)
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
    38
    : _next(_list), _write_prefix(prefix_writer) {
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    39
  _tag[0] = t0;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    40
  _tag[1] = t1;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    41
  _tag[2] = t2;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    42
  _tag[3] = t3;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    43
  _tag[4] = t4;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    44
  for (_ntags = 0; _ntags < LogTag::MaxTags && _tag[_ntags] != LogTag::__NO_TAG; _ntags++) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    45
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    46
  _list = this;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    47
  _ntagsets++;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    48
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    49
  // Set the default output to warning and error level for all new tagsets.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    50
  _output_list.set_output_level(LogOutput::Stderr, LogLevel::Default);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    51
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    52
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    53
void LogTagSet::update_decorators(const LogDecorators& decorator) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    54
  LogDecorators new_decorators = decorator;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    55
  for (LogOutputList::Iterator it = _output_list.iterator(); it != _output_list.end(); it++) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    56
    new_decorators.combine_with((*it)->decorators());
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    57
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    58
  _decorators = new_decorators;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    59
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    60
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    61
bool LogTagSet::has_output(const LogOutput* output) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    62
  for (LogOutputList::Iterator it = _output_list.iterator(); it != _output_list.end(); it++) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    63
    if (*it == output) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    64
      return true;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    65
    }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    66
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    67
  return false;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    68
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    69
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    70
void LogTagSet::log(LogLevelType level, const char* msg) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    71
  LogDecorations decorations(level, *this, _decorators);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    72
  for (LogOutputList::Iterator it = _output_list.iterator(level); it != _output_list.end(); it++) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    73
    (*it)->write(decorations, msg);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    74
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    75
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    76
34316
4d876653d940 8142952: Unified Logging framework does not allow multiple -Xlog: arguments.
mlarsson
parents: 33097
diff changeset
    77
int LogTagSet::label(char* buf, size_t len, const char* separator) const {
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    78
  int tot_written = 0;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    79
  for (size_t i = 0; i < _ntags; i++) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    80
    int written = jio_snprintf(buf + tot_written, len - tot_written, "%s%s",
34316
4d876653d940 8142952: Unified Logging framework does not allow multiple -Xlog: arguments.
mlarsson
parents: 33097
diff changeset
    81
                               (i == 0 ? "" : separator),
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    82
                               LogTag::name(_tag[i]));
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    83
    if (written < 0) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    84
      return -1;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    85
    }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    86
    tot_written += written;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    87
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    88
  return tot_written;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    89
}
37247
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
    90
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
    91
void LogTagSet::write(LogLevelType level, const char* fmt, ...) {
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
    92
  va_list args;
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
    93
  va_start(args, fmt);
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
    94
  vwrite(level, fmt, args);
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
    95
  va_end(args);
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
    96
}
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
    97
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
    98
const size_t vwrite_buffer_size = 512;
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
    99
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   100
void LogTagSet::vwrite(LogLevelType level, const char* fmt, va_list args) {
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   101
  char buf[vwrite_buffer_size];
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   102
  va_list saved_args;           // For re-format on buf overflow.
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   103
  va_copy(saved_args, args);
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   104
  size_t prefix_len = _write_prefix(buf, sizeof(buf));
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   105
  // Check that string fits in buffer; resize buffer if necessary
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   106
  int ret = os::log_vsnprintf(buf + prefix_len, sizeof(buf) - prefix_len, fmt, args);
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   107
  assert(ret >= 0, "Log message buffer issue");
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   108
  if ((size_t)ret >= sizeof(buf)) {
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   109
    size_t newbuf_len = prefix_len + ret + 1;
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   110
    char* newbuf = NEW_C_HEAP_ARRAY(char, newbuf_len, mtLogging);
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   111
    memcpy(newbuf, buf, prefix_len);
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   112
    ret = os::log_vsnprintf(newbuf + prefix_len, newbuf_len - prefix_len, fmt, saved_args);
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   113
    assert(ret >= 0, "Log message buffer issue");
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   114
    log(level, newbuf);
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   115
    FREE_C_HEAP_ARRAY(char, newbuf);
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   116
  } else {
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   117
    log(level, buf);
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   118
  }
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   119
  va_end(saved_args);
bdbfe42632cb 8153254: Delegate (v)write from Log to LogTagSet
rehn
parents: 37241
diff changeset
   120
}