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