--- /dev/null 2016-01-16 19:30:22.947416487 +0100 +++ new/src/share/vm/logging/logHandle.hpp 2016-03-29 15:32:05.415341114 +0200 @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ +#ifndef SHARE_VM_LOGGING_LOGHANDLE_HPP +#define SHARE_VM_LOGGING_LOGHANDLE_HPP + +#include "logging/log.hpp" + +// Wraps a Log instance and throws away the template information. +// +// This can be used to pass a Log instance as a parameter without +// polluting the surrounding API with template functions. +class LogHandle { +private: + bool (*_is_level)(LogLevel::type level); + void (*_vwrite)(LogLevelType level, const char* fmt, va_list args) ATTRIBUTE_PRINTF(2, 0); + +public: + template + LogHandle(const LogImpl& type_carrier) : + _is_level(&LogImpl::is_level), + _vwrite( &LogImpl::vwrite) {} + +#define LOG_LEVEL(level, name) ATTRIBUTE_PRINTF(2, 0) \ + LogHandle& v##name(const char* fmt, va_list args) { \ + _vwrite(LogLevel::level, fmt, args); \ + return *this; \ + } \ + LogHandle& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) { \ + va_list args; \ + va_start(args, fmt); \ + _vwrite(LogLevel::level, fmt, args); \ + va_end(args); \ + return *this; \ + } \ + bool is_##name() { \ + return _is_level(LogLevel::level); \ + } + LOG_LEVEL_LIST +#undef LOG_LEVEL +}; + +// Wraps a LogTarget instance and throws away the template information. +// +// This can be used to pass a Log instance as a parameter without +// polluting the surrounding API with template functions. +class LogTargetHandle { + friend class LogStream; + +private: + bool (*_is_level)(); + void (*_vwrite)(const char* fmt, va_list args) ATTRIBUTE_PRINTF(1, 0); + +public: + template + LogTargetHandle(const LogTargetImpl& type_carrier) : + _is_level(&LogImpl::template is_level), + _vwrite(&LogImpl::template vwrite) {} + + template + static LogTargetHandle create() { + return LogTargetHandle(LogTargetImpl()); + } + + void print(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) { + va_list args; + va_start(args, fmt); + _vwrite(fmt, args); + va_end(args); + } + + bool is_enabled() const { + return _is_level(); + } + + // Creates a log stream from the information stored in this instance. + // Callers need a ResourceMark on the stack. + outputStream* stream() { + return create_log_stream(_vwrite); + } +}; + +#endif // SHARE_VM_LOGGING_LOGHANDLE_HPP