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 #include "precompiled.hpp"
26 #include "logging/log.hpp"
27 #include "logging/logStream.inline.hpp"
28
29 // Create a log stream without an embedded ResourceMark.
30 // The function is placed here to be called out-of-line in log.hpp.
31 outputStream* create_log_stream(LogLevelType level, LogTagSet* tagset) {
32 return new LogStreamNoResourceMark(level, tagset);
33 }
34
|
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 #include "precompiled.hpp"
26 #include "logging/log.hpp"
27 #include "logging/logStream.hpp"
28
29 LogStream::LineBuffer::LineBuffer()
30 : _buf(_smallbuf), _cap(sizeof(_smallbuf)), _pos(0)
31 {
32 _buf[0] = '\0';
33 }
34
35 LogStream::LineBuffer::~LineBuffer() {
36 assert(_pos == 0, "still outstanding bytes in the line buffer");
37 if (_buf != _smallbuf) {
38 os::free(_buf);
39 }
40 }
41
42 // try_ensure_cap tries to enlarge the capacity of the internal buffer
43 // to the given atleast value. May fail if either OOM happens or atleast
44 // is larger than a reasonable max of 1 M. Caller must not assume
45 // capacity without checking.
46 void LogStream::LineBuffer::try_ensure_cap(size_t atleast) {
47 assert(_cap >= sizeof(_smallbuf), "sanity");
48 if (_cap < atleast) {
49 const size_t reasonable_max = 1 * M;
50 size_t newcap = align_size_up(atleast + 64, 64);
51 assert(_cap <= reasonable_max, "sanity");
52 // Cap out at a reasonable max to prevent runaway leaks.
53 if (newcap > reasonable_max) {
54 newcap = reasonable_max;
55 }
56
57 char* const newbuf = (char*) os::malloc(newcap, mtLogging);
58 if (newbuf == NULL) { // OOM. Leave object unchanged.
59 return;
60 }
61 if (_pos > 0) { // preserve old content
62 memcpy(newbuf, _buf, _pos + 1); // ..including trailing zero
63 }
64 if (_buf != _smallbuf) {
65 os::free(_buf);
66 }
67 _buf = newbuf;
68 _cap = newcap;
69 }
70 assert(_cap >= atleast, "sanity");
71 }
72
73 void LogStream::LineBuffer::append(const char* s, size_t len) {
74 assert(_buf[_pos] == '\0', "sanity");
75 assert(_pos < _cap, "sanity");
76 try_ensure_cap(_pos + len + 1);
77 // try_ensure_cap may not have enlarged the capacity to the full requested
78 // extend or may have not worked at all. In that case, just gracefully work
79 // with what we have already; just truncate if necessary.
80 if (_pos + len + 1 > _cap) {
81 len = _cap - _pos - 1;
82 if (len == 0) {
83 return;
84 }
85 }
86 memcpy(_buf + _pos, s, len);
87 _pos += len;
88 _buf[_pos] = '\0';
89 }
90
91 void LogStream::LineBuffer::reset() {
92 _pos = 0;
93 _buf[_pos] = '\0';
94 }
95
96 void LogStream::write(const char* s, size_t len) {
97 if (len > 0 && s[len - 1] == '\n') {
98 _current_line.append(s, len - 1); // omit the newline.
99 _log_handle.print("%s", _current_line.ptr());
100 _current_line.reset();
101 } else {
102 _current_line.append(s, len);
103 }
104 update_position(s, len);
105 }
106
|