| 1 | /* | 
|---|
| 2 | * Copyright (c) 2016, 2019, 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_LOGGING_LOGMESSAGE_HPP | 
|---|
| 25 | #define SHARE_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). Internal buffers | 
|---|
| 41 | // are C heap allocated lazily on first write. LogMessages are | 
|---|
| 42 | // automatically written when they go out of scope. | 
|---|
| 43 | // | 
|---|
| 44 | // Example usage: | 
|---|
| 45 | // | 
|---|
| 46 | // { | 
|---|
| 47 | //   LogMessage(logging) msg; | 
|---|
| 48 | //   if (msg.is_debug()) { | 
|---|
| 49 | //     msg.debug("debug message"); | 
|---|
| 50 | //     msg.trace("additional trace information"); | 
|---|
| 51 | //   } | 
|---|
| 52 | // } | 
|---|
| 53 | // | 
|---|
| 54 | // Log outputs on trace level will see both of the messages above, | 
|---|
| 55 | // and the trace line will immediately follow the debug line. | 
|---|
| 56 | // They will have identical decorations (apart from level). | 
|---|
| 57 | // Log outputs on debug level will see the debug message, | 
|---|
| 58 | // but not the trace message. | 
|---|
| 59 | // | 
|---|
| 60 | #define LogMessage(...) LogMessageImpl<LOG_TAGS(__VA_ARGS__)> | 
|---|
| 61 | template <LogTagType T0, LogTagType T1 = LogTag::__NO_TAG, LogTagType T2 = LogTag::__NO_TAG, | 
|---|
| 62 | LogTagType T3 = LogTag::__NO_TAG, LogTagType T4 = LogTag::__NO_TAG, LogTagType GuardTag = LogTag::__NO_TAG> | 
|---|
| 63 | class LogMessageImpl : public LogMessageBuffer { | 
|---|
| 64 | private: | 
|---|
| 65 | LogImpl<T0, T1, T2, T3, T4, GuardTag> _log; | 
|---|
| 66 | bool _has_content; | 
|---|
| 67 |  | 
|---|
| 68 | public: | 
|---|
| 69 | LogMessageImpl() : _has_content(false) { | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | ~LogMessageImpl() { | 
|---|
| 73 | if (_has_content) { | 
|---|
| 74 | flush(); | 
|---|
| 75 | } | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | void flush() { | 
|---|
| 79 | _log.write(*this); | 
|---|
| 80 | reset(); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | void reset() { | 
|---|
| 84 | _has_content = false; | 
|---|
| 85 | LogMessageBuffer::reset(); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | ATTRIBUTE_PRINTF(3, 0) | 
|---|
| 89 | void vwrite(LogLevelType level, const char* fmt, va_list args) { | 
|---|
| 90 | if (!_has_content) { | 
|---|
| 91 | _has_content = true; | 
|---|
| 92 | set_prefix(LogPrefix<T0, T1, T2, T3, T4>::prefix); | 
|---|
| 93 | } | 
|---|
| 94 | LogMessageBuffer::vwrite(level, fmt, args); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | #define LOG_LEVEL(level, name) \ | 
|---|
| 98 | bool is_##name() const { \ | 
|---|
| 99 | return _log.is_level(LogLevel::level); \ | 
|---|
| 100 | } | 
|---|
| 101 | LOG_LEVEL_LIST | 
|---|
| 102 | #undef LOG_LEVEL | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 | #endif // SHARE_LOGGING_LOGMESSAGE_HPP | 
|---|
| 106 |  | 
|---|