< prev index next >

src/hotspot/share/logging/logStream.cpp

Print this page
rev 58036 : [mq]: asynclog


   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");


  87   // with what we have already; just truncate if necessary.
  88   if (_cap < minimum_capacity_needed) {
  89     len = _cap - _pos - 1;
  90     if (len == 0) {
  91       return;
  92     }
  93   }
  94   memcpy(_buf + _pos, s, len);
  95   _pos += len;
  96   _buf[_pos] = '\0';
  97 }
  98 
  99 void LogStream::LineBuffer::reset() {
 100   _pos = 0;
 101   _buf[_pos] = '\0';
 102 }
 103 
 104 void LogStream::write(const char* s, size_t len) {
 105   if (len > 0 && s[len - 1] == '\n') {
 106     _current_line.append(s, len - 1); // omit the newline.

 107     _log_handle.print("%s", _current_line.buffer());






 108     _current_line.reset();
 109   } else {
 110     _current_line.append(s, len);
 111   }

 112   update_position(s, len);
 113 }
 114 
 115 // Destructor writes any unfinished output left in the line buffer.
 116 LogStream::~LogStream() {
 117   if (_current_line.is_empty() == false) {

 118     _log_handle.print("%s", _current_line.buffer());







 119     _current_line.reset();
 120   }
 121 }
 122 





 123 






   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 #include "logging/logMessageBuffer.hpp"
  29 #include "logging/logAsyncFlusher.hpp"
  30 
  31 LogStream::LineBuffer::LineBuffer()
  32  : _buf(_smallbuf), _cap(sizeof(_smallbuf)), _pos(0)
  33 {
  34   _buf[0] = '\0';
  35 }
  36 
  37 LogStream::LineBuffer::~LineBuffer() {
  38   assert(_pos == 0, "still outstanding bytes in the line buffer");
  39   if (_buf != _smallbuf) {
  40     os::free(_buf);
  41   }
  42 }
  43 
  44 // try_ensure_cap tries to enlarge the capacity of the internal buffer
  45 // to the given atleast value. May fail if either OOM happens or atleast
  46 // is larger than a reasonable max of 1 M. Caller must not assume
  47 // capacity without checking.
  48 void LogStream::LineBuffer::try_ensure_cap(size_t atleast) {
  49   assert(_cap >= sizeof(_smallbuf), "sanity");


  89   // with what we have already; just truncate if necessary.
  90   if (_cap < minimum_capacity_needed) {
  91     len = _cap - _pos - 1;
  92     if (len == 0) {
  93       return;
  94     }
  95   }
  96   memcpy(_buf + _pos, s, len);
  97   _pos += len;
  98   _buf[_pos] = '\0';
  99 }
 100 
 101 void LogStream::LineBuffer::reset() {
 102   _pos = 0;
 103   _buf[_pos] = '\0';
 104 }
 105 
 106 void LogStream::write(const char* s, size_t len) {
 107   if (len > 0 && s[len - 1] == '\n') {
 108     _current_line.append(s, len - 1); // omit the newline.
 109     if (!_async_log) {
 110       _log_handle.print("%s", _current_line.buffer());
 111     } else {
 112       if (!_async_buffer) {
 113         _async_buffer = new (mtLogging)LogMessageBuffer();
 114       }
 115       _async_buffer->write_n(_log_handle.level(), _current_line.buffer(), _current_line.size());
 116     }
 117     _current_line.reset();
 118   } else {
 119     _current_line.append(s, len);
 120   }
 121 
 122   update_position(s, len);
 123 }
 124 
 125 // Destructor writes any unfinished output left in the line buffer.
 126 LogStream::~LogStream() {
 127   if (!_current_line.is_empty()) {
 128     if (!_async_log) {
 129       _log_handle.print("%s", _current_line.buffer());
 130     }
 131     else {
 132       if (!_async_buffer) {
 133         _async_buffer = new (mtLogging)LogMessageBuffer();
 134       }
 135       _async_buffer->write_n(_log_handle.level(), _current_line.buffer(), _current_line.size());
 136     }
 137     _current_line.reset();
 138   }

 139 
 140   if (_async_log && _async_buffer) {
 141     LogAsyncFlusher* flusher = LogAsyncFlusher::instance();
 142     flusher->enqueue(_log_handle.tagset(), _async_buffer);
 143   }
 144 }
 145 
 146 void LogStream::create_async_buffer() {
 147   Arena* arena  = new (mtLogging)Arena(mtLogging, LogMessageBuffer::initial_buffer_size());
 148   _async_buffer = new (mtLogging)LogMessageBuffer(arena);
 149 }
< prev index next >