1 /* 2 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 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. 8 * 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). 14 * 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. 18 * 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 21 * questions. 22 * 23 */ 24 #ifndef SHARE_LOGGING_LOGMESSAGEBUFFER_HPP 25 #define SHARE_LOGGING_LOGMESSAGEBUFFER_HPP 26 27 #include "logging/logDecorations.hpp" 28 #include "logging/logLevel.hpp" 29 #include "memory/allocation.hpp" 30 31 class LogMessageBuffer : public CHeapObj<mtLogging> { 32 friend class LogMessageTest_nwrite_test_vm_Test; 33 34 protected: 35 struct LogLine { 36 LogLevelType level; 37 size_t message_offset; 38 }; 39 static const size_t InitialLineCapacity = 10; 40 static const size_t InitialMessageBufferCapacity = 1024; 41 42 Arena* _arena; 43 size_t _message_buffer_size; 44 size_t _message_buffer_capacity; 45 char* _message_buffer; 46 47 size_t _line_count; 48 size_t _line_capacity; 49 LogLine* _lines; 50 51 bool _allocated; 52 LogLevelType _least_detailed_level; 53 size_t (*_prefix_fn)(char*, size_t); 54 55 void initialize_buffers(); 56 template<typename T> 57 void grow(T*& buffer, size_t& capacity, size_t minimum_length = 0); 58 59 private: 60 NONCOPYABLE(LogMessageBuffer); 61 62 public: 63 LogMessageBuffer(Arena* arena = NULL); 64 ~LogMessageBuffer(); 65 66 static size_t initial_buffer_size(); 67 68 class Iterator { 69 private: 70 const LogMessageBuffer& _message; 71 size_t _current_line_index; 72 LogLevelType _level; 73 LogDecorations &_decorations; 74 75 void skip_messages_with_finer_level(); 76 77 public: 78 Iterator(const LogMessageBuffer& message, LogLevelType level, LogDecorations& decorations) 79 : _message(message), _current_line_index(0), _level(level), _decorations(decorations) { 80 skip_messages_with_finer_level(); 81 } 82 83 void operator++(int) { 84 _current_line_index++; 85 skip_messages_with_finer_level(); 86 } 87 88 bool is_at_end() { 89 return _current_line_index == _message._line_count; 90 } 91 92 const char* message() const { 93 return _message._message_buffer + _message._lines[_current_line_index].message_offset; 94 } 95 96 const LogDecorations& decorations() { 97 _decorations.set_level(_message._lines[_current_line_index].level); 98 return _decorations; 99 } 100 }; 101 102 void reset(); 103 104 Arena* arena() const { 105 return _arena; 106 } 107 108 LogLevelType least_detailed_level() const { 109 return _least_detailed_level; 110 } 111 112 Iterator iterator(LogLevelType level, LogDecorations& decorations) const { 113 return Iterator(*this, level, decorations); 114 } 115 116 // Lines in LogMessageBuffers are not automatically prefixed based on tags 117 // like regular simple messages (see LogPrefix.hpp for more about prefixes). 118 // It is, however, possible to specify a prefix per LogMessageBuffer, 119 // using set_prefix(). Lines added to the LogMessageBuffer after a prefix 120 // function has been set will be prefixed automatically. 121 // Setting this to NULL will disable prefixing. 122 void set_prefix(size_t (*prefix_fn)(char*, size_t)) { 123 _prefix_fn = prefix_fn; 124 } 125 126 void write_n(LogLevelType level, const char * s, size_t written); 127 128 ATTRIBUTE_PRINTF(3, 4) 129 void write(LogLevelType level, const char* fmt, ...); 130 131 ATTRIBUTE_PRINTF(3, 0) 132 virtual void vwrite(LogLevelType level, const char* fmt, va_list args); 133 134 #define LOG_LEVEL(level, name) \ 135 LogMessageBuffer& v##name(const char* fmt, va_list args) ATTRIBUTE_PRINTF(2, 0); \ 136 LogMessageBuffer& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3); 137 LOG_LEVEL_LIST 138 #undef LOG_LEVEL 139 }; 140 141 #endif // SHARE_LOGGING_LOGMESSAGEBUFFER_HPP