File: ut0log.h

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (550 lines) | stat: -rw-r--r-- 17,594 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/* Copyright (c) 2021, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
*/

/** @file include/ut0log.h Logging facilities. */

#ifndef ut0log_h
#define ut0log_h

#include "my_loglevel.h"
#include "mysql/components/services/log_shared.h"
#include "ut0core.h"

/** Get the format string for the logger.
@param[in]      errcode         The error code from share/errmsg-*.txt
@return the message string or nullptr */
const char *srv_get_server_errmsgs(int errcode);

namespace ib {

/** The class logger is the base class of all the error log related classes.
It contains a std::ostringstream object.  The main purpose of this class is
to forward operator<< to the underlying std::ostringstream object.  Do not
use this class directly, instead use one of the derived classes. */
class logger {
 public:
  /** Destructor */
  virtual ~logger();

#ifndef UNIV_NO_ERR_MSGS

  /** Format an error message.
  @param[in]    err             Error code from errmsg-*.txt.
  @param[in]    args            Variable length argument list */
  template <class... Args>
  logger &log(int err, Args &&...args) {
    ut_a(m_err == ER_IB_MSG_0);

    m_err = err;

    m_oss << msg(err, std::forward<Args>(args)...);

    return (*this);
  }

#endif /* !UNIV_NO_ERR_MSGS */

  template <typename T>
  logger &operator<<(const T &rhs) {
    m_oss << rhs;
    return (*this);
  }

  /** Write the given buffer to the internal string stream object.
  @param[in]    buf             the buffer contents to log.
  @param[in]    count           the length of the buffer buf.
  @return the output stream into which buffer was written. */
  std::ostream &write(const char *buf, std::streamsize count) {
    return (m_oss.write(buf, count));
  }

  /** Write the given buffer to the internal string stream object.
  @param[in]    buf             the buffer contents to log
  @param[in]    count           the length of the buffer buf.
  @return the output stream into which buffer was written. */
  std::ostream &write(const unsigned char *buf, std::streamsize count) {
    return (m_oss.write(reinterpret_cast<const char *>(buf), count));
  }

 public:
  /** For converting the message into a string. */
  std::ostringstream m_oss;

#ifndef UNIV_NO_ERR_MSGS
  /** Error code in errmsg-*.txt */
  int m_err{};

  /** Error logging level. */
  loglevel m_level{INFORMATION_LEVEL};
#endif /* !UNIV_NO_ERR_MSGS */

#ifdef UNIV_HOTBACKUP
  /** For MEB trace infrastructure. */
  int m_trace_level{};
#endif /* UNIV_HOTBACKUP */

 protected:
#ifndef UNIV_NO_ERR_MSGS
  /** Format an error message.
  @param[in]    err     Error code from errmsg-*.txt.
  @param[in]    args    Variable length argument list */
  template <class... Args>
  static std::string msg(int err, Args &&...args) {
    const char *fmt = srv_get_server_errmsgs(err);

    int ret;
    char buf[LOG_BUFF_MAX];
#ifdef UNIV_DEBUG
    if (get_first_format(fmt) != nullptr) {
      if (!verify_fmt_match(fmt, std::forward<Args>(args)...)) {
        fprintf(stderr, "The format '%s' does not match arguments\n", fmt);
        ut_error;
      }
    }
#endif
    ret = snprintf(buf, sizeof(buf), fmt, std::forward<Args>(args)...);

    std::string str;

    if (ret > 0 && (size_t)ret < sizeof(buf)) {
      str.append(buf);
    }

    return (str);
  }

 protected:
  /** Uses LogEvent to report the log entry, using provided message
  @param[in]    msg    message to be logged
  */
  void log_event(std::string msg);

  /** Constructor.
  @param[in]    level           Logging level
  @param[in]    err             Error message code. */
  logger(loglevel level, int err) : m_err(err), m_level(level) {
    /* Note: Dummy argument to avoid the warning:

    "format not a string literal and no format arguments"
    "[-Wformat-security]"

    The warning only kicks in if the call is of the form:

       snprintf(buf, sizeof(buf), str);
    */

    m_oss << msg(err, "");
  }

  /** Constructor.
  @param[in]    level           Logging level
  @param[in]    err             Error message code.
  @param[in]    args            Variable length argument list */
  template <class... Args>
  explicit logger(loglevel level, int err, Args &&...args)
      : m_err(err), m_level(level) {
    m_oss << msg(err, std::forward<Args>(args)...);
  }

