Path: blob/master/src/hotspot/share/logging/logMessageBuffer.cpp
40930 views
/*1* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/23#include "precompiled.hpp"24#include "logging/logMessageBuffer.hpp"25#include "memory/allocation.inline.hpp"26#include "runtime/thread.inline.hpp"2728template <typename T>29static void grow(T*& buffer, size_t& capacity, size_t minimum_length = 0) {30size_t new_size = capacity * 2;31if (new_size < minimum_length) {32new_size = minimum_length;33}34buffer = REALLOC_C_HEAP_ARRAY(T, buffer, new_size, mtLogging);35capacity = new_size;36}3738LogMessageBuffer::LogMessageBuffer() : _message_buffer_size(0),39_message_buffer_capacity(0),40_message_buffer(NULL),41_line_count(0),42_line_capacity(0),43_lines(NULL),44_allocated(false),45_least_detailed_level(LogLevel::Off),46_prefix_fn(NULL) {47}4849LogMessageBuffer::~LogMessageBuffer() {50if (_allocated) {51FREE_C_HEAP_ARRAY(char, _message_buffer);52FREE_C_HEAP_ARRAY(LogLine, _lines);53}54}5556void LogMessageBuffer::reset() {57_message_buffer_size = 0;58_line_count = 0;59}6061void LogMessageBuffer::initialize_buffers() {62assert(!_allocated, "buffer already initialized/allocated");63_allocated = true;64_message_buffer = NEW_C_HEAP_ARRAY(char, InitialMessageBufferCapacity, mtLogging);65_lines = NEW_C_HEAP_ARRAY(LogLine, InitialLineCapacity, mtLogging);66_message_buffer_capacity = InitialMessageBufferCapacity;67_line_capacity = InitialLineCapacity;68}6970void LogMessageBuffer::Iterator::skip_messages_with_finer_level() {71for (; _current_line_index < _message._line_count; _current_line_index++) {72if (_message._lines[_current_line_index].level >= _level) {73break;74}75}76}7778void LogMessageBuffer::write(LogLevelType level, const char* fmt, ...) {79va_list args;80va_start(args, fmt);81vwrite(level, fmt, args);82va_end(args);83};8485void LogMessageBuffer::vwrite(LogLevelType level, const char* fmt, va_list args) {86if (!_allocated) {87initialize_buffers();88}8990if (level > _least_detailed_level) {91_least_detailed_level = level;92}9394size_t written;95for (int attempts = 0; attempts < 2; attempts++) {96written = 0;97size_t remaining_buffer_length = _message_buffer_capacity - _message_buffer_size;98char* current_buffer_position = _message_buffer + _message_buffer_size;99100if (_prefix_fn != NULL) {101written += _prefix_fn(current_buffer_position, remaining_buffer_length);102current_buffer_position += written;103if (remaining_buffer_length < written) {104remaining_buffer_length = 0;105} else {106remaining_buffer_length -= written;107}108}109110va_list copy;111va_copy(copy, args);112written += (size_t)os::vsnprintf(current_buffer_position, remaining_buffer_length, fmt, copy) + 1;113va_end(copy);114if (written > _message_buffer_capacity - _message_buffer_size) {115assert(attempts == 0, "Second attempt should always have a sufficiently large buffer (resized to fit).");116grow(_message_buffer, _message_buffer_capacity, _message_buffer_size + written);117continue;118}119break;120}121122if (_line_count == _line_capacity) {123grow(_lines, _line_capacity);124}125126_lines[_line_count].level = level;127_lines[_line_count].message_offset = _message_buffer_size;128_message_buffer_size += written;129_line_count++;130}131132#define LOG_LEVEL(level, name) \133LogMessageBuffer& LogMessageBuffer::v##name(const char* fmt, va_list args) { \134vwrite(LogLevel::level, fmt, args); \135return *this; \136} \137LogMessageBuffer& LogMessageBuffer::name(const char* fmt, ...) { \138va_list args; \139va_start(args, fmt); \140vwrite(LogLevel::level, fmt, args); \141va_end(args); \142return *this; \143}144LOG_LEVEL_LIST145#undef LOG_LEVEL146147148