Path: blob/master/src/hotspot/share/logging/logMessageBuffer.hpp
40930 views
/*1* Copyright (c) 2016, 2019, 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#ifndef SHARE_LOGGING_LOGMESSAGEBUFFER_HPP24#define SHARE_LOGGING_LOGMESSAGEBUFFER_HPP2526#include "logging/logDecorations.hpp"27#include "logging/logLevel.hpp"28#include "memory/allocation.hpp"2930class LogMessageBuffer : public StackObj {31friend class LogMessageTest;32protected:33struct LogLine {34LogLevelType level;35size_t message_offset;36};37static const size_t InitialLineCapacity = 10;38static const size_t InitialMessageBufferCapacity = 1024;3940size_t _message_buffer_size;41size_t _message_buffer_capacity;42char* _message_buffer;4344size_t _line_count;45size_t _line_capacity;46LogLine* _lines;4748bool _allocated;49LogLevelType _least_detailed_level;50size_t (*_prefix_fn)(char*, size_t);5152void initialize_buffers();5354private:55// Forbid copy assignment and copy constructor.56void operator=(const LogMessageBuffer& ref) {}57LogMessageBuffer(const LogMessageBuffer& ref) {}5859public:60LogMessageBuffer();61~LogMessageBuffer();6263class Iterator {64private:65const LogMessageBuffer& _message;66size_t _current_line_index;67LogLevelType _level;68LogDecorations &_decorations;6970void skip_messages_with_finer_level();7172public:73Iterator(const LogMessageBuffer& message, LogLevelType level, LogDecorations& decorations)74: _message(message), _current_line_index(0), _level(level), _decorations(decorations) {75skip_messages_with_finer_level();76}7778void operator++(int) {79_current_line_index++;80skip_messages_with_finer_level();81}8283bool is_at_end() {84return _current_line_index == _message._line_count;85}8687const char* message() const {88return _message._message_buffer + _message._lines[_current_line_index].message_offset;89}9091const LogDecorations& decorations() {92_decorations.set_level(_message._lines[_current_line_index].level);93return _decorations;94}95};9697void reset();9899LogLevelType least_detailed_level() const {100return _least_detailed_level;101}102103Iterator iterator(LogLevelType level, LogDecorations& decorations) const {104return Iterator(*this, level, decorations);105}106107// Lines in LogMessageBuffers are not automatically prefixed based on tags108// like regular simple messages (see LogPrefix.hpp for more about prefixes).109// It is, however, possible to specify a prefix per LogMessageBuffer,110// using set_prefix(). Lines added to the LogMessageBuffer after a prefix111// function has been set will be prefixed automatically.112// Setting this to NULL will disable prefixing.113void set_prefix(size_t (*prefix_fn)(char*, size_t)) {114_prefix_fn = prefix_fn;115}116117ATTRIBUTE_PRINTF(3, 4)118void write(LogLevelType level, const char* fmt, ...);119120ATTRIBUTE_PRINTF(3, 0)121virtual void vwrite(LogLevelType level, const char* fmt, va_list args);122123#define LOG_LEVEL(level, name) \124LogMessageBuffer& v##name(const char* fmt, va_list args) ATTRIBUTE_PRINTF(2, 0); \125LogMessageBuffer& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3);126LOG_LEVEL_LIST127#undef LOG_LEVEL128};129130#endif // SHARE_LOGGING_LOGMESSAGEBUFFER_HPP131132133