< prev index next >

src/share/vm/logging/logStream.cpp

Print this page
rev 13113 : imported patch 8181917-refactor-ul-logstream
rev 13114 : imported patch 8181917-refactor-ul-logstream-changes-2
rev 13115 : [mq]: 8181917-refactor-ul-logstream-changes-3

@@ -22,13 +22,63 @@
  *
  */
 
 #include "precompiled.hpp"
 #include "logging/log.hpp"
-#include "logging/logStream.inline.hpp"
+#include "logging/logStream.hpp"
 
-// Create a log stream without an embedded ResourceMark.
-// The function is placed here to be called out-of-line in log.hpp.
-outputStream* create_log_stream(LogLevelType level, LogTagSet* tagset) {
-  return new LogStreamNoResourceMark(level, tagset);
+LogStream::LineBuffer::LineBuffer()
+ : _buf(_smallbuf), _cap(sizeof(_smallbuf)), _pos(0)
+{
+  _buf[0] = '\0';
+}
+
+LogStream::LineBuffer::~LineBuffer() {
+  assert(_pos == 0, "still outstanding bytes in the line buffer");
+  if (_buf != _smallbuf) {
+    os::free(_buf);
+  }
+}
+
+void LogStream::LineBuffer::ensure_cap(size_t atleast) {
+  assert(_cap >= sizeof(_smallbuf), "sanity");
+  if (_cap < atleast) {
+    const size_t newcap = MAX2(_cap * 2, atleast * 2);
+    char* const newbuf = (char*) os::malloc(newcap, mtLogging);
+    if (_pos > 0) { // preserve old content
+      memcpy(newbuf, _buf, _pos + 1); // ..including trailing zero
+    }
+    if (_buf != _smallbuf) {
+      os::free(_buf);
+    }
+    _buf = newbuf;
+    _cap = newcap;
+  }
+  assert(_cap >= atleast, "sanity");
+}
+
+void LogStream::LineBuffer::append(const char* s, size_t len) {
+  assert(_buf[_pos] == '\0', "sanity");
+  ensure_cap(_pos + len + 1);
+  assert(_cap >= _pos + len + 1, "sanity");
+  memcpy(_buf + _pos, s, len);
+  _pos += len;
+  _buf[_pos] = '\0';
+}
+
+void LogStream::LineBuffer::reset() {
+  _pos = 0;
+  _buf[_pos] = '\0';
+}
+
+void LogStream::write(const char* s, size_t len) {
+
+  if (len > 0 && s[len - 1] == '\n') {
+    _current_line.append(s, len - 1); // omit the newline.
+    _log_handle.print("%s", _current_line.ptr());
+    _current_line.reset();
+  } else {
+    _current_line.append(s, len);
+  }
+  update_position(s, len);
 }
 
< prev index next >