1 /* 2 * Copyright (c) 2016, 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_VM_LOGGING_LOGSTREAM_HPP 26 #define SHARE_VM_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 37 // Helper class, maintains the line buffer. For small line lengths, 38 // we avoid malloc and use a fixed sized member char array. If LogStream 39 // is allocated on the stack, this means small lines are assembled 40 // directly on the stack. 41 class LineBuffer { 42 char _smallbuf[128]; 43 char* _buf; 44 size_t _cap; 45 size_t _pos; 46 void ensure_cap(size_t cap); 47 public: 48 LineBuffer(); 49 ~LineBuffer(); 50 const char* ptr() const { return _buf; } 51 void append(const char* s, size_t len); 52 void reset(); 53 }; 54 LineBuffer _current_line; 55 LogTargetHandle _log_handle; 56 57 // Prevent operator new for LogStream. 58 static void* operator new (size_t); 59 static void* operator new[] (size_t); 60 61 public: 62 // Constructor to support creation from a LogTarget instance. 63 // 64 // LogTarget(Debug, gc) log; 65 // LogStreamBase(log) stream; 66 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag> 67 LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) : 68 _log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {} 69 70 // Constructor to support creation from typed (likely NULL) pointer. Mostly used by the logging framework. 71 // 72 // LogStreamBase stream(log.debug()); 73 // or 74 // LogStreamBase stream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL); 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()) {} 78 79 // Constructor to support creation from a LogTargetHandle. 80 // 81 // LogTarget(Debug, gc) log; 82 // LogTargetHandle(log) handle; 83 // LogStreamBase stream(handle); 84 LogStream(LogTargetHandle handle) : _log_handle(handle) {} 85 86 // Constructor to support creation from a log level and tagset. 87 // 88 // LogStreamBase(level, tageset); 89 LogStream(LogLevelType level, LogTagSet* tagset) : _log_handle(level, tagset) {} 90 91 public: 92 void write(const char* s, size_t len); 93 }; 94 95 // Support creation of a LogStream without having to provide a LogTarget pointer. 96 #define LogStreamHandle(level, ...) LogStreamTemplate<LogLevel::level, LOG_TAGS(__VA_ARGS__)> 97 98 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag> 99 class LogStreamTemplate : public LogStream { 100 public: 101 LogStreamTemplate() : LogStream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL) {} 102 }; 103 104 105 #endif // SHARE_VM_LOGGING_LOGSTREAM_HPP