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.
33 class LogHandle {
34 private:
35 LogTagSet* _tagset;
36
37 public:
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 {
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.
73 class LogTargetHandle {
74 private:
75 const LogLevelType _level;
76 LogTagSet* _tagset;
77
78 public:
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
|
1 /*
2 * Copyright (c) 2016, 2020, 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/asynclog.hpp"
28 #include "logging/log.hpp"
29
30 // Wraps a Log instance and throws away the template information.
31 //
32 // This can be used to pass a Log instance as a parameter without
33 // polluting the surrounding API with template functions.
34 class LogHandle {
35 private:
36 LogTagSet* _tagset;
37
38 public:
39 template <LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
40 LogHandle(const LogImpl<T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
41 _tagset(&LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
42
43 bool is_level(LogLevelType level) {
44 return _tagset->is_level(level);
45 }
46
47 LogTagSet* tagset() const {
54 return *this; \
55 } \
56 LogHandle& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) { \
57 va_list args; \
58 va_start(args, fmt); \
59 _tagset->vwrite(LogLevel::level, fmt, args); \
60 va_end(args); \
61 return *this; \
62 } \
63 bool is_##name() { \
64 return _tagset->is_level(LogLevel::level); \
65 }
66 LOG_LEVEL_LIST
67 #undef LOG_LEVEL
68 };
69
70 // Wraps a LogTarget instance and throws away the template information.
71 //
72 // This can be used to pass a Log instance as a parameter without
73 // polluting the surrounding API with template functions.
74 template<typename D>
75 class LogTargetHandleBase {
76 protected :
77 const LogLevelType _level;
78 LogTagSet* _tagset;
79
80 LogTargetHandleBase(LogLevelType level, LogTagSet* tagset) : _level(level), _tagset(tagset) {}
81
82 public:
83 void print(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) {
84 va_list args;
85 va_start(args, fmt);
86 static_cast<D*>(this)->print(fmt,args);
87 va_end(args);
88 }
89
90 bool is_enabled() const {
91 return _tagset->is_level(_level);
92 }
93
94 LogLevelType level() const {
95 return _level;
96 }
97
98 LogTagSet* tagset() const {
99 return _tagset;
100 }
101 };
102
103 class LogTargetHandle : public LogTargetHandleBase<LogTargetHandle> {
104 public:
105 LogTargetHandle(LogLevelType level, LogTagSet* tagset) : LogTargetHandleBase<LogTargetHandle>(level, tagset) {}
106
107 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
108 LogTargetHandle(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
109 LogTargetHandleBase<LogTargetHandle>(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
110
111 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
112 static LogTargetHandle create() {
113 return LogTargetHandle(LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>());
114 }
115
116 void print(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) {
117 va_list args;
118 va_start(args, fmt);
119 _tagset->vwrite(_level, fmt, args);
120 va_end(args);
121 }
122 };
123
124 class AsyncLogTargetHandle : public LogTargetHandleBase<AsyncLogTargetHandle> {
125 public:
126 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
127 AsyncLogTargetHandle(const AsyncLogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
128 LogTargetHandleBase<AsyncLogTargetHandle>(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
129
130 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
131 static AsyncLogTargetHandle create() {
132 return AsyncLogTargetHandle(AsyncLogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>());
133 }
134
135 void print(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) {
136 DEBUG_ONLY(ShouldNotReachHere());
137 }
138 };
139
140
141 #endif // SHARE_LOGGING_LOGHANDLE_HPP
|