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_LOGHANDLE_HPP
25#define SHARE_LOGGING_LOGHANDLE_HPP
26
27#include "logging/log.hpp"
28
29// Wraps a Log instance and throws away the template information.
30//
31// This can be used to pass a Log instance as a parameter without
32// polluting the surrounding API with template functions.
33class LogHandle {
34private:
35 LogTagSet* _tagset;
36
37public:
38 template <LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
39 LogHandle(const LogImpl<T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
40 _tagset(&LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
41
42 bool is_level(LogLevelType level) {
43 return _tagset->is_level(level);
44 }
45
46 LogTagSet* tagset() const {
47 return _tagset;
48 }
49
50#define LOG_LEVEL(level, name) ATTRIBUTE_PRINTF(2, 0) \
51 LogHandle& v##name(const char* fmt, va_list args) { \
52 _tagset->vwrite(LogLevel::level, fmt, args); \
53 return *this; \
54 } \
55 LogHandle& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) { \
56 va_list args; \
57 va_start(args, fmt); \
58 _tagset->vwrite(LogLevel::level, fmt, args); \
59 va_end(args); \
60 return *this; \
61 } \
62 bool is_##name() { \
63 return _tagset->is_level(LogLevel::level); \
64 }
65 LOG_LEVEL_LIST
66#undef LOG_LEVEL
67};
68
69// Wraps a LogTarget instance and throws away the template information.
70//
71// This can be used to pass a Log instance as a parameter without
72// polluting the surrounding API with template functions.
73class LogTargetHandle {
74private:
75 const LogLevelType _level;
76 LogTagSet* _tagset;
77
78public:
79 LogTargetHandle(LogLevelType level, LogTagSet* tagset) : _level(level), _tagset(tagset) {}
80
81 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
82 LogTargetHandle(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
83 _level(level),
84 _tagset(&LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
85
86 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
87 static LogTargetHandle create() {
88 return LogTargetHandle(LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>());
89 }
90
91 void print(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) {
92 va_list args;
93 va_start(args, fmt);
94 _tagset->vwrite(_level, fmt, args);
95 va_end(args);
96 }
97
98 bool is_enabled() const {
99 return _tagset->is_level(_level);
100 }
101
102};
103
104#endif // SHARE_LOGGING_LOGHANDLE_HPP
105