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 #ifndef SHARE_VM_LOGGING_LOGMESSAGE_HPP 25 #define SHARE_VM_LOGGING_LOGMESSAGE_HPP 26 27 #include "logging/log.hpp" 28 #include "logging/logMessageBuffer.hpp" 29 #include "logging/logPrefix.hpp" 30 #include "logging/logTag.hpp" 31 32 // The LogMessage class represents a multi-part/multi-line message 33 // that is guaranteed to be sent and written to the log outputs 34 // in a way that prevents interleaving by other log messages. 35 // 36 // The interface of LogMessage is very similar to the Log class, 37 // with printf functions for each level (trace(), debug(), etc). 38 // The difference is that these functions will append/write to the 39 // LogMessage, which only buffers the message-parts until the whole 40 // message is sent to a log (using Log::write). If TLS has been 41 // initialized, messages will be resource allocated, otherwise 42 // they will be C heap allocated. 43 // 44 // Example usage: 45 // 46 // { 47 // LogMessage(logging) msg; 48 // if (msg.is_debug()) { 49 // ResourceMark rm; 50 // msg.debug("debug message"); 51 // msg.trace("additional trace information"); 52 // } 53 // } 54 // 55 // Log outputs on trace level will see both of the messages above, 56 // and the trace line will immediately follow the debug line. 57 // They will have identical decorations (apart from level). 58 // Log outputs on debug level will see the debug message, 59 // but not the trace message. 60 // 61 #define LogMessage(...) ScopedLogMessage<LOG_TAGS(__VA_ARGS__)> 62 template <LogTagType T0, LogTagType T1 = LogTag::__NO_TAG, LogTagType T2 = LogTag::__NO_TAG, 63 LogTagType T3 = LogTag::__NO_TAG, LogTagType T4 = LogTag::__NO_TAG, LogTagType GuardTag = LogTag::__NO_TAG> 64 class ScopedLogMessage : public LogMessageBuffer { 65 private: 66 Log<T0, T1, T2, T3, T4> _log; 67 68 public: 69 ScopedLogMessage() { 70 set_prefix(LogPrefix<T0, T1, T2, T3, T4>::prefix); 71 } 72 73 ~ScopedLogMessage() { 74 if (_line_count > 0) { 75 _log.write(*this); 76 } 77 } 78 79 #define LOG_LEVEL(level, name) \ 80 bool is_##name() const { \ 81 return _log.is_level(LogLevel::level); \ 82 } 83 LOG_LEVEL_LIST 84 #undef LOG_LEVEL 85 }; 86 87 #endif // SHARE_VM_LOGGING_LOGMESSAGE_HPP