src/hotspot/share/logging/logMessageBuffer.cpp
author naoto
Tue, 09 Jul 2019 08:05:38 -0700
changeset 55627 9c1885fb2a42
parent 49177 eebf559c9e0d
permissions -rw-r--r--
8227127: Era designator not displayed correctly using the COMPAT provider Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38263
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     1
/*
49177
eebf559c9e0d 8196882: VS2017 Hotspot Defined vsnprintf Function Causes C2084 Already Defined Compilation Error
kbarrett
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
38263
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     4
 *
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     8
 *
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    13
 * accompanied this code).
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    14
 *
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    18
 *
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    21
 * questions.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    22
 *
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    23
 */
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    24
#include "precompiled.hpp"
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    25
#include "logging/logMessageBuffer.hpp"
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    26
#include "memory/allocation.inline.hpp"
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    27
#include "runtime/thread.inline.hpp"
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    28
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    29
template <typename T>
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    30
static void grow(T*& buffer, size_t& capacity, size_t minimum_length = 0) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    31
  size_t new_size = capacity * 2;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    32
  if (new_size < minimum_length) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    33
    new_size = minimum_length;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    34
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    35
  buffer = REALLOC_C_HEAP_ARRAY(T, buffer, new_size, mtLogging);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    36
  capacity = new_size;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    37
}
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    38
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    39
LogMessageBuffer::LogMessageBuffer() : _message_buffer_size(0),
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    40
                                       _message_buffer_capacity(0),
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    41
                                       _message_buffer(NULL),
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    42
                                       _line_count(0),
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    43
                                       _line_capacity(0),
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    44
                                       _lines(NULL),
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    45
                                       _allocated(false),
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    46
                                       _least_detailed_level(LogLevel::Off),
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    47
                                       _prefix_fn(NULL) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    48
}
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    49
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    50
LogMessageBuffer::~LogMessageBuffer() {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    51
  if (_allocated) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    52
    FREE_C_HEAP_ARRAY(char, _message_buffer);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    53
    FREE_C_HEAP_ARRAY(LogLine, _lines);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    54
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    55
}
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    56
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    57
void LogMessageBuffer::reset() {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    58
  _message_buffer_size = 0;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    59
  _line_count = 0;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    60
}
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    61
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    62
void LogMessageBuffer::initialize_buffers() {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    63
  assert(!_allocated, "buffer already initialized/allocated");
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    64
  _allocated = true;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    65
  _message_buffer = NEW_C_HEAP_ARRAY(char, InitialMessageBufferCapacity, mtLogging);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    66
  _lines = NEW_C_HEAP_ARRAY(LogLine, InitialLineCapacity, mtLogging);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    67
  _message_buffer_capacity = InitialMessageBufferCapacity;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    68
  _line_capacity = InitialLineCapacity;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    69
}
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    70
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    71
void LogMessageBuffer::Iterator::skip_messages_with_finer_level() {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    72
  for (; _current_line_index < _message._line_count; _current_line_index++) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    73
    if (_message._lines[_current_line_index].level >= _level) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    74
      break;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    75
    }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    76
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    77
}
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    78
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    79
void LogMessageBuffer::write(LogLevelType level, const char* fmt, ...) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    80
  va_list args;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    81
  va_start(args, fmt);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    82
  vwrite(level, fmt, args);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    83
  va_end(args);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    84
};
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    85
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    86
void LogMessageBuffer::vwrite(LogLevelType level, const char* fmt, va_list args) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    87
  if (!_allocated) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    88
    initialize_buffers();
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    89
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    90
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    91
  if (level > _least_detailed_level) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    92
    _least_detailed_level = level;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    93
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    94
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    95
  size_t written;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    96
  for (int attempts = 0; attempts < 2; attempts++) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    97
    written = 0;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    98
    size_t remaining_buffer_length = _message_buffer_capacity - _message_buffer_size;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    99
    char* current_buffer_position = _message_buffer + _message_buffer_size;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   100
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   101
    if (_prefix_fn != NULL) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   102
      written += _prefix_fn(current_buffer_position, remaining_buffer_length);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   103
      current_buffer_position += written;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   104
      if (remaining_buffer_length < written) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   105
        remaining_buffer_length = 0;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   106
      } else {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   107
        remaining_buffer_length -= written;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   108
      }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   109
    }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   110
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   111
    va_list copy;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   112
    va_copy(copy, args);
49177
eebf559c9e0d 8196882: VS2017 Hotspot Defined vsnprintf Function Causes C2084 Already Defined Compilation Error
kbarrett
parents: 47216
diff changeset
   113
    written += (size_t)os::vsnprintf(current_buffer_position, remaining_buffer_length, fmt, copy) + 1;
38263
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   114
    va_end(copy);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   115
    if (written > _message_buffer_capacity - _message_buffer_size) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   116
      assert(attempts == 0, "Second attempt should always have a sufficiently large buffer (resized to fit).");
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   117
      grow(_message_buffer, _message_buffer_capacity, _message_buffer_size + written);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   118
      continue;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   119
    }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   120
    break;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   121
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   122
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   123
  if (_line_count == _line_capacity) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   124
    grow(_lines, _line_capacity);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   125
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   126
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   127
  _lines[_line_count].level = level;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   128
  _lines[_line_count].message_offset = _message_buffer_size;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   129
  _message_buffer_size += written;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   130
  _line_count++;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   131
}
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   132
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   133
#define LOG_LEVEL(level, name) \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   134
LogMessageBuffer& LogMessageBuffer::v##name(const char* fmt, va_list args) { \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   135
  vwrite(LogLevel::level, fmt, args); \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   136
  return *this; \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   137
} \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   138
LogMessageBuffer& LogMessageBuffer::name(const char* fmt, ...) { \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   139
  va_list args; \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   140
  va_start(args, fmt); \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   141
  vwrite(LogLevel::level, fmt, args); \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   142
  va_end(args); \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   143
  return *this; \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   144
}
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   145
LOG_LEVEL_LIST
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   146
#undef LOG_LEVEL