/Users/deen/code/yugabyte-db/src/yb/client/callbacks.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | #ifndef YB_CLIENT_CALLBACKS_H_ |
33 | | #define YB_CLIENT_CALLBACKS_H_ |
34 | | |
35 | | #include "yb/gutil/macros.h" |
36 | | #include "yb/gutil/port.h" |
37 | | #include "yb/gutil/walltime.h" |
38 | | |
39 | | namespace yb { |
40 | | |
41 | | class Status; |
42 | | |
43 | | namespace client { |
44 | | |
45 | | // All possible log levels. |
46 | | enum YBLogSeverity { |
47 | | SEVERITY_INFO, |
48 | | SEVERITY_WARNING, |
49 | | SEVERITY_ERROR, |
50 | | SEVERITY_FATAL |
51 | | }; |
52 | | |
53 | | // Interface for all logging callbacks. |
54 | | class YBLoggingCallback { |
55 | | public: |
56 | 0 | YBLoggingCallback() { |
57 | 0 | } |
58 | | |
59 | 0 | virtual ~YBLoggingCallback() { |
60 | 0 | } |
61 | | |
62 | | // 'message' is NOT terminated with an endline. |
63 | | virtual void Run(YBLogSeverity severity, |
64 | | const char* filename, |
65 | | int line_number, |
66 | | const struct ::tm* time, |
67 | | const char* message, |
68 | | size_t message_len) = 0; |
69 | | |
70 | | private: |
71 | | DISALLOW_COPY_AND_ASSIGN(YBLoggingCallback); |
72 | | }; |
73 | | |
74 | | // Logging callback that invokes a member function pointer. |
75 | | template <typename T> |
76 | | class YBLoggingMemberCallback : public YBLoggingCallback { |
77 | | public: |
78 | | typedef void (T::*MemberType)( |
79 | | YBLogSeverity severity, |
80 | | const char* filename, |
81 | | int line_number, |
82 | | const struct ::tm* time, |
83 | | const char* message, |
84 | | size_t message_len); |
85 | | |
86 | | YBLoggingMemberCallback(T* object, MemberType member) |
87 | | : object_(object), |
88 | | member_(member) { |
89 | | } |
90 | | |
91 | | virtual void Run(YBLogSeverity severity, |
92 | | const char* filename, |
93 | | int line_number, |
94 | | const struct ::tm* time, |
95 | | const char* message, |
96 | | size_t message_len) override { |
97 | | (object_->*member_)(severity, filename, line_number, time, |
98 | | message, message_len); |
99 | | } |
100 | | |
101 | | private: |
102 | | T* object_; |
103 | | MemberType member_; |
104 | | }; |
105 | | |
106 | | // Logging callback that invokes a function pointer with a single argument. |
107 | | template <typename T> |
108 | | class YBLoggingFunctionCallback : public YBLoggingCallback { |
109 | | public: |
110 | | typedef void (*FunctionType)(T arg, |
111 | | YBLogSeverity severity, |
112 | | const char* filename, |
113 | | int line_number, |
114 | | const struct ::tm* time, |
115 | | const char* message, |
116 | | size_t message_len); |
117 | | |
118 | | YBLoggingFunctionCallback(FunctionType function, T arg) |
119 | | : function_(function), |
120 | | arg_(arg) { |
121 | | } |
122 | | |
123 | | virtual void Run(YBLogSeverity severity, |
124 | | const char* filename, |
125 | | int line_number, |
126 | | const struct ::tm* time, |
127 | | const char* message, |
128 | | size_t message_len) override { |
129 | | function_(arg_, severity, filename, line_number, time, |
130 | | message, message_len); |
131 | | } |
132 | | |
133 | | private: |
134 | | FunctionType function_; |
135 | | T arg_; |
136 | | }; |
137 | | |
138 | | // Interface for all status callbacks. |
139 | | class YBStatusCallback { |
140 | | public: |
141 | 0 | YBStatusCallback() { |
142 | 0 | } |
143 | | |
144 | 0 | virtual ~YBStatusCallback() { |
145 | 0 | } |
146 | | |
147 | | virtual void Run(const Status& s) = 0; |
148 | | |
149 | | private: |
150 | | DISALLOW_COPY_AND_ASSIGN(YBStatusCallback); |
151 | | }; |
152 | | |
153 | | // Status callback that invokes a member function pointer. |
154 | | template <typename T> |
155 | | class YBStatusMemberCallback : public YBStatusCallback { |
156 | | public: |
157 | | typedef void (T::*MemberType)(const Status& s); |
158 | | |
159 | | YBStatusMemberCallback(T* object, MemberType member) |
160 | | : object_(object), |
161 | | member_(member) { |
162 | | } |
163 | | |
164 | | virtual void Run(const Status& s) override { |
165 | | (object_->*member_)(s); |
166 | | } |
167 | | |
168 | | private: |
169 | | T* object_; |
170 | | MemberType member_; |
171 | | }; |
172 | | |
173 | | // Status callback that invokes a function pointer with a single argument. |
174 | | template <typename T> |
175 | | class YBStatusFunctionCallback : public YBStatusCallback { |
176 | | public: |
177 | | typedef void (*FunctionType)(T arg, const Status& s); |
178 | | |
179 | | YBStatusFunctionCallback(FunctionType function, T arg) |
180 | | : function_(function), |
181 | | arg_(arg) { |
182 | | } |
183 | | |
184 | | virtual void Run(const Status& s) override { |
185 | | function_(arg_, s); |
186 | | } |
187 | | |
188 | | private: |
189 | | FunctionType function_; |
190 | | T arg_; |
191 | | }; |
192 | | |
193 | | template <class Functor> |
194 | | class YBStatusFunctorCallback : public YBStatusCallback { |
195 | | public: |
196 | | explicit YBStatusFunctorCallback(const Functor& functor) : functor_(functor) {} |
197 | | |
198 | | void Run(const Status& status) override { |
199 | | functor_(status); |
200 | | delete this; |
201 | | } |
202 | | private: |
203 | | Functor functor_; |
204 | | }; |
205 | | |
206 | | template <class Functor> |
207 | | YBStatusFunctorCallback<Functor>* MakeYBStatusFunctorCallback(const Functor& functor) { |
208 | | return new YBStatusFunctorCallback<Functor>(functor); |
209 | | } |
210 | | |
211 | | } // namespace client |
212 | | } // namespace yb |
213 | | |
214 | | #endif // YB_CLIENT_CALLBACKS_H_ |