|
1 /* |
|
2 * Copyright (c) 2016, 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_VM_LOGGING_LOGMESSAGEBUFFER_HPP |
|
25 #define SHARE_VM_LOGGING_LOGMESSAGEBUFFER_HPP |
|
26 |
|
27 #include "logging/logDecorations.hpp" |
|
28 #include "logging/logLevel.hpp" |
|
29 #include "memory/allocation.hpp" |
|
30 |
|
31 class LogMessageBuffer : public StackObj { |
|
32 friend class LogMessageTest; |
|
33 protected: |
|
34 struct LogLine VALUE_OBJ_CLASS_SPEC { |
|
35 LogLevelType level; |
|
36 size_t message_offset; |
|
37 }; |
|
38 static const size_t InitialLineCapacity = 10; |
|
39 static const size_t InitialMessageBufferCapacity = 1024; |
|
40 |
|
41 size_t _message_buffer_size; |
|
42 size_t _message_buffer_capacity; |
|
43 char* _message_buffer; |
|
44 |
|
45 size_t _line_count; |
|
46 size_t _line_capacity; |
|
47 LogLine* _lines; |
|
48 |
|
49 bool _allocated; |
|
50 LogLevelType _least_detailed_level; |
|
51 size_t (*_prefix_fn)(char*, size_t); |
|
52 |
|
53 void initialize_buffers(); |
|
54 |
|
55 private: |
|
56 // Forbid copy assignment and copy constructor. |
|
57 void operator=(const LogMessageBuffer& ref) {} |
|
58 LogMessageBuffer(const LogMessageBuffer& ref) {} |
|
59 |
|
60 public: |
|
61 LogMessageBuffer(); |
|
62 ~LogMessageBuffer(); |
|
63 |
|
64 class Iterator { |
|
65 private: |
|
66 const LogMessageBuffer& _message; |
|
67 size_t _current_line_index; |
|
68 LogLevelType _level; |
|
69 LogDecorations &_decorations; |
|
70 |
|
71 void skip_messages_with_finer_level(); |
|
72 |
|
73 public: |
|
74 Iterator(const LogMessageBuffer& message, LogLevelType level, LogDecorations& decorations) |
|
75 : _message(message), _level(level), _decorations(decorations), _current_line_index(0) { |
|
76 skip_messages_with_finer_level(); |
|
77 } |
|
78 |
|
79 void operator++(int) { |
|
80 _current_line_index++; |
|
81 skip_messages_with_finer_level(); |
|
82 } |
|
83 |
|
84 bool is_at_end() { |
|
85 return _current_line_index == _message._line_count; |
|
86 } |
|
87 |
|
88 const char* message() const { |
|
89 return _message._message_buffer + _message._lines[_current_line_index].message_offset; |
|
90 } |
|
91 |
|
92 const LogDecorations& decorations() { |
|
93 _decorations.set_level(_message._lines[_current_line_index].level); |
|
94 return _decorations; |
|
95 } |
|
96 }; |
|
97 |
|
98 void reset(); |
|
99 |
|
100 LogLevelType least_detailed_level() const { |
|
101 return _least_detailed_level; |
|
102 } |
|
103 |
|
104 Iterator iterator(LogLevelType level, LogDecorations& decorations) const { |
|
105 return Iterator(*this, level, decorations); |
|
106 } |
|
107 |
|
108 // Lines in LogMessageBuffers are not automatically prefixed based on tags |
|
109 // like regular simple messages (see LogPrefix.hpp for more about prefixes). |
|
110 // It is, however, possible to specify a prefix per LogMessageBuffer, |
|
111 // using set_prefix(). Lines added to the LogMessageBuffer after a prefix |
|
112 // function has been set will be prefixed automatically. |
|
113 // Setting this to NULL will disable prefixing. |
|
114 void set_prefix(size_t (*prefix_fn)(char*, size_t)) { |
|
115 _prefix_fn = prefix_fn; |
|
116 } |
|
117 |
|
118 ATTRIBUTE_PRINTF(3, 4) |
|
119 void write(LogLevelType level, const char* fmt, ...); |
|
120 |
|
121 ATTRIBUTE_PRINTF(3, 0) |
|
122 virtual void vwrite(LogLevelType level, const char* fmt, va_list args); |
|
123 |
|
124 #define LOG_LEVEL(level, name) \ |
|
125 LogMessageBuffer& v##name(const char* fmt, va_list args) ATTRIBUTE_PRINTF(2, 0); \ |
|
126 LogMessageBuffer& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3); |
|
127 LOG_LEVEL_LIST |
|
128 #undef LOG_LEVEL |
|
129 }; |
|
130 |
|
131 #endif // SHARE_VM_LOGGING_LOGMESSAGEBUFFER_HPP |