MySQL  8.0.12
Source Code Documentation
ut0ut.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 1994, 2018, Oracle and/or its affiliates. All Rights Reserved.
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24  The lines above are intentionally left blank
25 */
26 
27 /** @file include/ut0ut.h
28  Various utilities
29 
30  Created 1/20/1994 Heikki Tuuri
31  ***********************************************************************/
32 
33 /**************************************************/ /**
34  @page PAGE_INNODB_UTILS Innodb utils
35 
36  Useful data structures:
37  - @ref Link_buf - to track concurrent operations
38  - @ref Sharded_rw_lock - sharded rw-lock (very fast s-lock, slow x-lock)
39 
40  *******************************************************/
41 
42 #ifndef ut0ut_h
43 #define ut0ut_h
44 
45 /* Do not include univ.i because univ.i includes this. */
46 
47 #include <string.h>
48 #include <algorithm>
49 #include <iterator>
50 #include <ostream>
51 #include <sstream>
52 
53 #include "db0err.h"
54 
55 #ifndef UNIV_HOTBACKUP
56 #include "os0atomic.h"
57 #endif /* !UNIV_HOTBACKUP */
58 
59 #include <time.h>
60 
61 #include <ctype.h>
62 
63 #include <stdarg.h>
64 #include "ut/ut.h"
65 #include "ut0dbg.h"
66 
67 #ifndef UNIV_NO_ERR_MSGS
69 #include "mysqld_error.h"
70 #include "sql/derror.h"
71 #endif /* !UNIV_NO_ERR_MSGS */
72 
73 /** Index name prefix in fast index creation, as a string constant */
74 #define TEMP_INDEX_PREFIX_STR "\377"
75 
76 /** Get the format string for the logger.
77 @param[in] errcode The error code from share/errmsg-*.txt
78 @return the message string or nullptr */
79 const char *srv_get_server_errmsgs(int errcode);
80 
81 /** Time stamp */
82 typedef time_t ib_time_t;
83 
84 #ifndef UNIV_HOTBACKUP
85 #if defined(HAVE_PAUSE_INSTRUCTION)
86 /* According to the gcc info page, asm volatile means that the
87 instruction has important side-effects and must not be removed.
88 Also asm volatile may trigger a memory barrier (spilling all registers
89 to memory). */
90 #ifdef __SUNPRO_CC
91 #define UT_RELAX_CPU() asm("pause")
92 #else
93 #define UT_RELAX_CPU() __asm__ __volatile__("pause")
94 #endif /* __SUNPRO_CC */
95 
96 #elif defined(HAVE_FAKE_PAUSE_INSTRUCTION)
97 #define UT_RELAX_CPU() __asm__ __volatile__("rep; nop")
98 #elif defined _WIN32
99 /* In the Win32 API, the x86 PAUSE instruction is executed by calling
100 the YieldProcessor macro defined in WinNT.h. It is a CPU architecture-
101 independent way by using YieldProcessor. */
102 #define UT_RELAX_CPU() YieldProcessor()
103 #else
104 #define UT_RELAX_CPU() __asm__ __volatile__("" ::: "memory")
105 #endif
106 
107 #if defined(HAVE_HMT_PRIORITY_INSTRUCTION)
108 #define UT_LOW_PRIORITY_CPU() __asm__ __volatile__("or 1,1,1")
109 #define UT_RESUME_PRIORITY_CPU() __asm__ __volatile__("or 2,2,2")
110 #else
111 #define UT_LOW_PRIORITY_CPU() ((void)0)
112 #define UT_RESUME_PRIORITY_CPU() ((void)0)
113 #endif
114 
115 /** Delays execution for at most max_wait_us microseconds or returns earlier
116  if cond becomes true.
117  @param cond in: condition to wait for; evaluated every 2 ms
118  @param max_wait_us in: maximum delay to wait, in microseconds */
119 #define UT_WAIT_FOR(cond, max_wait_us) \
120  do { \
121  uintmax_t start_us; \
122  start_us = ut_time_us(NULL); \
123  while (!(cond) && ut_time_us(NULL) - start_us < (max_wait_us)) { \
124  os_thread_sleep(2000 /* 2 ms */); \
125  } \
126  } while (0)
127 #else /* !UNIV_HOTBACKUP */
128 #define UT_RELAX_CPU() /* No op */
129 #endif /* !UNIV_HOTBACKUP */
130 
131 #define ut_max std::max
132 #define ut_min std::min
133 
134 /** Calculate the minimum of two pairs.
135 @param[out] min_hi MSB of the minimum pair
136 @param[out] min_lo LSB of the minimum pair
137 @param[in] a_hi MSB of the first pair
138 @param[in] a_lo LSB of the first pair
139 @param[in] b_hi MSB of the second pair
140 @param[in] b_lo LSB of the second pair */
141 UNIV_INLINE
142 void ut_pair_min(ulint *min_hi, ulint *min_lo, ulint a_hi, ulint a_lo,
143  ulint b_hi, ulint b_lo);
144 
145 /** Compares two ulints.
146 @param[in] a ulint
147 @param[in] b ulint
148 @return 1 if a > b, 0 if a == b, -1 if a < b */
149 UNIV_INLINE
150 int ut_ulint_cmp(ulint a, ulint b);
151 
152 /** Compare two pairs of integers.
153 @param[in] a_h more significant part of first pair
154 @param[in] a_l less significant part of first pair
155 @param[in] b_h more significant part of second pair
156 @param[in] b_l less significant part of second pair
157 @return comparison result of (a_h,a_l) and (b_h,b_l)
158 @retval -1 if (a_h,a_l) is less than (b_h,b_l)
159 @retval 0 if (a_h,a_l) is equal to (b_h,b_l)
160 @retval 1 if (a_h,a_l) is greater than (b_h,b_l) */
161 UNIV_INLINE
162 int ut_pair_cmp(ulint a_h, ulint a_l, ulint b_h, ulint b_l)
163  MY_ATTRIBUTE((warn_unused_result));
164 
165 /** Calculates fast the remainder of n/m when m is a power of two.
166  @param n in: numerator
167  @param m in: denominator, must be a power of two
168  @return the remainder of n/m */
169 #define ut_2pow_remainder(n, m) ((n) & ((m)-1))
170 /** Calculates the biggest multiple of m that is not bigger than n
171  when m is a power of two. In other words, rounds n down to m * k.
172  @param n in: number to round down
173  @param m in: alignment, must be a power of two
174  @return n rounded down to the biggest possible integer multiple of m */
175 #define ut_2pow_round(n, m) ((n) & ~((m)-1))
176 /** Align a number down to a multiple of a power of two.
177 @param n in: number to round down
178 @param m in: alignment, must be a power of two
179 @return n rounded down to the biggest possible integer multiple of m */
180 #define ut_calc_align_down(n, m) ut_2pow_round(n, m)
181 /** Calculates the smallest multiple of m that is not smaller than n
182  when m is a power of two. In other words, rounds n up to m * k.
183  @param n in: number to round up
184  @param m in: alignment, must be a power of two
185  @return n rounded up to the smallest possible integer multiple of m */
186 #define ut_calc_align(n, m) (((n) + ((m)-1)) & ~((m)-1))
187 /** Calculates fast the 2-logarithm of a number, rounded upward to an
188  integer.
189  @return logarithm in the base 2, rounded upward */
190 UNIV_INLINE
191 ulint ut_2_log(ulint n); /*!< in: number */
192 
193 /** Calculates 2 to power n.
194 @param[in] n power of 2
195 @return 2 to power n */
196 UNIV_INLINE
197 uint32_t ut_2_exp(uint32_t n);
198 
199 /** Calculates fast the number rounded up to the nearest power of 2.
200  @return first power of 2 which is >= n */
201 ulint ut_2_power_up(ulint n) /*!< in: number != 0 */
202  MY_ATTRIBUTE((const));
203 
204 /** Determine how many bytes (groups of 8 bits) are needed to
205 store the given number of bits.
206 @param b in: bits
207 @return number of bytes (octets) needed to represent b */
208 #define UT_BITS_IN_BYTES(b) (((b) + 7) / 8)
209 
210 /** Returns system time. We do not specify the format of the time returned:
211  the only way to manipulate it is to use the function ut_difftime.
212  @return system time */
213 ib_time_t ut_time(void);
214 /** Returns system time.
215  Upon successful completion, the value 0 is returned; otherwise the
216  value -1 is returned and the global variable errno is set to indicate the
217  error.
218  @return 0 on success, -1 otherwise */
219 int ut_usectime(ulint *sec, /*!< out: seconds since the Epoch */
220  ulint *ms); /*!< out: microseconds since the Epoch+*sec */
221 
222 /** Returns the number of microseconds since epoch. Similar to
223  time(3), the return value is also stored in *tloc, provided
224  that tloc is non-NULL.
225  @return us since epoch */
226 uintmax_t ut_time_us(uintmax_t *tloc); /*!< out: us since epoch, if non-NULL */
227 /** Returns the number of milliseconds since some epoch. The
228  value may wrap around. It should only be used for heuristic
229  purposes.
230  @return ms since epoch */
231 ulint ut_time_ms(void);
232 
233 #ifdef _WIN32
234 /** Initialise highest available time resolution API on Windows
235  @return false if all OK else true */
236 bool ut_win_init_time();
237 
238 #endif /* _WIN32 */
239 
240 /** Returns the number of milliseconds since some epoch. The
241  value may wrap around. It should only be used for heuristic
242  purposes.
243  @return ms since epoch */
244 ulint ut_time_ms(void);
245 
246 /** Returns the difference of two times in seconds.
247  @return time2 - time1 expressed in seconds */
248 double ut_difftime(ib_time_t time2, /*!< in: time */
249  ib_time_t time1); /*!< in: time */
250 
251 /** Determines if a number is zero or a power of two.
252 @param[in] n number
253 @return nonzero if n is zero or a power of two; zero otherwise */
254 #define ut_is_2pow(n) UNIV_LIKELY(!((n) & ((n)-1)))
255 
256 /** Functor that compares two C strings. Can be used as a comparator for
257 e.g. std::map that uses char* as keys. */
259  bool operator()(const char *a, const char *b) const {
260  return (strcmp(a, b) < 0);
261  }
262 };
263 
264 /** Runs an idle loop on CPU. The argument gives the desired delay
265  in microseconds on 100 MHz Pentium + Visual C++.
266  @return dummy value */
267 ulint ut_delay(
268  ulint delay); /*!< in: delay in microseconds on 100 MHz Pentium */
269 
270 /* Forward declaration of transaction handle */
271 struct trx_t;
272 
273 /** Get a fixed-length string, quoted as an SQL identifier.
274 If the string contains a slash '/', the string will be
275 output as two identifiers separated by a period (.),
276 as in SQL database_name.identifier.
277  @param [in] trx transaction (NULL=no quotes).
278  @param [in] name table name.
279  @retval String quoted as an SQL identifier.
280 */
281 std::string ut_get_name(const trx_t *trx, const char *name);
282 
283 /** Outputs a fixed-length string, quoted as an SQL identifier.
284  If the string contains a slash '/', the string will be
285  output as two identifiers separated by a period (.),
286  as in SQL database_name.identifier. */
287 void ut_print_name(FILE *f, /*!< in: output stream */
288  const trx_t *trx, /*!< in: transaction */
289  const char *name); /*!< in: table name to print */
290 
291 /** Format a table name, quoted as an SQL identifier.
292 If the name contains a slash '/', the result will contain two
293 identifiers separated by a period (.), as in SQL
294 database_name.table_name.
295 @see table_name_t
296 @param[in] name table or index name
297 @param[out] formatted formatted result, will be NUL-terminated
298 @param[in] formatted_size size of the buffer in bytes
299 @return pointer to 'formatted' */
300 char *ut_format_name(const char *name, char *formatted, ulint formatted_size);
301 
302 /** Catenate files. */
303 void ut_copy_file(FILE *dest, /*!< in: output file */
304  FILE *src); /*!< in: input file to be appended to output */
305 
306 #ifdef _WIN32
307 /** A substitute for vsnprintf(3), formatted output conversion into
308  a limited buffer. Note: this function DOES NOT return the number of
309  characters that would have been printed if the buffer was unlimited because
310  VC's _vsnprintf() returns -1 in this case and we would need to call
311  _vscprintf() in addition to estimate that but we would need another copy
312  of "ap" for that and VC does not provide va_copy(). */
313 void ut_vsnprintf(char *str, /*!< out: string */
314  size_t size, /*!< in: str size */
315  const char *fmt, /*!< in: format */
316  va_list ap); /*!< in: format values */
317 #else
318 /** A wrapper for vsnprintf(3), formatted output conversion into
319  a limited buffer. Note: this function DOES NOT return the number of
320  characters that would have been printed if the buffer was unlimited because
321  VC's _vsnprintf() returns -1 in this case and we would need to call
322  _vscprintf() in addition to estimate that but we would need another copy
323  of "ap" for that and VC does not provide va_copy(). */
324 #define ut_vsnprintf(buf, size, fmt, ap) ((void)vsnprintf(buf, size, fmt, ap))
325 #endif /* _WIN32 */
326 
327 /** Convert an error number to a human readable text message. The
328  returned string is static and should not be freed or modified.
329  @return string, describing the error */
330 const char *ut_strerr(dberr_t num); /*!< in: error number */
331 
332 #ifdef UNIV_PFS_MEMORY
333 
334 /** Extract the basename of a file without its extension.
335 For example, extract "foo0bar" out of "/path/to/foo0bar.cc".
336 @param[in] file file path, e.g. "/path/to/foo0bar.cc"
337 @param[out] base result, e.g. "foo0bar"
338 @param[in] base_size size of the output buffer 'base', if there
339 is not enough space, then the result will be truncated, but always
340 '\0'-terminated
341 @return number of characters that would have been printed if the size
342 were unlimited (not including the final ‘\0’) */
343 size_t ut_basename_noext(const char *file, char *base, size_t base_size);
344 
345 #endif /* UNIV_PFS_MEMORY */
346 
347 namespace ib {
348 
349 /** This is a wrapper class, used to print any unsigned integer type
350 in hexadecimal format. The main purpose of this data type is to
351 overload the global operator<<, so that we can print the given
352 wrapper value in hex. */
353 struct hex {
354  explicit hex(uintmax_t t) : m_val(t) {}
355  const uintmax_t m_val;
356 };
357 
358 /** This is an overload of the global operator<< for the user defined type
359 ib::hex. The unsigned value held in the ib::hex wrapper class will be printed
360 into the given output stream in hexadecimal format.
361 @param[in,out] lhs the output stream into which rhs is written.
362 @param[in] rhs the object to be written into lhs.
363 @retval reference to the output stream. */
364 inline std::ostream &operator<<(std::ostream &lhs, const hex &rhs) {
365  std::ios_base::fmtflags ff = lhs.flags();
366  lhs << std::showbase << std::hex << rhs.m_val;
367  lhs.setf(ff);
368  return (lhs);
369 }
370 
371 /** The class logger is the base class of all the error log related classes.
372 It contains a std::ostringstream object. The main purpose of this class is
373 to forward operator<< to the underlying std::ostringstream object. Do not
374 use this class directly, instead use one of the derived classes. */
375 class logger {
376  public:
377  /** Destructor */
378  virtual ~logger();
379 
380 #ifndef UNIV_NO_ERR_MSGS
381 
382  /** Format an error message.
383  @param[in] err Error code from errmsg-*.txt.
384  @param[in] args Variable length argument list */
385  template <class... Args>
386  logger &log(int err, Args &&... args) {
387  ut_a(m_err == ER_IB_MSG_0);
388 
389  m_err = err;
390 
391  m_oss << msg(err, std::forward<Args>(args)...);
392 
393  return (*this);
394  }
395 
396 #endif /* !UNIV_NO_ERR_MSGS */
397 
398  template <typename T>
399  logger &operator<<(const T &rhs) {
400  m_oss << rhs;
401  return (*this);
402  }
403 
404  /** Write the given buffer to the internal string stream object.
405  @param[in] buf the buffer contents to log.
406  @param[in] count the length of the buffer buf.
407  @return the output stream into which buffer was written. */
408  std::ostream &write(const char *buf, std::streamsize count) {
409  return (m_oss.write(buf, count));
410  }
411 
412  /** Write the given buffer to the internal string stream object.
413  @param[in] buf the buffer contents to log
414  @param[in] count the length of the buffer buf.
415  @return the output stream into which buffer was written. */
416  std::ostream &write(const byte *buf, std::streamsize count) {
417  return (m_oss.write(reinterpret_cast<const char *>(buf), count));
418  }
419 
420  public:
421  /** For converting the message into a string. */
422  std::ostringstream m_oss;
423 
424 #ifndef UNIV_NO_ERR_MSGS
425  /** Error code in errmsg-*.txt */
426  int m_err{};
427 
428  /** Error logging level. */
430 #endif /* !UNIV_NO_ERR_MSGS */
431 
432 #ifdef UNIV_HOTBACKUP
433  /** For MEB trace infrastructure. */
434  int m_trace_level{};
435 #endif /* UNIV_HOTBACKUP */
436 
437  protected:
438 #ifndef UNIV_NO_ERR_MSGS
439  /** String prefix for all log messages. */
440 #if defined(__SUNPRO_CC)
441  static const char *PREFIX;
442 #else
443  /** String prefix for all log messages. */
444  static constexpr const char *PREFIX = "InnoDB: ";
445 #endif /* __SUNPRO_CC */
446 
447  /** Format an error message.
448  @param[in] err Error code from errmsg-*.txt.
449  @param[in] args Variable length argument list */
450  template <class... Args>
451  static std::string msg(int err, Args &&... args) {
452  const char *fmt = srv_get_server_errmsgs(err);
453 
454  int ret;
455  char buf[LOG_BUFF_MAX];
456 
457  ret = snprintf(buf, sizeof(buf), fmt, std::forward<Args>(args)...);
458 
459  std::string str;
460 
461  if (ret > 0 && (size_t)ret < sizeof(buf)) {
462  str.append(buf);
463  }
464 
465  return (str);
466  }
467 
468  protected:
469  /** Constructor.
470  @param[in] level Logging level
471  @param[in] err Error message code. */
472  logger(loglevel level, int err) : m_err(err), m_level(level) {
473  /* Note: Dummy argument to avoid the warning:
474 
475  "format not a string literal and no format arguments"
476  "[-Wformat-security]"
477 
478  The warning only kicks in if the call is of the form:
479 
480  snprintf(buf, sizeof(buf), str);
481  */
482 
483  m_oss << PREFIX;
484  m_oss << msg(err, "");
485  }
486 
487  /** Constructor.
488  @param[in] level Logging level
489  @param[in] err Error message code.
490  @param[in] args Variable length argument list */
491  template <class... Args>
492  explicit logger(loglevel level, int err, Args &&... args)
493  : m_err(err), m_level(level) {
494  m_oss << PREFIX;
495  m_oss << msg(err, std::forward<Args>(args)...);
496  }
497 
498  /** Constructor
499  @param[in] level Log error level */
500  explicit logger(loglevel level) : m_err(ER_IB_MSG_0), m_level(level) {
501  m_oss << PREFIX;
502  }
503 
504 #endif /* !UNIV_NO_ERR_MSGS */
505 };
506 
507 /** The class info is used to emit informational log messages. It is to be
508 used similar to std::cout. But the log messages will be emitted only when
509 the dtor is called. The preferred usage of this class is to make use of
510 unnamed temporaries as follows:
511 
512 info() << "The server started successfully.";
513 
514 In the above usage, the temporary object will be destroyed at the end of the
515 statement and hence the log message will be emitted at the end of the
516 statement. If a named object is created, then the log message will be emitted
517 only when it goes out of scope or destroyed. */
518 class info : public logger {
519  public:
520 #ifndef UNIV_NO_ERR_MSGS
521 
522  /** Default constructor uses ER_IB_MSG_0 */
524 
525  /** Constructor.
526  @param[in] err Error code from errmsg-*.txt.
527  @param[in] args Variable length argument list */
528  template <class... Args>
529  explicit info(int err, Args &&... args)
530  : logger(INFORMATION_LEVEL, err, std::forward<Args>(args)...) {}
531 #else
532  /** Destructor */
533  ~info();
534 #endif /* !UNIV_NO_ERR_MSGS */
535 };
536 
537 /** The class warn is used to emit warnings. Refer to the documentation of
538 class info for further details. */
539 class warn : public logger {
540  public:
541 #ifndef UNIV_NO_ERR_MSGS
542  /** Default constructor uses ER_IB_MSG_0 */
544 
545  /** Constructor.
546  @param[in] err Error code from errmsg-*.txt.
547  @param[in] args Variable length argument list */
548  template <class... Args>
549  explicit warn(int err, Args &&... args)
550  : logger(WARNING_LEVEL, err, std::forward<Args>(args)...) {}
551 
552 #else
553  /** Destructor */
554  ~warn();
555 #endif /* !UNIV_NO_ERR_MSGS */
556 };
557 
558 /** The class error is used to emit error messages. Refer to the
559 documentation of class info for further details. */
560 class error : public logger {
561  public:
562 #ifndef UNIV_NO_ERR_MSGS
563  /** Default constructor uses ER_IB_MSG_0 */
565 
566  /** Constructor.
567  @param[in] err Error code from errmsg-*.txt.
568  @param[in] args Variable length argument list */
569  template <class... Args>
570  explicit error(int err, Args &&... args)
571  : logger(ERROR_LEVEL, err, std::forward<Args>(args)...) {}
572 
573 #else
574  /** Destructor */
575  ~error();
576 #endif /* !UNIV_NO_ERR_MSGS */
577 };
578 
579 /** The class fatal is used to emit an error message and stop the server
580 by crashing it. Use this class when MySQL server needs to be stopped
581 immediately. Refer to the documentation of class info for usage details. */
582 class fatal : public logger {
583  public:
584 #ifndef UNIV_NO_ERR_MSGS
585  /** Default constructor uses ER_IB_MSG_0 */
586  fatal() : logger(ERROR_LEVEL) { m_oss << "[FATAL] "; }
587 
588  /** Default constructor uses ER_IB_MSG_0 */
589  explicit fatal(int err) : logger(ERROR_LEVEL) {
590  m_oss << "[FATAL] ";
591 
592  m_oss << msg(err, "");
593  }
594 
595  /** Constructor.
596  @param[in] err Error code from errmsg-*.txt.
597  @param[in] args Variable length argument list */
598  template <class... Args>
599  explicit fatal(int err, Args &&... args) : logger(ERROR_LEVEL, err) {
600  m_oss << "[FATAL]";
601 
602  m_oss << msg(err, std::forward<Args>(args)...);
603  }
604 
605  /** Destructor. */
606  virtual ~fatal();
607 #else
608  /** Destructor. */
609  ~fatal();
610 #endif /* !UNIV_NO_ERR_MSGS */
611 };
612 
613 /** Emit an error message if the given predicate is true, otherwise emit a
614 warning message */
615 class error_or_warn : public logger {
616  public:
617 #ifndef UNIV_NO_ERR_MSGS
618 
619  /** Default constructor uses ER_IB_MSG_0
620  @param[in] pred True if it's a warning. */
621  error_or_warn(bool pred) : logger(pred ? ERROR_LEVEL : WARNING_LEVEL) {}
622 
623  /** Constructor.
624  @param[in] pred True if it's a warning.
625  @param[in] err Error code from errmsg-*.txt.
626  @param[in] args Variable length argument list */
627  template <class... Args>
628  explicit error_or_warn(bool pred, int err, Args &&... args)
629  : logger(pred ? ERROR_LEVEL : WARNING_LEVEL, err,
630  std::forward<Args>(args)...) {}
631 
632 #endif /* !UNIV_NO_ERR_MSGS */
633 };
634 
635 /** Emit a fatal message if the given predicate is true, otherwise emit a
636 error message. */
637 class fatal_or_error : public logger {
638  public:
639 #ifndef UNIV_NO_ERR_MSGS
640  /** Default constructor uses ER_IB_MSG_0
641  @param[in] fatal true if it's a fatal message */
642  fatal_or_error(bool fatal) : logger(ERROR_LEVEL), m_fatal(fatal) {
643  if (m_fatal) {
644  m_oss << "[fatal]";
645  }
646  }
647 
648  /** Constructor.
649  @param[in] fatal true if it's a fatal message
650  @param[in] err Error code from errmsg-*.txt. */
651  template <class... Args>
652  explicit fatal_or_error(bool fatal, int err)
653  : logger(ERROR_LEVEL, err), m_fatal(fatal) {
654  if (m_fatal) {
655  m_oss << "[fatal]";
656  }
657 
658  m_oss << msg(err, "");
659  }
660 
661  /** Constructor.
662  @param[in] fatal true if it's a fatal message
663  @param[in] err Error code from errmsg-*.txt.
664  @param[in] args Variable length argument list */
665  template <class... Args>
666  explicit fatal_or_error(bool fatal, int err, Args &&... args)
667  : logger(ERROR_LEVEL, err), m_fatal(fatal) {
668  if (m_fatal) {
669  m_oss << "[fatal]";
670  }
671 
672  m_oss << msg(err, std::forward<Args>(args)...);
673  }
674 
675  /** Destructor */
676  virtual ~fatal_or_error();
677 #else
678  /** Constructor */
679  fatal_or_error(bool fatal) : m_fatal(fatal) {}
680 #endif /* !UNIV_NO_ERR_MSGS */
681  private:
682  /** If true then assert after printing an error message. */
683  const bool m_fatal;
684 };
685 
686 #ifdef UNIV_HOTBACKUP
687 /** The class trace is used to emit informational log messages. only when
688 trace level is set in the MEB code */
689 class trace_1 : public logger {
690  public:
691 #ifndef UNIV_NO_ERR_MSGS
692  /** Default constructor uses ER_IB_MSG_0 */
693  trace_1() : logger(INFORMATION_LEVEL) { m_trace_level = 1; }
694 
695  /** Constructor.
696  @param[in] err Error code from errmsg-*.txt.
697  @param[in] args Variable length argument list */
698  template <class... Args>
699  explicit trace_1(int err, Args &&... args)
700  : logger(INFORMATION_LEVEL, err, std::forward<Args>(args)...) {
701  m_trace_level = 1;
702  }
703 
704 #else
705  /** Constructor */
706  trace_1();
707 #endif /* !UNIV_NO_ERR_MSGS */
708 };
709 
710 /** The class trace_2 is used to emit informational log messages only when
711 trace level 2 is set in the MEB code */
712 class trace_2 : public logger {
713  public:
714 #ifndef UNIV_NO_ERR_MSGS
715  /** Default constructor uses ER_IB_MSG_0 */
716  trace_2() : logger(INFORMATION_LEVEL) { m_trace_level = 2; }
717 
718  /** Constructor.
719  @param[in] err Error code from errmsg-*.txt.
720  @param[in] args Variable length argument list */
721  template <class... Args>
722  explicit trace_2(int err, Args &&... args)
723  : logger(INFORMATION_LEVEL, err, std::forward<Args>(args)...) {
724  m_trace_level = 2;
725  }
726 #else
727  /** Destructor. */
728  trace_2();
729 #endif /* !UNIV_NO_ERR_MSGS */
730 };
731 
732 /** The class trace_3 is used to emit informational log messages only when
733 trace level 3 is set in the MEB code */
734 class trace_3 : public logger {
735  public:
736 #ifndef UNIV_NO_ERR_MSGS
737  /** Default constructor uses ER_IB_MSG_0 */
738  trace_3() : logger(INFORMATION_LEVEL) { m_trace_level = 3; }
739 
740  /** Constructor.
741  @param[in] err Error code from errmsg-*.txt.
742  @param[in] args Variable length argument list */
743  template <class... Args>
744  explicit trace_3(int err, Args &&... args)
745  : logger(INFORMATION_LEVEL, err, std::forward<Args>(args)...) {
746  m_trace_level = 3;
747  }
748 
749 #else
750  /** Destructor. */
751  trace_3();
752 #endif /* !UNIV_NO_ERR_MSGS */
753 };
754 #endif /* UNIV_HOTBACKUP */
755 } // namespace ib
756 
757 #ifdef UNIV_HOTBACKUP
758 /** Sprintfs a timestamp to a buffer with no spaces and with ':' characters
759 replaced by '_'.
760 @param[in] buf buffer where to sprintf */
761 void meb_sprintf_timestamp_without_extra_chars(char *buf);
762 #endif /* UNIV_HOTBACKUP */
763 
764 struct Wait_stats {
765  uint64_t wait_loops;
766 
767  explicit Wait_stats(uint64_t wait_loops = 0) : wait_loops(wait_loops) {}
768 
770  wait_loops += rhs.wait_loops;
771  return (*this);
772  }
773 
774  Wait_stats operator+(const Wait_stats &rhs) const {
775  return (Wait_stats{wait_loops + rhs.wait_loops});
776  }
777 
778  bool any_waits() const { return (wait_loops != 0); }
779 };
780 
781 #include "ut0ut.ic"
782 
783 #endif /* !ut0ut_h */
Various utilities.
unsigned char byte
Blob class.
Definition: common.h:159
ulint ut_2_power_up(ulint n)
Calculates fast the number rounded up to the nearest power of 2.
Definition: ut0ut.cc:286
hex(uintmax_t t)
Definition: ut0ut.h:354
bool operator()(const char *a, const char *b) const
Definition: ut0ut.h:259
The class logger is the base class of all the error log related classes.
Definition: ut0ut.h:375
void ut_print_name(FILE *f, const trx_t *trx, const char *name)
Outputs a fixed-length string, quoted as an SQL identifier.
Definition: ut0ut.cc:326
Definition: ut0ut.h:347
Definition: my_loglevel.h:35
double ut_difftime(ib_time_t time2, ib_time_t time1)
Returns the difference of two times in seconds.
Definition: ut0ut.cc:225
Emit a fatal message if the given predicate is true, otherwise emit a error message.
Definition: ut0ut.h:637
Global error codes for the database.
ssize_t count
Definition: memcached.c:386
std::ostream & write(const char *buf, std::streamsize count)
Write the given buffer to the internal string stream object.
Definition: ut0ut.h:408
info(int err, Args &&... args)
Constructor.
Definition: ut0ut.h:529
const string name("\ame\)
Functor that compares two C strings.
Definition: ut0ut.h:258
#define ut_vsnprintf(buf, size, fmt, ap)
A wrapper for vsnprintf(3), formatted output conversion into a limited buffer.
Definition: ut0ut.h:324
logger & operator<<(const T &rhs)
Definition: ut0ut.h:399
Macros for using atomics.
logger(loglevel level)
Constructor.
Definition: ut0ut.h:500
Definition: trx0trx.h:713
std::ostringstream m_oss
For converting the message into a string.
Definition: ut0ut.h:422
time_t ib_time_t
Time stamp.
Definition: ut0ut.h:82
logger(loglevel level, int err, Args &&... args)
Constructor.
Definition: ut0ut.h:492
logger & log(int err, Args &&... args)
Format an error message.
Definition: ut0ut.h:386
Emit an error message if the given predicate is true, otherwise emit a warning message.
Definition: ut0ut.h:615
error_or_warn(bool pred)
Default constructor uses ER_IB_MSG_0.
Definition: ut0ut.h:621
fatal(int err)
Default constructor uses ER_IB_MSG_0.
Definition: ut0ut.h:589
Definition: varlen_sort.h:182
The class fatal is used to emit an error message and stop the server by crashing it.
Definition: ut0ut.h:582
fatal(int err, Args &&... args)
Constructor.
Definition: ut0ut.h:599
ulint ut_delay(ulint delay)
Runs an idle loop on CPU.
Definition: ut0ut.cc:265
error_or_warn(bool pred, int err, Args &&... args)
Constructor.
Definition: ut0ut.h:628
uint64_t wait_loops
Definition: ut0ut.h:765
char * ut_format_name(const char *name, char *formatted, ulint formatted_size)
Format a table name, quoted as an SQL identifier.
Definition: ut0ut.cc:352
The class warn is used to emit warnings.
Definition: ut0ut.h:539
error(int err, Args &&... args)
Constructor.
Definition: ut0ut.h:570
std::ostream & write(const byte *buf, std::streamsize count)
Write the given buffer to the internal string stream object.
Definition: ut0ut.h:416
Definition: my_loglevel.h:36
int ut_usectime(ulint *sec, ulint *ms)
Returns system time.
Definition: ut0ut.cc:155
static std::string msg(int err, Args &&... args)
Format an error message.
Definition: ut0ut.h:451
The class info is used to emit informational log messages.
Definition: ut0ut.h:518
Wait_stats & operator+=(const Wait_stats &rhs)
Definition: ut0ut.h:769
#define LOG_BUFF_MAX
advisory.
Definition: log_shared.h:221
const char * srv_get_server_errmsgs(int errcode)
Get the format string for the logger.
Definition: srv0srv.cc:3051
dberr_t
Definition: db0err.h:38
logger(loglevel level, int err)
Constructor.
Definition: ut0ut.h:472
size_t ut_basename_noext(const char *file, char *base, size_t base_size)
Extract the basename of a file without its extension.
fatal_or_error(bool fatal)
Default constructor uses ER_IB_MSG_0.
Definition: ut0ut.h:642
t
Definition: dbug_analyze.cc:147
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:48
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:910
void delay(int amt)
Definition: suite_stubs.c:27
char msg[1024]
Definition: test_sql_9_sessions.cc:282
std::ostream & operator<<(std::ostream &lhs, const hex &rhs)
This is an overload of the global operator<< for the user defined type ib::hex.
Definition: ut0ut.h:364
UNIV_INLINE uint32_t ut_2_exp(uint32_t n)
Calculates 2 to power n.
fatal_or_error(bool fatal, int err, Args &&... args)
Constructor.
Definition: ut0ut.h:666
Debug utilities for Innobase.
ib_time_t ut_time(void)
Returns system time.
Definition: ut0ut.cc:148
#define PREFIX
Definition: pfs_builtin_memory.cc:141
const char * ut_strerr(dberr_t num)
Convert an error number to a human readable text message.
Definition: ut0ut.cc:425
Definition: my_loglevel.h:34
uintmax_t ut_time_us(uintmax_t *tloc)
Returns the number of microseconds since epoch.
Definition: ut0ut.cc:195
Wait_stats operator+(const Wait_stats &rhs) const
Definition: ut0ut.h:774
const bool m_fatal
If true then assert after printing an error message.
Definition: ut0ut.h:683
error()
Default constructor uses ER_IB_MSG_0.
Definition: ut0ut.h:564
static Logger logger
The "top-level" logger used when no connection context is given.
Definition: test_trace_plugin.cc:293
UNIV_INLINE void ut_pair_min(ulint *min_hi, ulint *min_lo, ulint a_hi, ulint a_lo, ulint b_hi, ulint b_lo)
Calculate the minimum of two pairs.
fatal_or_error(bool fatal, int err)
Constructor.
Definition: ut0ut.h:652
UNIV_INLINE ulint ut_2_log(ulint n)
Calculates fast the 2-logarithm of a number, rounded upward to an integer.
loglevel
Definition: my_loglevel.h:32
Wait_stats(uint64_t wait_loops=0)
Definition: ut0ut.h:767
void ut_copy_file(FILE *dest, FILE *src)
Catenate files.
Definition: ut0ut.cc:380
UNIV_INLINE int ut_ulint_cmp(ulint a, ulint b)
Compares two ulints.
UNIV_INLINE int ut_pair_cmp(ulint a_h, ulint a_l, ulint b_h, ulint b_l)
Compare two pairs of integers.
warn(int err, Args &&... args)
Constructor.
Definition: ut0ut.h:549
fatal()
Default constructor uses ER_IB_MSG_0.
Definition: ut0ut.h:586
warn()
Default constructor uses ER_IB_MSG_0.
Definition: ut0ut.h:543
ulint ut_time_ms(void)
Returns the number of milliseconds since some epoch.
Definition: ut0ut.cc:215
const uintmax_t m_val
Definition: ut0ut.h:355
bool any_waits() const
Definition: ut0ut.h:778
Definition: ut0ut.h:764
static struct my_cs_file_section_st sec[]
Definition: ctype.cc:155
std::string ut_get_name(const trx_t *trx, const char *name)
Get a fixed-length string, quoted as an SQL identifier.
Definition: ut0ut.cc:310
This is a wrapper class, used to print any unsigned integer type in hexadecimal format.
Definition: ut0ut.h:353
info()
Default constructor uses ER_IB_MSG_0.
Definition: ut0ut.h:523
The class error is used to emit error messages.
Definition: ut0ut.h:560