  /** Constructor
  @param[in]    level           Log error level */
  explicit logger(loglevel level) : m_err(ER_IB_MSG_0), m_level(level) {}

#endif /* !UNIV_NO_ERR_MSGS */
};

/** The class info is used to emit informational log messages.  It is to be
used similar to std::cout.  But the log messages will be emitted only when
the dtor is called.  The preferred usage of this class is to make use of
unnamed temporaries as follows:

info() << "The server started successfully.";

In the above usage, the temporary object will be destroyed at the end of the
statement and hence the log message will be emitted at the end of the
statement.  If a named object is created, then the log message will be emitted
only when it goes out of scope or destroyed. */
class info : public logger {
 public:
#ifndef UNIV_NO_ERR_MSGS

  /** Default constructor uses ER_IB_MSG_0 */
  info() : logger(INFORMATION_LEVEL) {}

  /** Constructor.
  @param[in]    err             Error code from errmsg-*.txt.
  @param[in]    args            Variable length argument list */
  template <class... Args>
  explicit info(int err, Args &&...args)
      : logger(INFORMATION_LEVEL, err, std::forward<Args>(args)...) {}
#else
  /** Destructor */
  ~info() override;
#endif /* !UNIV_NO_ERR_MSGS */
};

/** The class warn is used to emit warnings.  Refer to the documentation of
class info for further details. */
class warn : public logger {
 public:
#ifndef UNIV_NO_ERR_MSGS
  /** Default constructor uses ER_IB_MSG_0 */
  warn() : logger(WARNING_LEVEL) {}

  /** Constructor.
  @param[in]    err             Error code from errmsg-*.txt.
  @param[in]    args            Variable length argument list */
  template <class... Args>
  explicit warn(int err, Args &&...args)
      : logger(WARNING_LEVEL, err, std::forward<Args>(args)...) {}

#else
  /** Destructor */
  ~warn() override;
#endif /* !UNIV_NO_ERR_MSGS */
};

/** The class error is used to emit error messages.  Refer to the
documentation of class info for further details. */
class error : public logger {
 public:
#ifndef UNIV_NO_ERR_MSGS
  /** Default constructor uses ER_IB_MSG_0 */
  error() : logger(ERROR_LEVEL) {}

  /** Constructor.
  @param[in]    err             Error code from errmsg-*.txt.
  @param[in]    args            Variable length argument list */
  template <class... Args>
  explicit error(int err, Args &&...args)
      : logger(ERROR_LEVEL, err, std::forward<Args>(args)...) {}

#else
  /** Destructor */
  ~error() override;
#endif /* !UNIV_NO_ERR_MSGS */
};

/** The class fatal is used to emit an error message and stop the server
by crashing it.  Use this class when MySQL server needs to be stopped
immediately.  Refer to the documentation of class info for usage details. */
class fatal : public logger {
 public:
#ifndef UNIV_NO_ERR_MSGS
  /** Default constructor uses ER_IB_MSG_0
  @param[in]    location                Location that creates the fatal message.
*/
  fatal(ut::Location location) : logger(ERROR_LEVEL), m_location(location) {}

  /** Constructor.
  @param[in]    location                Location that creates the fatal message.
  @param[in]    err             Error code from errmsg-*.txt.
  @param[in]    args            Variable length argument list */
  template <class... Args>
  explicit fatal(ut::Location location, int err, Args &&...args)
      : logger(ERROR_LEVEL, err, std::forward<Args>(args)...),
        m_location(location) {}
#else
  /** Constructor
  @param[in]    location                Location that creates the fatal message.
  */
  fatal(ut::Location location) : m_location(location) {}
#endif /* !UNIV_NO_ERR_MSGS */

  /** Destructor. */
  ~fatal() override;

 private:
  /** Location of the original caller to report to assertion failure */
  ut::Location m_location;
};

/** Emit an error message if the given predicate is true, otherwise emit a
warning message */
class error_or_warn : public logger {
 public:
#ifndef UNIV_NO_ERR_MSGS

  /** Default constructor uses ER_IB_MSG_0
  @param[in]    pred            True if it's a warning. */
  error_or_warn(bool pred) : logger(pred ? ERROR_LEVEL : WARNING_LEVEL) {}

