Path: blob/master/src/hotspot/share/logging/logStream.hpp
40930 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_LOGGING_LOGSTREAM_HPP25#define SHARE_LOGGING_LOGSTREAM_HPP2627#include "logging/log.hpp"28#include "logging/logHandle.hpp"29#include "utilities/ostream.hpp"303132class LogStream : public outputStream {33// see test/hotspot/gtest/logging/test_logStream.cpp34friend class LogStreamTest_TestLineBufferAllocation_vm_Test;35friend class LogStreamTest_TestLineBufferAllocationCap_vm_Test;3637// Helper class, maintains the line buffer. For small line lengths,38// we avoid malloc and use a fixed sized member char array. If LogStream39// is allocated on the stack, this means small lines are assembled40// directly on the stack.41class LineBuffer {42char _smallbuf[64];43char* _buf;44size_t _cap;45size_t _pos;46void try_ensure_cap(size_t cap);47public:48LineBuffer();49~LineBuffer();50bool is_empty() const { return _pos == 0; }51const char* buffer() const { return _buf; }52void append(const char* s, size_t len);53void reset();54};55LineBuffer _current_line;56LogTargetHandle _log_handle;5758// Prevent operator new for LogStream.59static void* operator new (size_t);60static void* operator new[] (size_t);6162public:63// Constructor to support creation from a LogTarget instance.64//65// LogTarget(Debug, gc) log;66// LogStreamBase(log) stream;67template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>68LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :69_log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}7071// Constructor to support creation from typed (likely NULL) pointer. Mostly used by the logging framework.72//73// LogStreamBase stream(log.debug());74// or75// LogStreamBase stream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL);76template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>77LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>* type_carrier) :78_log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}7980// Destructor writes any unfinished output left in the line buffer.81~LogStream();8283// Constructor to support creation from a LogTargetHandle.84//85// LogTarget(Debug, gc) log;86// LogTargetHandle(log) handle;87// LogStreamBase stream(handle);88LogStream(LogTargetHandle handle) : _log_handle(handle) {}8990// Constructor to support creation from a log level and tagset.91//92// LogStreamBase(level, tageset);93LogStream(LogLevelType level, LogTagSet* tagset) : _log_handle(level, tagset) {}9495bool is_enabled() const {96return _log_handle.is_enabled();97}9899void write(const char* s, size_t len);100};101102// Support creation of a LogStream without having to provide a LogTarget pointer.103#define LogStreamHandle(level, ...) LogStreamTemplate<LogLevel::level, LOG_TAGS(__VA_ARGS__)>104105template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>106class LogStreamTemplate : public LogStream {107public:108LogStreamTemplate() : LogStream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL) {}109};110111#endif // SHARE_LOGGING_LOGSTREAM_HPP112113114