Path: blob/master/src/hotspot/share/logging/logHandle.hpp
40930 views
/*1* Copyright (c) 2016, 2019, 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*/23#ifndef SHARE_LOGGING_LOGHANDLE_HPP24#define SHARE_LOGGING_LOGHANDLE_HPP2526#include "logging/log.hpp"2728// Wraps a Log instance and throws away the template information.29//30// This can be used to pass a Log instance as a parameter without31// polluting the surrounding API with template functions.32class LogHandle {33private:34LogTagSet* _tagset;3536public:37template <LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>38LogHandle(const LogImpl<T0, T1, T2, T3, T4, GuardTag>& type_carrier) :39_tagset(&LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}4041bool is_level(LogLevelType level) {42return _tagset->is_level(level);43}4445LogTagSet* tagset() const {46return _tagset;47}4849#define LOG_LEVEL(level, name) ATTRIBUTE_PRINTF(2, 0) \50LogHandle& v##name(const char* fmt, va_list args) { \51_tagset->vwrite(LogLevel::level, fmt, args); \52return *this; \53} \54LogHandle& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) { \55va_list args; \56va_start(args, fmt); \57_tagset->vwrite(LogLevel::level, fmt, args); \58va_end(args); \59return *this; \60} \61bool is_##name() { \62return _tagset->is_level(LogLevel::level); \63}64LOG_LEVEL_LIST65#undef LOG_LEVEL66};6768// Wraps a LogTarget instance and throws away the template information.69//70// This can be used to pass a Log instance as a parameter without71// polluting the surrounding API with template functions.72class LogTargetHandle {73private:74const LogLevelType _level;75LogTagSet* _tagset;7677public:78LogTargetHandle(LogLevelType level, LogTagSet* tagset) : _level(level), _tagset(tagset) {}7980template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>81LogTargetHandle(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :82_level(level),83_tagset(&LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}8485template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>86static LogTargetHandle create() {87return LogTargetHandle(LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>());88}8990void print(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) {91va_list args;92va_start(args, fmt);93if (is_enabled()) {94_tagset->vwrite(_level, fmt, args);95}96va_end(args);97}9899bool is_enabled() const {100return _tagset->is_level(_level);101}102103};104105#endif // SHARE_LOGGING_LOGHANDLE_HPP106107108