MySQL 26.7.0
Source Code Documentation
ut0log.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2026, 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
25/** @file include/ut0log.h Logging facilities. */
26
27#ifndef ut0log_h
28#define ut0log_h
29
31#include "mysql/my_loglevel.h"
32#include "mysqld_error.h"
33#include "ut0core.h"
34
35/** Get the format string for the logger.
36@param[in] errcode The error code from share/errmsg-*.txt
37@return the message string or nullptr */
38const char *srv_get_server_errmsgs(int errcode);
39
40namespace ib {
41
42/** The class logger is the base class of all the error log related classes.
43It contains a std::ostringstream object. The main purpose of this class is
44to forward operator<< to the underlying std::ostringstream object. Do not
45use this class directly, instead use one of the derived classes. */
46class logger {
47 public:
48 /** Destructor */
49 virtual ~logger();
50
51#ifndef UNIV_NO_ERR_MSGS
52
53 /** Format an error message.
54 @param[in] err Error code from errmsg-*.txt.
55 @param[in] args Variable length argument list */
56 template <class... Args>
57 logger &log(int err, Args &&...args) {
58 ut_a(m_err == ER_IB_MSG_0);
59
60 m_err = err;
61
62 m_oss << msg(err, std::forward<Args>(args)...);
63
64 return (*this);
65 }
66
67#endif /* !UNIV_NO_ERR_MSGS */
68
69 template <typename T>
70 logger &operator<<(const T &rhs) {
71 m_oss << rhs;
72 return (*this);
73 }
74
75 /** Write the given buffer to the internal string stream object.
76 @param[in] buf the buffer contents to log.
77 @param[in] count the length of the buffer buf.
78 @return the output stream into which buffer was written. */
79 std::ostream &write(const char *buf, std::streamsize count) {
80 return (m_oss.write(buf, count));
81 }
82
83 /** Write the given buffer to the internal string stream object.
84 @param[in] buf the buffer contents to log
85 @param[in] count the length of the buffer buf.
86 @return the output stream into which buffer was written. */
87 std::ostream &write(const unsigned char *buf, std::streamsize count) {
88 return (m_oss.write(reinterpret_cast<const char *>(buf), count));
89 }
90
91 public:
92 /** For converting the message into a string. */
94
95#ifndef UNIV_NO_ERR_MSGS
96 /** Error code in errmsg-*.txt */
97 int m_err{};
98
99 /** Error logging level. */
101#endif /* !UNIV_NO_ERR_MSGS */
102
103#ifdef UNIV_HOTBACKUP
104 /** For MEB trace infrastructure. */
105 int m_trace_level{};
106#endif /* UNIV_HOTBACKUP */
107
108 public:
109#ifndef UNIV_NO_ERR_MSGS
110 /** Format an error message.
111 @param[in] err Error code from errmsg-*.txt.
112 @param[in] args Variable length argument list */
113 template <class... Args>
114 static std::string msg(int err, Args &&...args) {
115 const char *fmt = srv_get_server_errmsgs(err);
116
117 char buf[LOG_BUFF_MAX];
118#ifdef UNIV_DEBUG
119 if (get_first_format(fmt) != nullptr) {
120 if (!verify_fmt_match(fmt, std::forward<Args>(args)...)) {
121 fprintf(stderr, "The format '%s' does not match arguments\n", fmt);
122 ut_error;
123 }
124 }
125#endif
126 const auto ret =
127 snprintf(buf, sizeof(buf), fmt, std::forward<Args>(args)...);
128
129 return (ret > 0 && (size_t)ret < sizeof(buf)) ? buf : "";
130 }
131
132 protected:
133 /** Uses LogEvent to report the log entry, using provided message
134 @param[in] msg message to be logged
135 */
136 void log_event(std::string msg);
137
138 /** Constructor.
139 @param[in] level Logging level
140 @param[in] err Error message code. */
141 logger(loglevel level, int err) : m_err(err), m_level(level) {
142 /* Note: Dummy argument to avoid the warning:
143
144 "format not a string literal and no format arguments"
145 "[-Wformat-security]"
146
147 The warning only kicks in if the call is of the form:
148
149 snprintf(buf, sizeof(buf), str);
150 */
151
152 m_oss << msg(err, "");
153 }
154
155 /** Constructor.
156 @param[in] level Logging level
157 @param[in] err Error message code.
158 @param[in] args Variable length argument list */
159 template <class... Args>
160 explicit logger(loglevel level, int err, Args &&...args)
161 : m_err(err), m_level(level) {
162 m_oss << msg(err, std::forward<Args>(args)...);
163 }
164
165 /** Constructor
166 @param[in] level Log error level */
167 explicit logger(loglevel level) : m_err(ER_IB_MSG_0), m_level(level) {}
168
169#endif /* !UNIV_NO_ERR_MSGS */
170};
171
172/** The class info is used to emit informational log messages. It is to be
173used similar to std::cout. But the log messages will be emitted only when
174the dtor is called. The preferred usage of this class is to make use of
175unnamed temporaries as follows:
176
177info() << "The server started successfully.";
178
179In the above usage, the temporary object will be destroyed at the end of the
180statement and hence the log message will be emitted at the end of the
181statement. If a named object is created, then the log message will be emitted
182only when it goes out of scope or destroyed. */
183class info : public logger {
184 public:
185#ifndef UNIV_NO_ERR_MSGS
186
187 /** Default constructor uses ER_IB_MSG_0 */
189
190 /** Constructor.
191 @param[in] err Error code from errmsg-*.txt.
192 @param[in] args Variable length argument list */
193 template <class... Args>
194 explicit info(int err, Args &&...args)
195 : logger(INFORMATION_LEVEL, err, std::forward<Args>(args)...) {}
196#else
197 /** Destructor */
198 ~info() override;
199#endif /* !UNIV_NO_ERR_MSGS */
200};
201
202/** The class warn is used to emit warnings. Refer to the documentation of
203class info for further details. */
204class warn : public logger {
205 public:
206#ifndef UNIV_NO_ERR_MSGS
207 /** Default constructor uses ER_IB_MSG_0 */
209
210 /** Constructor.
211 @param[in] err Error code from errmsg-*.txt.
212 @param[in] args Variable length argument list */
213 template <class... Args>
214 explicit warn(int err, Args &&...args)
215 : logger(WARNING_LEVEL, err, std::forward<Args>(args)...) {}
216
217#else
218 /** Destructor */
219 ~warn() override;
220#endif /* !UNIV_NO_ERR_MSGS */
221};
222
223/** The class error is used to emit error messages. Refer to the
224documentation of class info for further details. */
225class error : public logger {
226 public:
227#ifndef UNIV_NO_ERR_MSGS
228 /** Default constructor uses ER_IB_MSG_0 */
230
231 /** Constructor.
232 @param[in] err Error code from errmsg-*.txt.
233 @param[in] args Variable length argument list */
234 template <class... Args>
235 explicit error(int err, Args &&...args)
236 : logger(ERROR_LEVEL, err, std::forward<Args>(args)...) {}
237
238#else
239 /** Destructor */
240 ~error() override;
241#endif /* !UNIV_NO_ERR_MSGS */
242};
243
244/** The class fatal is used to emit an error message and stop the server
245by crashing it. Use this class when MySQL server needs to be stopped
246immediately. Refer to the documentation of class info for usage details. */
247class fatal : public logger {
248 public:
249#ifndef UNIV_NO_ERR_MSGS
250 /** Default constructor uses ER_IB_MSG_0
251 @param[in] location Location that creates the fatal message.
252*/
253 fatal(ut::Location location) : logger(ERROR_LEVEL), m_location(location) {}
254
255 /** Constructor.
256 @param[in] location Location that creates the fatal message.
257 @param[in] err Error code from errmsg-*.txt.
258 @param[in] args Variable length argument list */
259 template <class... Args>
260 explicit fatal(ut::Location location, int err, Args &&...args)
261 : logger(ERROR_LEVEL, err, std::forward<Args>(args)...),
262 m_location(location) {}
263#else
264 /** Constructor
265 @param[in] location Location that creates the fatal message.
266 */
267 fatal(ut::Location location) : m_location(location) {}
268#endif /* !UNIV_NO_ERR_MSGS */
269
270 /** Destructor. */
271 [[noreturn]] ~fatal() override;
272
273 private:
274 /** Location of the original caller to report to assertion failure */
276};
277
278/** Emit an error message if the given predicate is true, otherwise emit a
279warning message */
280class error_or_warn : public logger {
281 public:
282#ifndef UNIV_NO_ERR_MSGS
283
284 /** Default constructor uses ER_IB_MSG_0
285 @param[in] pred True if it's a warning. */
287
288 /** Constructor.
289 @param[in] pred True if it's a warning.
290 @param[in] err Error code from errmsg-*.txt.
291 @param[in] args Variable length argument list */
292 template <class... Args>
293 explicit error_or_warn(bool pred, int err, Args &&...args)
295 std::forward<Args>(args)...) {}
296
297#endif /* !UNIV_NO_ERR_MSGS */
298};
299
300/** Emit a fatal message if the given predicate is true, otherwise emit a
301error message. */
302class fatal_or_error : public logger {
303 public:
304#ifndef UNIV_NO_ERR_MSGS
305 /** Default constructor uses ER_IB_MSG_0
306 @param[in] fatal true if it's a fatal message
307 @param[in] location Location that creates the fatal */
309 : logger(ERROR_LEVEL), m_fatal(fatal), m_location(location) {}
310
311 /** Constructor.
312 @param[in] fatal true if it's a fatal message
313 @param[in] location Location that creates the fatal
314 @param[in] err Error code from errmsg-*.txt.
315 @param[in] args Variable length argument list */
316 template <class... Args>
317 explicit fatal_or_error(bool fatal, ut::Location location, int err,
318 Args &&...args)
319 : logger(ERROR_LEVEL, err, std::forward<Args>(args)...),
320 m_fatal(fatal),
321 m_location(location) {}
322
323 /** Destructor */
324 ~fatal_or_error() override;
325#else
326 /** Constructor
327 @param[in] location Location that creates the fatal */
328 fatal_or_error(bool fatal, ut::Location location)
329 : m_fatal(fatal), m_location(location) {}
330
331 /** Destructor */
332 ~fatal_or_error() override;
333
334#endif /* !UNIV_NO_ERR_MSGS */
335 private:
336 /** If true then assert after printing an error message. */
337 const bool m_fatal;
338 /** Location of the original caller to report to assertion failure */
340};
341
342#ifdef UNIV_HOTBACKUP
343/** The class trace is used to emit informational log messages. only when
344trace level is set in the MEB code */
345class trace_1 : public logger {
346 public:
347#ifndef UNIV_NO_ERR_MSGS
348 /** Default constructor uses ER_IB_MSG_0 */
349 trace_1() : logger(INFORMATION_LEVEL) { m_trace_level = 1; }
350
351 /** Constructor.
352 @param[in] err Error code from errmsg-*.txt.
353 @param[in] args Variable length argument list */
354 template <class... Args>
355 explicit trace_1(int err, Args &&...args)
356 : logger(INFORMATION_LEVEL, err, std::forward<Args>(args)...) {
357 m_trace_level = 1;
358 }
359
360#else
361 /** Constructor */
362 trace_1();
363#endif /* !UNIV_NO_ERR_MSGS */
364};
365
366/** The class trace_2 is used to emit informational log messages only when
367trace level 2 is set in the MEB code */
368class trace_2 : public logger {
369 public:
370#ifndef UNIV_NO_ERR_MSGS
371 /** Default constructor uses ER_IB_MSG_0 */
372 trace_2() : logger(INFORMATION_LEVEL) { m_trace_level = 2; }
373
374 /** Constructor.
375 @param[in] err Error code from errmsg-*.txt.
376 @param[in] args Variable length argument list */
377 template <class... Args>
378 explicit trace_2(int err, Args &&...args)
379 : logger(INFORMATION_LEVEL, err, std::forward<Args>(args)...) {
380 m_trace_level = 2;
381 }
382#else
383 /** Destructor. */
384 trace_2();
385#endif /* !UNIV_NO_ERR_MSGS */
386};
387
388/** The class trace_3 is used to emit informational log messages only when
389trace level 3 is set in the MEB code */
390class trace_3 : public logger {
391 public:
392#ifndef UNIV_NO_ERR_MSGS
393 /** Default constructor uses ER_IB_MSG_0 */
394 trace_3() : logger(INFORMATION_LEVEL) { m_trace_level = 3; }
395
396 /** Constructor.
397 @param[in] err Error code from errmsg-*.txt.
398 @param[in] args Variable length argument list */
399 template <class... Args>
400 explicit trace_3(int err, Args &&...args)
401 : logger(INFORMATION_LEVEL, err, std::forward<Args>(args)...) {
402 m_trace_level = 3;
403 }
404
405#else
406 /** Destructor. */
407 trace_3();
408#endif /* !UNIV_NO_ERR_MSGS */
409};
410#endif /* UNIV_HOTBACKUP */
411
412/* Convenience functions that ease the usage of logging facilities throughout
413 the code.
414
415 Logging facilities are designed such so that they differentiate between the
416 case when UNIV_NO_ERR_MSGS is defined and when it is not. In particular, end
417 user code must take into account when code is built with UNIV_NO_ERR_MSGS
418 because not the same set of ib::logger constructors will be available in such
419 setting. Design of the logging facility therefore imposes that every possible
420 usage of it in the end user code will result with sprinkling the #ifdefs all
421 around.
422
423 So, what these convenience wrappers do is that they provide somewhat better
424 alternative to the following code, which without the wrapper look like:
425 #ifdef UNIV_NO_ERR_MSGS
426 ib::info();
427 #else
428 ib::info(ER_IB_MSG_1158);
429 #endif
430 << "Some message";
431
432 Same applies for any other ib:: logging facility, e.g.:
433 #ifdef UNIV_NO_ERR_MSGS
434 ib::fatal(UT_LOCATION_HERE)
435 #else
436 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_1157)
437 #endif
438 << "Some message";
439
440 With the convenience wrapper these two usages become:
441 log_info(ER_IB_MSG_1158) << "Some message";
442 log_fatal(UT_LOCATION_HERE, ER_IB_MSG_1157) << "Some message";
443*/
444
445static inline auto log_info() { return ib::info(); }
446static inline auto log_warn() { return ib::warn(); }
447static inline auto log_error() { return ib::error(); }
448static inline auto log_fatal(ut::Location location) {
449 return ib::fatal(location);
450}
451static inline auto log_error_or_warn(bool pred) {
452#ifdef UNIV_NO_ERR_MSGS
453 return ib::error_or_warn();
454#else
455 return ib::error_or_warn(pred);
456#endif
457}
458static inline auto log_fatal_or_error(bool fatal, ut::Location location) {
459 return ib::fatal_or_error(fatal, location);
460}
461
462template <typename... Args>
463static inline auto log_info(int err, Args &&...args) {
464#ifdef UNIV_NO_ERR_MSGS
465 return log_info();
466#else
467 return ib::info(err, std::forward<Args>(args)...);
468#endif
469}
470template <typename... Args>
471static inline auto log_warn(int err, Args &&...args) {
472#ifdef UNIV_NO_ERR_MSGS
473 return log_warn();
474#else
475 return ib::warn(err, std::forward<Args>(args)...);
476#endif
477}
478template <typename... Args>
479static inline auto log_error(int err, Args &&...args) {
480#ifdef UNIV_NO_ERR_MSGS
481 return log_error();
482#else
483 return ib::error(err, std::forward<Args>(args)...);
484#endif
485}
486template <typename... Args>
487static inline auto log_fatal(ut::Location location, int err, Args &&...args) {
488#ifdef UNIV_NO_ERR_MSGS
489 return log_fatal(location);
490#else
491 return ib::fatal(location, err, std::forward<Args>(args)...);
492#endif
493}
494template <typename... Args>
495static inline auto log_error_or_warn(bool pred, int err, Args &&...args) {
496#ifdef UNIV_NO_ERR_MSGS
497 return log_error_or_warn(pred);
498#else
499 return ib::error_or_warn(pred, err, std::forward<Args>(args)...);
500#endif
501}
502template <typename... Args>
503static inline auto log_fatal_or_error(bool fatal, ut::Location location,
504 int err, Args &&...args) {
505#ifdef UNIV_NO_ERR_MSGS
506 return log_fatal_or_error(fatal, location);
507#else
508 return ib::fatal_or_error(fatal, location, err, std::forward<Args>(args)...);
509#endif
510}
511
512#ifdef UNIV_HOTBACKUP
513static inline auto log_trace_1() { return ib::trace_1(); }
514static inline auto log_trace_2() { return ib::trace_2(); }
515static inline auto log_trace_3() { return ib::trace_3(); }
516
517template <typename... Args>
518static inline auto log_trace_1(int err, Args &&...args) {
519#ifdef UNIV_NO_ERR_MSGS
520 return log_trace_1();
521#else
522 return ib::trace_1(err, std::forward<Args>(args)...);
523#endif
524}
525template <typename... Args>
526static inline auto log_trace_2(int err, Args &&...args) {
527#ifdef UNIV_NO_ERR_MSGS
528 return log_trace_2();
529#else
530 return ib::trace_2(err, std::forward<Args>(args)...);
531#endif
532}
533template <typename... Args>
534static inline auto log_trace_3(int err, Args &&...args) {
535#ifdef UNIV_NO_ERR_MSGS
536 return log_trace_3();
537#else
538 return ib::trace_3(err, std::forward<Args>(args)...);
539#endif
540}
541#endif /* UNIV_HOTBACKUP */
542
543} // namespace ib
544
545#endif
Emit an error message if the given predicate is true, otherwise emit a warning message.
Definition: ut0log.h:280
error_or_warn(bool pred)
Default constructor uses ER_IB_MSG_0.
Definition: ut0log.h:286
error_or_warn(bool pred, int err, Args &&...args)
Constructor.
Definition: ut0log.h:293
The class error is used to emit error messages.
Definition: ut0log.h:225
error()
Default constructor uses ER_IB_MSG_0.
Definition: ut0log.h:229
error(int err, Args &&...args)
Constructor.
Definition: ut0log.h:235
Emit a fatal message if the given predicate is true, otherwise emit a error message.
Definition: ut0log.h:302
ut::Location m_location
Location of the original caller to report to assertion failure.
Definition: ut0log.h:339
fatal_or_error(bool fatal, ut::Location location)
Default constructor uses ER_IB_MSG_0.
Definition: ut0log.h:308
fatal_or_error(bool fatal, ut::Location location, int err, Args &&...args)
Constructor.
Definition: ut0log.h:317
const bool m_fatal
If true then assert after printing an error message.
Definition: ut0log.h:337
~fatal_or_error() override
Destructor.
Definition: ut0ut.cc:536
The class fatal is used to emit an error message and stop the server by crashing it.
Definition: ut0log.h:247
~fatal() override
Destructor.
Definition: ut0ut.cc:528
fatal(ut::Location location)
Default constructor uses ER_IB_MSG_0.
Definition: ut0log.h:253
ut::Location m_location
Location of the original caller to report to assertion failure.
Definition: ut0log.h:275
fatal(ut::Location location, int err, Args &&...args)
Constructor.
Definition: ut0log.h:260
The class info is used to emit informational log messages.
Definition: ut0log.h:183
info()
Default constructor uses ER_IB_MSG_0.
Definition: ut0log.h:188
info(int err, Args &&...args)
Constructor.
Definition: ut0log.h:194
The class logger is the base class of all the error log related classes.
Definition: ut0log.h:46
std::ostringstream m_oss
For converting the message into a string.
Definition: ut0log.h:93
std::ostream & write(const unsigned char *buf, std::streamsize count)
Write the given buffer to the internal string stream object.
Definition: ut0log.h:87
static std::string msg(int err, Args &&...args)
Format an error message.
Definition: ut0log.h:114
std::ostream & write(const char *buf, std::streamsize count)
Write the given buffer to the internal string stream object.
Definition: ut0log.h:79
void log_event(std::string msg)
Uses LogEvent to report the log entry, using provided message.
Definition: ut0ut.cc:511
logger(loglevel level)
Constructor.
Definition: ut0log.h:167
loglevel m_level
Error logging level.
Definition: ut0log.h:100
virtual ~logger()
Destructor.
Definition: ut0ut.cc:519
logger & log(int err, Args &&...args)
Format an error message.
Definition: ut0log.h:57
logger(loglevel level, int err, Args &&...args)
Constructor.
Definition: ut0log.h:160
int m_err
Error code in errmsg-*.txt.
Definition: ut0log.h:97
logger(loglevel level, int err)
Constructor.
Definition: ut0log.h:141
logger & operator<<(const T &rhs)
Definition: ut0log.h:70
The class warn is used to emit warnings.
Definition: ut0log.h:204
warn(int err, Args &&...args)
Constructor.
Definition: ut0log.h:214
warn()
Default constructor uses ER_IB_MSG_0.
Definition: ut0log.h:208
static char buf[MAX_BUF]
Definition: conf_to_src.cc:74
#define T
Definition: jit_executor_value.cc:373
#define LOG_BUFF_MAX
advisory.
Definition: log_shared.h:234
Definition of the global "loglevel" enumeration.
loglevel
Definition: my_loglevel.h:41
@ WARNING_LEVEL
Definition: my_loglevel.h:44
@ ERROR_LEVEL
Definition: my_loglevel.h:43
@ INFORMATION_LEVEL
Definition: my_loglevel.h:45
static int count
Definition: myisam_ftdump.cc:45
Definition: buf0block_hint.cc:30
Definition: fil0fil.cc:1392
static bool verify_fmt_match(const char *fmt)
Verifies that the fmt format string does not require any arguments.
Definition: ut0core.h:95
static auto log_error_or_warn(bool pred)
Definition: ut0log.h:451
static const char * get_first_format(const char *fmt)
Finds the first format specifier in fmt format string.
Definition: ut0core.h:83
static auto log_fatal(ut::Location location)
Definition: ut0log.h:448
static auto log_fatal_or_error(bool fatal, ut::Location location)
Definition: ut0log.h:458
static auto log_warn()
Definition: ut0log.h:446
static auto log_info()
Definition: ut0log.h:445
static auto log_error()
Definition: ut0log.h:447
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:943
Define std::hash<Gtid>.
Definition: gtid.h:355
static Logger logger
The "top-level" logger used when no connection context is given.
Definition: test_trace_plugin.cc:296
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2720
Definition: ut0core.h:36
#define ut_error
Abort execution.
Definition: ut0dbg.h:105
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:97
const char * srv_get_server_errmsgs(int errcode)
Get the format string for the logger.
Definition: srv0srv.cc:3256