2
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
24
#include "precompiled.hpp"
25
#include "logging/logMessageBuffer.hpp"
26
#include "memory/allocation.inline.hpp"
27
#include "runtime/javaThread.hpp"
30
static void grow(T*& buffer, size_t& capacity, size_t minimum_length = 0) {
31
size_t new_size = capacity * 2;
32
if (new_size < minimum_length) {
33
new_size = minimum_length;
35
buffer = REALLOC_C_HEAP_ARRAY(T, buffer, new_size, mtLogging);
39
LogMessageBuffer::LogMessageBuffer() : _message_buffer_size(0),
40
_message_buffer_capacity(0),
41
_message_buffer(nullptr),
46
_least_detailed_level(LogLevel::Off),
50
LogMessageBuffer::~LogMessageBuffer() {
52
FREE_C_HEAP_ARRAY(char, _message_buffer);
53
FREE_C_HEAP_ARRAY(LogLine, _lines);
57
void LogMessageBuffer::reset() {
58
_message_buffer_size = 0;
62
void LogMessageBuffer::initialize_buffers() {
63
assert(!_allocated, "buffer already initialized/allocated");
65
_message_buffer = NEW_C_HEAP_ARRAY(char, InitialMessageBufferCapacity, mtLogging);
66
_lines = NEW_C_HEAP_ARRAY(LogLine, InitialLineCapacity, mtLogging);
67
_message_buffer_capacity = InitialMessageBufferCapacity;
68
_line_capacity = InitialLineCapacity;
71
void LogMessageBuffer::Iterator::skip_messages_with_finer_level() {
72
for (; _current_line_index < _message._line_count; _current_line_index++) {
73
if (_message._lines[_current_line_index].level >= _level) {
79
void LogMessageBuffer::write(LogLevelType level, const char* fmt, ...) {
82
vwrite(level, fmt, args);
86
void LogMessageBuffer::vwrite(LogLevelType level, const char* fmt, va_list args) {
91
if (level > _least_detailed_level) {
92
_least_detailed_level = level;
96
for (int attempts = 0; attempts < 2; attempts++) {
98
size_t remaining_buffer_length = _message_buffer_capacity - _message_buffer_size;
99
char* current_buffer_position = _message_buffer + _message_buffer_size;
101
if (_prefix_fn != nullptr) {
102
written += _prefix_fn(current_buffer_position, remaining_buffer_length);
103
current_buffer_position += written;
104
if (remaining_buffer_length < written) {
105
remaining_buffer_length = 0;
107
remaining_buffer_length -= written;
113
int ret = os::vsnprintf(current_buffer_position, remaining_buffer_length, fmt, copy);
115
assert(ret >= 0, "Log message buffer issue");
117
this->write(level, "%s", "Log message buffer issue");
120
written += (size_t)ret + 1;
121
if (written > _message_buffer_capacity - _message_buffer_size) {
122
assert(attempts == 0, "Second attempt should always have a sufficiently large buffer (resized to fit).");
123
grow(_message_buffer, _message_buffer_capacity, _message_buffer_size + written);
129
if (_line_count == _line_capacity) {
130
grow(_lines, _line_capacity);
133
_lines[_line_count].level = level;
134
_lines[_line_count].message_offset = _message_buffer_size;
135
_message_buffer_size += written;
139
#define LOG_LEVEL(level, name) \
140
LogMessageBuffer& LogMessageBuffer::v##name(const char* fmt, va_list args) { \
141
vwrite(LogLevel::level, fmt, args); \
144
LogMessageBuffer& LogMessageBuffer::name(const char* fmt, ...) { \
146
va_start(args, fmt); \
147
vwrite(LogLevel::level, fmt, args); \