MySQL 9.6.0
Source Code Documentation
gcs_logging.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2025, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef GCS_LOGGING_INCLUDED
25#define GCS_LOGGING_INCLUDED
26
27#include <atomic>
28#include <cstdint>
29#include <string>
30
32
33/**
34 @class Common_interface
35
36 Common interface that is used to define the sink and logger interfaces.
37*/
39 public:
40 /**
41 Define a virtual destructor as instances of this interface can be
42 polymorphically used.
43 */
44
45 virtual ~Common_interface() = default;
46
47 /**
48 The purpose of this method is to initialize resources used by the objects
49 that implement this interface.
50
51 @retval GCS_OK in case everything goes well. Any other value of
52 gcs_error in case of error.
53 */
54
56
57 /**
58 The purpose of this method is to free any resources used by the objects
59 that implement this interface.
60
61 @retval GCS_OK in case everything goes well. Any other value of
62 gcs_error in case of error.
63 */
64
65 virtual enum_gcs_error finalize() = 0;
66};
67
68/**
69 This is the interface for the timestamp provider for entries that end up in
70 the Debugger log. The GCS will add timestamps to the log entries according to
71 the representation returned by the member functions below.
72 */
74 public:
76 ~Clock_timestamp_interface() override = default;
77 // non-copyable
80 delete;
81 // non-movable
84 delete;
85
86 /**
87 Get the timestamp as c string object.
88
89 @param[in] buffer the buffer to store the timestamp into
90 @param[in,out] size as an input contains the size of the buffer, as output
91 contains the number of bytes written to the buffer.
92 */
93 virtual void get_timestamp_as_c_string(char *buffer, size_t *size) = 0;
94
95 /**
96 Get the timestamp as std::string.
97
98 @param str the string to store the timestamp into.
99 */
100 virtual void get_timestamp_as_string(std::string &str) = 0;
101};
102
103/**
104 @class Sink_interface
105
106 Common sink that may be shared by the logging and debugging systems.
107*/
108
110 public:
111 /**
112 Define a virtual destructor as instances of this interface can be
113 polymorphically used.
114 */
115
116 ~Sink_interface() override = default;
117
118 /**
119 The purpose of this method is to effectively log the information.
120
121 @param[in] message the message to log
122 */
123
124 virtual void log_event(const std::string &message) = 0;
125
126 /**
127 The purpose of this method is to effectively log the information.
128
129 @param[in] message the message to log
130 @param[in] message_size message size to log
131 */
132
133 virtual void log_event(const char *message, size_t message_size) = 0;
134
135 /**
136 The purpose of this method is to return information on the sink such
137 as its location.
138 */
139
140 virtual const std::string get_information() const = 0;
141};
142
143typedef enum {
149
150static const char *const gcs_log_levels[] = {
151 "[MYSQL_GCS_FATAL] ", "[MYSQL_GCS_ERROR] ", "[MYSQL_GCS_WARN] ",
152 "[MYSQL_GCS_INFO] "};
153
154/**
155 @class Logger_interface
156
157 Logger interface that must be used to define a logger object.
158*/
159
161 public:
162 /**
163 Define a virtual destructor as instances of this interface can be
164 polymorphically used.
165 */
166
167 ~Logger_interface() override = default;
168
169 /**
170 The purpose of this method is to deliver to the logging system any message
171 to be logged.
172
173 @param[in] level logging level of message
174 @param[in] message the message to be logged
175 */
176
177 virtual void log_event(const gcs_log_level_t level,
178 const std::string &message) = 0;
179};
180
181/**
182 @class Gcs_log_manager
183
184 This class sets up and configures the logging infrastructure, storing the
185 logger to be used by the application as a singleton.
186*/
187
189 private:
191
192 public:
193 /**
194 Set the logger object and initialize it by invoking its initialization
195 method.
196
197 This allows any resources needed by the logging system to be initialized,
198 and ensures its usage throughout the lifecycle of the current application,
199 i.e. GCS.
200
201 @param[in] logger logging system
202 @retval GCS_OK in case everything goes well. Any other value of
203 gcs_error in case of error.
204 */
205
207
208 /**
209 Get a reference to the logger object if there is any.
210
211 @return The current logging system.
212
213 */
214
216
217 /**
218 Free any resource used in the logging system.
219
220 @retval GCS_OK in case everything goes well. Any other value of
221 gcs_error in case of error.
222 */
223
224 static enum_gcs_error finalize();
225};
226
227/*
228 These two definitions contain information on the valid debug options. If
229 one wants to add a new option both definitions must be changed.
230
231 Any new option must be added before the GCS_DEBUG_ALL and gaps in the
232 numbers are not allowed. Adding a new debug option, e.g. GCS_DEBUG_THREAD,
233 would result in the following definitions:
234
235 typedef enum
236 {
237 GCS_DEBUG_NONE = 0x00000000,
238 GCS_DEBUG_BASIC = 0x00000001,
239 GCS_DEBUG_TRACE = 0x00000002,
240 XCOM_DEBUG_BASIC = 0x00000004,
241 XCOM_DEBUG_TRACE = 0x00000008,
242 GCS_INVALID_DEBUG = ~(0x7FFFFFFF),
243 GCS_DEBUG_ALL = ~(GCS_DEBUG_NONE)
244 } gcs_xcom_debug_option_t;
245
246
247 static const char* const gcs_xcom_debug_strings[]=
248 {
249 "GCS_DEBUG_BASIC",
250 "GCS_DEBUG_TRACE",
251 "XCOM_DEBUG_BASIC",
252 "XCOM_DEBUG_TRACE",
253 "GCS_DEBUG_ALL",
254 "GCS_DEBUG_NONE"
255 };
256*/
257#if !defined(GCS_XCOM_DEBUG_INFORMATION)
258#define GCS_XCOM_DEBUG_INFORMATION
259/*
260This code is duplicated in the xcom_logger.h file. So if you make any change
261here, remember to update the aforementioned file. Note that XCOM is written
262in C which uses a 32 bit integer as the type for the enumeration and although
263the code is prepared to handle 64 bits, the enumeration in both the GCS (i.e.
264C++) and XCOM (i.e. C) is restricted to 32 bits.
265
266Assuming that we are not using all bits available to define a debug level,
267GCS_INVALID_DEBUG will give us the last possible entry that is not used.
268
269The GCS_DEBUG_NONE, GCS_DEBUG_ALL and GCS_INVALID_DEBUG are options that apply
270to both GCS and XCOM but we don't prefix it with XCOM to avoid big names.
271*/
272typedef enum {
273 GCS_DEBUG_NONE = 0x00000000,
274 GCS_DEBUG_BASIC = 0x00000001,
275 GCS_DEBUG_TRACE = 0x00000002,
276 XCOM_DEBUG_BASIC = 0x00000004,
277 XCOM_DEBUG_TRACE = 0x00000008,
278 GCS_DEBUG_MSG_FLOW = 0x00000010,
280 GCS_INVALID_DEBUG = ~(0x7FFFFFFF),
283
284static const char *const gcs_xcom_debug_strings[] = {
285 "GCS_DEBUG_BASIC", "GCS_DEBUG_TRACE", "XCOM_DEBUG_BASIC",
286 "XCOM_DEBUG_TRACE", "GCS_DEBUG_MSG_FLOW", "XCOM_DEBUG_MSG_FLOW",
287 "GCS_DEBUG_ALL", "GCS_DEBUG_NONE",
288};
289
290/*
291 Assuming that we are not using all bits available to define a debug
292 level, this will give us the last possible entry that is not used.
293*/
294#endif
295
296/**
297 @class Gcs_debug_options
298
299 This class sets up and configures the debugging infrastructure, storing the
300 debugger to be used by the application as a singleton.
301*/
302
304 private:
305 /**
306 The debug level enabled which is by default GCS_DEBUG_NONE;
307 */
308 static std::atomic<std::int64_t> m_debug_options;
309
310 /**
311 String that represents the GCS_DEBUG_NONE;
312 */
313 static const std::string m_debug_none;
314
315 /**
316 String that represents the GCS_DEBUG_ALL;
317 */
318 static const std::string m_debug_all;
319
320 public:
321 /**
322 Atomically load information on debug options.
323 */
324
325 static inline int64_t load_debug_options() {
326 /*
327 We don't need anything stronger than "memory order relaxed" because
328 we are only interested in reading and updating a single variable
329 atomically.
330 */
331 return m_debug_options.load(std::memory_order_relaxed);
332 }
333
334 /**
335 Atomically store information on debug options.
336 */
337
338 static inline void store_debug_options(int64_t debug_options) {
339 /*
340 We don't need anything stronger than "memory order relaxed" because
341 we are only interested in reading and updating a single variable
342 atomically.
343 */
344 m_debug_options.store(debug_options, std::memory_order_relaxed);
345 }
346
347 /**
348 Verify whether any of the debug options are defined.
349
350 @param debug_options Set of debug options to be verified.
351
352 @retval true if it is, false otherwise.
353 */
354
355 static inline bool test_debug_options(const int64_t debug_options) {
356 return load_debug_options() & debug_options;
357 }
358
359 /**
360 Get the current set of debug options.
361 */
362
363 static int64_t get_current_debug_options();
364
365 /**
366 Get the current set of debug options as an integer value and as a
367 string separated by comma.
368
369 @param[out] res_debug_options String containing the result
370 */
371
372 static int64_t get_current_debug_options(std::string &res_debug_options);
373
374 /**
375 Get the set of valid debug options excluding GCS_DEBUG_NONE and
376 GCS_DEBUG_ALL.
377 */
378
379 static int64_t get_valid_debug_options();
380
381 /**
382 Check whether the set of debug options is valid or not including
383 GCS_DEBUG_NONE and GCS_DEBUG_ALL.
384
385 @param[in] debug_options Set of debug options
386 @retval true if success, false otherwise
387 */
388
389 static bool is_valid_debug_options(const int64_t debug_options);
390
391 /**
392 Check whether the set of debug options is valid or not including
393 GCS_DEBUG_NONE and GCS_DEBUG_ALL.
394
395 @param[in] debug_options Set of debug options
396 @retval false if success, true otherwise.
397 */
398
399 static bool is_valid_debug_options(const std::string &debug_options);
400
401 /**
402 Get the set of debug options passed as parameter as an unsigned integer.
403
404 If there is any invalid debug option in the debug_options parameter, true
405 is returned.
406
407 @param[in] debug_options Set of debug options
408 @param[out] res_debug_options Unsigned integer that contains the result
409 @retval false if success, true otherwise.
410 */
411
412 static bool get_debug_options(const std::string &debug_options,
413 int64_t &res_debug_options);
414
415 /**
416 Get the set of debug options passed as parameter as a string.
417
418 If there is any invalid debug option in the debug_options parameter, true
419 is returned.
420
421 @param[in] debug_options Set of debug options
422 @param[out] res_debug_options String that contains the result
423 @retval false if success, true otherwise.
424 */
425 static bool get_debug_options(const int64_t debug_options,
426 std::string &res_debug_options);
427
428 /**
429 Get the the number of possible debug options.
430 */
431
432 static unsigned int get_number_debug_options();
433
434 /**
435 Extend the current set of debug options with new debug options expressed as
436 an integer parameter.
437
438 If there is any invalid debug option in the debug_options parameter, true
439 is returned and nothing is changed.
440
441 @param[in] debug_options Set of debug options to be added
442 @retval false if success, true otherwise.
443 */
444
445 static bool set_debug_options(const int64_t debug_options);
446
447 /**
448 Change the current set of debug options by the new debug options expressed
449 as an integer parameter.
450
451 If there is any invalid debug option in the debug_options parameter, true
452 is returned and nothing is changed.
453
454 @param[in] debug_options Set of debug options to be defined
455 @retval false if success, true otherwise.
456 */
457
458 static bool force_debug_options(const int64_t debug_options);
459
460 /**
461 Extend the current set of debug options with new debug options expressed
462 as a string.
463
464 If there is any invalid debug option in the debug_options parameter, true
465 is returned and nothing is changed.
466
467 @param[in] debug_options Set of debug options to be added
468 @retval false if success, true otherwise.
469 */
470
471 static bool set_debug_options(const std::string &debug_options);
472
473 /**
474 Changed the current set of debug options by the new debug options expressed
475 as a string.
476
477 If there is any invalid debug option in the debug_options parameter, true
478 is returned and nothing is changed.
479
480 @param[in] debug_options Set of debug options to be defined
481 @retval false if success, true otherwise.
482
483 */
484
485 static bool force_debug_options(const std::string &debug_options);
486
487 /**
488 Reduce the current set of debug options by disabling the debug options
489 expressed as an integer parameter.
490
491 If there is any invalid debug option in the debug_options parameter, true
492 is returned and nothing is changed.
493
494 @param[in] debug_options Set of debug options to be disabled
495 @retval false if success, true otherwise.
496 */
497
498 static bool unset_debug_options(const int64_t debug_options);
499
500 /**
501 Reduce the current set of debug options by disabling the debug options
502 expressed as a string.
503
504 If there is any invalid debug option in the debug_options parameter, true
505 is returned and nothing is changed.
506
507 @param[in] debug_options Set of debug options to be disabled
508 @retval false if success, true otherwise.
509 */
510
511 static bool unset_debug_options(const std::string &debug_options);
512};
513#endif /* GCS_LOGGING_INCLUDED */
This is the interface for the timestamp provider for entries that end up in the Debugger log.
Definition: gcs_logging.h:73
Clock_timestamp_interface()=default
Clock_timestamp_interface & operator=(Clock_timestamp_interface &&other)=delete
Clock_timestamp_interface(Clock_timestamp_interface &&other)=delete
Clock_timestamp_interface(const Clock_timestamp_interface &other)=delete
virtual void get_timestamp_as_c_string(char *buffer, size_t *size)=0
Get the timestamp as c string object.
virtual void get_timestamp_as_string(std::string &str)=0
Get the timestamp as std::string.
Clock_timestamp_interface & operator=(const Clock_timestamp_interface &other)=delete
~Clock_timestamp_interface() override=default
Common interface that is used to define the sink and logger interfaces.
Definition: gcs_logging.h:38
virtual ~Common_interface()=default
Define a virtual destructor as instances of this interface can be polymorphically used.
virtual enum_gcs_error finalize()=0
The purpose of this method is to free any resources used by the objects that implement this interface...
virtual enum_gcs_error initialize()=0
The purpose of this method is to initialize resources used by the objects that implement this interfa...
This class sets up and configures the debugging infrastructure, storing the debugger to be used by th...
Definition: gcs_logging.h:303
static bool force_debug_options(const int64_t debug_options)
Change the current set of debug options by the new debug options expressed as an integer parameter.
Definition: gcs_logging.cc:203
static unsigned int get_number_debug_options()
Get the the number of possible debug options.
Definition: gcs_logging.cc:183
static const std::string m_debug_all
String that represents the GCS_DEBUG_ALL;.
Definition: gcs_logging.h:318
static bool test_debug_options(const int64_t debug_options)
Verify whether any of the debug options are defined.
Definition: gcs_logging.h:355
static std::atomic< std::int64_t > m_debug_options
The debug level enabled which is by default GCS_DEBUG_NONE;.
Definition: gcs_logging.h:308
static bool is_valid_debug_options(const int64_t debug_options)
Check whether the set of debug options is valid or not including GCS_DEBUG_NONE and GCS_DEBUG_ALL.
Definition: gcs_logging.cc:75
static int64_t load_debug_options()
Atomically load information on debug options.
Definition: gcs_logging.h:325
static int64_t get_valid_debug_options()
Get the set of valid debug options excluding GCS_DEBUG_NONE and GCS_DEBUG_ALL.
Definition: gcs_logging.cc:63
static void store_debug_options(int64_t debug_options)
Atomically store information on debug options.
Definition: gcs_logging.h:338
static const std::string m_debug_none
String that represents the GCS_DEBUG_NONE;.
Definition: gcs_logging.h:313
static int64_t get_current_debug_options()
Get the current set of debug options.
Definition: gcs_logging.cc:88
static bool get_debug_options(const std::string &debug_options, int64_t &res_debug_options)
Get the set of debug options passed as parameter as an unsigned integer.
Definition: gcs_logging.cc:134
static bool unset_debug_options(const int64_t debug_options)
Reduce the current set of debug options by disabling the debug options expressed as an integer parame...
Definition: gcs_logging.cc:227
static bool set_debug_options(const int64_t debug_options)
Extend the current set of debug options with new debug options expressed as an integer parameter.
Definition: gcs_logging.cc:187
This class sets up and configures the logging infrastructure, storing the logger to be used by the ap...
Definition: gcs_logging.h:188
static enum_gcs_error finalize()
Free any resource used in the logging system.
Definition: gcs_logging.cc:44
static Logger_interface * m_logger
Definition: gcs_logging.h:190
static Logger_interface * get_logger()
Get a reference to the logger object if there is any.
Definition: gcs_logging.cc:37
static enum_gcs_error initialize(Logger_interface *logger)
Set the logger object and initialize it by invoking its initialization method.
Definition: gcs_logging.cc:39
Logger interface that must be used to define a logger object.
Definition: gcs_logging.h:160
virtual void log_event(const gcs_log_level_t level, const std::string &message)=0
The purpose of this method is to deliver to the logging system any message to be logged.
~Logger_interface() override=default
Define a virtual destructor as instances of this interface can be polymorphically used.
Common sink that may be shared by the logging and debugging systems.
Definition: gcs_logging.h:109
virtual void log_event(const char *message, size_t message_size)=0
The purpose of this method is to effectively log the information.
~Sink_interface() override=default
Define a virtual destructor as instances of this interface can be polymorphically used.
virtual const std::string get_information() const =0
The purpose of this method is to return information on the sink such as its location.
virtual void log_event(const std::string &message)=0
The purpose of this method is to effectively log the information.
static const char *const gcs_log_levels[]
Definition: gcs_logging.h:150
static const char *const gcs_xcom_debug_strings[]
Definition: gcs_logging.h:284
gcs_xcom_debug_option_t
Definition: gcs_logging.h:272
@ XCOM_DEBUG_TRACE
Definition: gcs_logging.h:277
@ GCS_DEBUG_TRACE
Definition: gcs_logging.h:275
@ GCS_INVALID_DEBUG
Definition: gcs_logging.h:280
@ GCS_DEBUG_MSG_FLOW
Definition: gcs_logging.h:278
@ GCS_DEBUG_ALL
Definition: gcs_logging.h:281
@ XCOM_DEBUG_MSG_FLOW
Definition: gcs_logging.h:279
@ GCS_DEBUG_BASIC
Definition: gcs_logging.h:274
@ XCOM_DEBUG_BASIC
Definition: gcs_logging.h:276
@ GCS_DEBUG_NONE
Definition: gcs_logging.h:273
gcs_log_level_t
Definition: gcs_logging.h:143
@ GCS_FATAL
Definition: gcs_logging.h:144
@ GCS_ERROR
Definition: gcs_logging.h:145
@ GCS_WARN
Definition: gcs_logging.h:146
@ GCS_INFO
Definition: gcs_logging.h:147
enum_gcs_error
This enumeration describes errors which can occur during group communication operations.
Definition: gcs_types.h:41
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1077
size_t size(const char *const c)
Definition: base64.h:46
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
static Logger logger
The "top-level" logger used when no connection context is given.
Definition: test_trace_plugin.cc:296