log.hpp revision 9336:766ae06f30ca
1/* 2 * Copyright (c) 2015, 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_LOG_HPP 25#define SHARE_VM_LOGGING_LOG_HPP 26 27#include "logging/logLevel.hpp" 28#include "logging/logPrefix.hpp" 29#include "logging/logTagSet.hpp" 30#include "logging/logTag.hpp" 31#include "memory/allocation.hpp" 32#include "memory/allocation.inline.hpp" 33#include "runtime/os.hpp" 34#include "utilities/debug.hpp" 35#include "utilities/ostream.hpp" 36 37// 38// Logging macros 39// 40// Usage: 41// log_<level>(<comma separated log tags>)(<printf-style log arguments>); 42// e.g. 43// log_debug(logging)("message %d", i); 44// 45// Note that these macros will not evaluate the arguments unless the logging is enabled. 46// 47#define log_error(...) (!log_is_enabled(Error, __VA_ARGS__)) ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Error> 48#define log_warning(...) (!log_is_enabled(Warning, __VA_ARGS__)) ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Warning> 49#define log_info(...) (!log_is_enabled(Info, __VA_ARGS__)) ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Info> 50#define log_debug(...) (!log_is_enabled(Debug, __VA_ARGS__)) ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Debug> 51#define log_trace(...) (!log_is_enabled(Trace, __VA_ARGS__)) ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Trace> 52#ifndef PRODUCT 53#define log_develop(...) (!log_is_enabled(Develop, __VA_ARGS__)) ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Develop> 54#else 55#define DUMMY_ARGUMENT_CONSUMER(...) 56#define log_develop(...) DUMMY_ARGUMENT_CONSUMER 57#endif 58 59// Convenience macro to test if the logging is enabled on the specified level for given tags. 60#define log_is_enabled(level, ...) (Log<LOG_TAGS(__VA_ARGS__)>::is_level(LogLevel::level)) 61 62// 63// Log class for more advanced logging scenarios. 64// Has printf-style member functions for each log level (trace(), debug(), etc). 65// 66// Also has outputStream compatible API for the different log-levels. 67// The streams are resource allocated when requested and are accessed through 68// calls to <level>_stream() functions (trace_stream(), debug_stream(), etc). 69// 70// Example usage: 71// LogHandle(logging) log; 72// if (log.is_debug()) { 73// ... 74// log.debug("result = %d", result).trace(" tracing info"); 75// obj->print_on(log.debug_stream()); 76// } 77// 78#define LogHandle(...) Log<LOG_TAGS(__VA_ARGS__)> 79 80template <LogTagType T0, LogTagType T1 = LogTag::__NO_TAG, LogTagType T2 = LogTag::__NO_TAG, LogTagType T3 = LogTag::__NO_TAG, 81 LogTagType T4 = LogTag::__NO_TAG, LogTagType GuardTag = LogTag::__NO_TAG> 82class Log VALUE_OBJ_CLASS_SPEC { 83 private: 84 static const size_t LogBufferSize = 512; 85 public: 86 // Make sure no more than the maximum number of tags have been given. 87 // The GuardTag allows this to be detected if/when it happens. If the GuardTag 88 // is not __NO_TAG, the number of tags given exceeds the maximum allowed. 89 STATIC_ASSERT(GuardTag == LogTag::__NO_TAG); // Number of logging tags exceeds maximum supported! 90 91 static bool is_level(LogLevelType level) { 92 return LogTagSetMapping<T0, T1, T2, T3, T4>::tagset().is_level(level); 93 } 94 95 template <LogLevelType Level> 96 ATTRIBUTE_PRINTF(1, 2) 97 static void write(const char* fmt, ...) { 98 va_list args; 99 va_start(args, fmt); 100 vwrite<Level>(fmt, args); 101 va_end(args); 102 }; 103 104 template <LogLevelType Level> 105 ATTRIBUTE_PRINTF(1, 0) 106 static void vwrite(const char* fmt, va_list args) { 107 char buf[LogBufferSize]; 108 size_t prefix_len = LogPrefix<T0, T1, T2, T3, T4>::prefix(buf, sizeof(buf)); 109 // Check that string fits in buffer; resize buffer if necessary 110 int ret = os::log_vsnprintf(buf + prefix_len, sizeof(buf) - prefix_len, fmt, args); 111 assert(ret >= 0, "Log message buffer issue"); 112 if ((size_t)ret > sizeof(buf)) { 113 size_t newbuf_len = prefix_len + ret + 1; 114 char* newbuf = NEW_C_HEAP_ARRAY(char, newbuf_len, mtLogging); 115 prefix_len = LogPrefix<T0, T1, T2, T3, T4>::prefix(newbuf, newbuf_len); 116 ret = os::log_vsnprintf(newbuf + prefix_len, newbuf_len - prefix_len, fmt, args); 117 assert(ret >= 0, "Log message buffer issue"); 118 puts<Level>(newbuf); 119 FREE_C_HEAP_ARRAY(char, newbuf); 120 } else { 121 puts<Level>(buf); 122 } 123 } 124 125 template <LogLevelType Level> 126 static void puts(const char* string) { 127 LogTagSetMapping<T0, T1, T2, T3, T4>::tagset().log(Level, string); 128 } 129 130#define LOG_LEVEL(level, name) ATTRIBUTE_PRINTF(2, 0) \ 131 Log& v##name(const char* fmt, va_list args) { \ 132 vwrite<LogLevel::level>(fmt, args); \ 133 return *this; \ 134 } \ 135 Log& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) { \ 136 va_list args; \ 137 va_start(args, fmt); \ 138 vwrite<LogLevel::level>(fmt, args); \ 139 va_end(args); \ 140 return *this; \ 141 } \ 142 static bool is_##name() { \ 143 return is_level(LogLevel::level); \ 144 } \ 145 static outputStream* name##_stream() { \ 146 return new logStream(write<LogLevel::level>); \ 147 } 148 LOG_LEVEL_LIST 149#undef LOG_LEVEL 150}; 151 152#endif // SHARE_VM_LOGGING_LOG_HPP 153