  /** Constructor.
  @param[in]    pred            True if it's a warning.
  @param[in]    err             Error code from errmsg-*.txt.
  @param[in]    args            Variable length argument list */
  template <class... Args>
  explicit error_or_warn(bool pred, int err, Args &&...args)
      : logger(pred ? ERROR_LEVEL : WARNING_LEVEL, err,
               std::forward<Args>(args)...) {}

#endif /* !UNIV_NO_ERR_MSGS */
};

/** Emit a fatal message if the given predicate is true, otherwise emit a
error message. */
class fatal_or_error : public logger {
 public:
#ifndef UNIV_NO_ERR_MSGS
  /** Default constructor uses ER_IB_MSG_0
  @param[in]    fatal           true if it's a fatal message
  @param[in] location Location that creates the fatal */
  fatal_or_error(bool fatal, ut::Location location)
      : logger(ERROR_LEVEL), m_fatal(fatal), m_location(location) {}

  /** Constructor.
  @param[in]    fatal           true if it's a fatal message
  @param[in] location Location that creates the fatal
  @param[in]    err             Error code from errmsg-*.txt.
  @param[in]    args            Variable length argument list */
  template <class... Args>
  explicit fatal_or_error(bool fatal, ut::Location location, int err,
                          Args &&...args)
      : logger(ERROR_LEVEL, err, std::forward<Args>(args)...),
        m_fatal(fatal),
        m_location(location) {}

  /** Destructor */
  ~fatal_or_error() override;
#else
  /** Constructor
  @param[in] location Location that creates the fatal */
  fatal_or_error(bool fatal, ut::Location location)
      : m_fatal(fatal), m_location(location) {}

  /** Destructor */
  ~fatal_or_error() override;

#endif /* !UNIV_NO_ERR_MSGS */
 private:
  /** If true then assert after printing an error message. */
  const bool m_fatal;
  /** Location of the original caller to report to assertion failure */
  ut::Location m_location;
};

#ifdef UNIV_HOTBACKUP
/**  The class trace is used to emit informational log messages. only when
trace level is set in the MEB code */
class trace_1 : public logger {
 public:
#ifndef UNIV_NO_ERR_MSGS
  /** Default constructor uses ER_IB_MSG_0 */
  trace_1() : logger(INFORMATION_LEVEL) { m_trace_level = 1; }

  /** Constructor.
  @param[in]    err             Error code from errmsg-*.txt.
  @param[in]    args            Variable length argument list */
  template <class... Args>
  explicit trace_1(int err, Args &&...args)
      : logger(INFORMATION_LEVEL, err, std::forward<Args>(args)...) {
    m_trace_level = 1;
  }

#else
  /** Constructor */
  trace_1();
#endif /* !UNIV_NO_ERR_MSGS */
};

/**  The class trace_2 is used to emit informational log messages only when
trace level 2 is set in the MEB code */
class trace_2 : public logger {
 public:
#ifndef UNIV_NO_ERR_MSGS
  /** Default constructor uses ER_IB_MSG_0 */
  trace_2() : logger(INFORMATION_LEVEL) { m_trace_level = 2; }

  /** Constructor.
  @param[in]    err             Error code from errmsg-*.txt.
  @param[in]    args            Variable length argument list */
  template <class... Args>
  explicit trace_2(int err, Args &&...args)
      : logger(INFORMATION_LEVEL, err, std::forward<Args>(args)...) {
    m_trace_level = 2;
  }
#else
  /** Destructor. */
  trace_2();
#endif /* !UNIV_NO_ERR_MSGS */
};

/**  The class trace_3 is used to emit informational log messages only when
trace level 3 is set in the MEB code */
class trace_3 : public logger {
 public:
#ifndef UNIV_NO_ERR_MSGS
  /** Default constructor uses ER_IB_MSG_0 */
  trace_3() : logger(INFORMATION_LEVEL) { m_trace_level = 3; }

