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
25 #ifndef SHARE_LOGGING_LOGSTREAM_HPP
26 #define SHARE_LOGGING_LOGSTREAM_HPP
27
28 #include "logging/log.hpp"
29 #include "logging/logHandle.hpp"
30 #include "memory/resourceArea.hpp"
31 #include "utilities/ostream.hpp"
32
33
34 class LogStream : public outputStream {
35 friend class LogStreamTest_TestLineBufferAllocation_test_vm_Test; // see test/native/logging/test_logStream.cpp
36 friend class LogStreamTest_TestLineBufferAllocationCap_test_vm_Test; // see test/native/logging/test_logStream.cpp
37
38 // Helper class, maintains the line buffer. For small line lengths,
39 // we avoid malloc and use a fixed sized member char array. If LogStream
40 // is allocated on the stack, this means small lines are assembled
41 // directly on the stack.
42 class LineBuffer {
43 char _smallbuf[64];
44 char* _buf;
45 size_t _cap;
46 size_t _pos;
47 void try_ensure_cap(size_t cap);
48 public:
49 LineBuffer();
50 ~LineBuffer();
51 bool is_empty() const { return _pos == 0; }
52 const char* buffer() const { return _buf; }
53 void append(const char* s, size_t len);
54 void reset();
55 };
56 LineBuffer _current_line;
57 LogTargetHandle _log_handle;
58
59 // Prevent operator new for LogStream.
60 static void* operator new (size_t);
61 static void* operator new[] (size_t);
62
63 public:
64 // Constructor to support creation from a LogTarget instance.
65 //
66 // LogTarget(Debug, gc) log;
67 // LogStreamBase(log) stream;
68 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
69 LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
70 _log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
71
72 // Constructor to support creation from typed (likely NULL) pointer. Mostly used by the logging framework.
73 //
74 // LogStreamBase stream(log.debug());
75 // or
76 // LogStreamBase stream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL);
77 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
78 LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>* type_carrier) :
79 _log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
80
81 // Destructor writes any unfinished output left in the line buffer.
82 ~LogStream();
83
84 // Constructor to support creation from a LogTargetHandle.
85 //
86 // LogTarget(Debug, gc) log;
87 // LogTargetHandle(log) handle;
88 // LogStreamBase stream(handle);
89 LogStream(LogTargetHandle handle) : _log_handle(handle) {}
90
91 // Constructor to support creation from a log level and tagset.
92 //
93 // LogStreamBase(level, tageset);
94 LogStream(LogLevelType level, LogTagSet* tagset) : _log_handle(level, tagset) {}
95
96 void write(const char* s, size_t len);
97 };
98
99 // Support creation of a LogStream without having to provide a LogTarget pointer.
100 #define LogStreamHandle(level, ...) LogStreamTemplate<LogLevel::level, LOG_TAGS(__VA_ARGS__)>
101
102 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
103 class LogStreamTemplate : public LogStream {
104 public:
105 LogStreamTemplate() : LogStream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL) {}
106 };
107
108 #endif // SHARE_LOGGING_LOGSTREAM_HPP
|
1 /*
2 * Copyright (c) 2016, 2020, 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
25 #ifndef SHARE_LOGGING_LOGSTREAM_HPP
26 #define SHARE_LOGGING_LOGSTREAM_HPP
27
28 #include "logging/log.hpp"
29 #include "logging/logHandle.hpp"
30 #include "utilities/ostream.hpp"
31
32 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
33 class AsyncLogTargetImpl;
34
35 class LogStream : public outputStream {
36 friend class LogStreamTest_TestLineBufferAllocation_test_vm_Test; // see test/native/logging/test_logStream.cpp
37 friend class LogStreamTest_TestLineBufferAllocationCap_test_vm_Test; // see test/native/logging/test_logStream.cpp
38
39 // Helper class, maintains the line buffer. For small line lengths,
40 // we avoid malloc and use a fixed sized member char array. If LogStream
41 // is allocated on the stack, this means small lines are assembled
42 // directly on the stack.
43 class LineBuffer {
44 char _smallbuf[64];
45 char* _buf;
46 size_t _cap;
47 size_t _pos;
48 void try_ensure_cap(size_t cap);
49 public:
50 LineBuffer();
51 ~LineBuffer();
52 bool is_empty() const { return _pos == 0; }
53 const char* buffer() const { return _buf; }
54 void append(const char* s, size_t len);
55 void reset();
56 size_t size() const { return _pos; }
57 };
58 LineBuffer _current_line;
59 LogTargetHandle _log_handle;
60
61 bool _async_log;
62 LogMessageBuffer* _async_buffer;
63
64 // Prevent operator new for LogStream.
65 static void* operator new (size_t);
66 static void* operator new[] (size_t);
67
68 void create_async_buffer();
69
70 public:
71 // Constructor to support creation from a LogTarget instance.
72 //
73 // LogTarget(Debug, gc) log;
74 // LogStreamBase(log) stream;
75 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
76 LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
77 _log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()), _async_log(false) {}
78
79 // Constructor to support creation from typed (likely NULL) pointer. Mostly used by the logging framework.
80 //
81 // LogStreamBase stream(log.debug());
82 // or
83 // LogStreamBase stream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL);
84 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
85 LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>* type_carrier) :
86 _log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()), _async_log(false) {}
87
88 // Destructor writes any unfinished output left in the line buffer.
89 ~LogStream();
90
91 // Constructor to support creation from a LogTargetHandle.
92 //
93 // LogTarget(Debug, gc) log;
94 // LogTargetHandle(log) handle;
95 // LogStreamBase stream(handle);
96 LogStream(LogTargetHandle handle) : _log_handle(handle), _async_log(false) {}
97
98 // Constructor to support creation from a log level and tagset.
99 //
100 // LogStreamBase(level, tageset);
101 LogStream(LogLevelType level, LogTagSet* tagset) : _log_handle(level, tagset), _async_log(false) {}
102
103 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
104 LogStream(const AsyncLogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
105 _log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()), _async_log(true), _async_buffer(NULL) {}
106
107 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
108 LogStream(const AsyncLogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>* type_carrier) :
109 _log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()), _async_log(true), _async_buffer(NULL) {}
110
111 LogStream(AsyncLogTargetHandle h) :
112 _log_handle(h.level(), h.tagset()), _async_log(true), _async_buffer(NULL) {}
113
114 void write(const char* s, size_t len);
115 };
116
117 // Support creation of a LogStream without having to provide a LogTarget pointer.
118 #define LogStreamHandle(level, ...) LogStreamTemplate<LogLevel::level, LOG_TAGS(__VA_ARGS__)>
119
120 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
121 class LogStreamTemplate : public LogStream {
122 public:
123 LogStreamTemplate() : LogStream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL) {}
124 };
125
126 #endif // SHARE_LOGGING_LOGSTREAM_HPP
|