  /** Constructor.
  @param[in]    err             Error code from errmsg-*.txt.
  @param[in]    args            Variable length argument list */
  template <class... Args>
  explicit trace_3(int err, Args &&...args)
      : logger(INFORMATION_LEVEL, err, std::forward<Args>(args)...) {
    m_trace_level = 3;
  }

#else
  /** Destructor. */
  trace_3();
#endif /* !UNIV_NO_ERR_MSGS */
};
#endif /* UNIV_HOTBACKUP */

/* Convenience functions that ease the usage of logging facilities throughout
   the code.

   Logging facilities are designed such so that they differentiate between the
   case when UNIV_NO_ERR_MSGS is defined and when it is not. In particular, end
   user code must take into account when code is built with  UNIV_NO_ERR_MSGS
   because not the same set of ib::logger constructors will be available in such
   setting. Design of the logging facility therefore imposes that every possible
   usage of it in the end user code will result with sprinkling the #ifdefs all
   around.

   So, what these convenience wrappers do is that they provide somewhat better
   alternative to the following code, which without the wrapper look like:
     #ifdef UNIV_NO_ERR_MSGS
       ib::info();
     #else
       ib::info(ER_IB_MSG_1158);
     #endif
         << "Some message";

   Same applies for any other ib:: logging facility, e.g.:
     #ifdef UNIV_NO_ERR_MSGS
       ib::fatal(UT_LOCATION_HERE)
     #else
       ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_1157)
     #endif
         << "Some message";

   With the convenience wrapper these two usages become:
     log_info(ER_IB_MSG_1158) << "Some message";
     log_fatal(UT_LOCATION_HERE, ER_IB_MSG_1157) << "Some message";
*/

static inline auto log_info() { return ib::info(); }
static inline auto log_warn() { return ib::warn(); }
static inline auto log_error() { return ib::error(); }
static inline auto log_fatal(ut::Location location) {
  return ib::fatal(location);
}
static inline auto log_error_or_warn(bool pred) {
#ifdef UNIV_NO_ERR_MSGS
  return ib::error_or_warn();
#else
  return ib::error_or_warn(pred);
#endif
}
static inline auto log_fatal_or_error(bool fatal, ut::Location location) {
  return ib::fatal_or_error(fatal, location);
}

template <typename... Args>
static inline auto log_info(int err, Args &&...args) {
#ifdef UNIV_NO_ERR_MSGS
  return log_info();
#else
  return ib::info(err, std::forward<Args>(args)...);
#endif
}
template <typename... Args>
static inline auto log_warn(int err, Args &&...args) {
#ifdef UNIV_NO_ERR_MSGS
  return log_warn();
#else
  return ib::warn(err, std::forward<Args>(args)...);
#endif
}
template <typename... Args>
static inline auto log_error(int err, Args &&...args) {
#ifdef UNIV_NO_ERR_MSGS
  return log_error();
#else
  return ib::error(err, std::forward<Args>(args)...);
#endif
}
template <typename... Args>
static inline auto log_fatal(ut::Location location, int err, Args &&...args) {
#ifdef UNIV_NO_ERR_MSGS
  return log_fatal(location);
#else
  return ib::fatal(location, err, std::forward<Args>(args)...);
#endif
}
template <typename... Args>
static inline auto log_error_or_warn(bool pred, int err, Args &&...args) {
#ifdef UNIV_NO_ERR_MSGS
  return log_error_or_warn(pred);
#else
  return ib::error_or_warn(pred, err, std::forward<Args>(args)...);
#endif
}
template <typename... Args>
static inline auto log_fatal_or_error(bool fatal, ut::Location location,
                                      int err, Args &&...args) {
#ifdef UNIV_NO_ERR_MSGS
  return log_fatal_or_error(fatal, location);
#else
  return ib::fatal_or_error(fatal, location, err, std::forward<Args>(args)...);
#endif
}

#ifdef UNIV_HOTBACKUP
static inline auto log_trace_1() { return ib::trace_1(); }
static inline auto log_trace_2() { return ib::trace_2(); }
static inline auto log_trace_3() { return ib::trace_3(); }

template <typename... Args>
static inline auto log_trace_1(int err, Args &&...args) {
#ifdef UNIV_NO_ERR_MSGS
  return log_trace_1();
#else
  return ib::trace_1(err, std::forward<Args>(args)...);
#endif
}
template <typename... Args>
static inline auto log_trace_2(int err, Args &&...args) {
#ifdef UNIV_NO_ERR_MSGS
  return log_trace_2();
#else
  return ib::trace_2(err, std::forward<Args>(args)...);
#endif
}
template <typename... Args>
static inline auto log_trace_3(int err, Args &&...args) {
#ifdef UNIV_NO_ERR_MSGS
  return log_trace_3();
#else
  return ib::trace_3(err, std::forward<Args>(args)...);
#endif
}
#endif /* UNIV_HOTBACKUP */

}  // namespace ib

#endif