MySQL  8.0.27
Source Code Documentation
log.h
Go to the documentation of this file.
1 /* Copyright (c) 2005, 2021, 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 sql/log.h
25 
26  Error logging, slow query logging, general query logging:
27  If it's server-internal, and it's logging, it's here.
28 
29  Components/services should NOT include this, but include
30  include/mysql/components/services/log_builtins.h instead
31  to gain access to the error logging stack.
32 
33  Legacy plugins (pre-"services") will likely include
34  include/mysql/service_my_plugin_log.h instead.
35 */
36 
37 #ifndef LOG_H
38 #define LOG_H
39 
41 #include <stdarg.h>
42 #include <stddef.h>
43 #include <sys/types.h>
44 
45 #include "lex_string.h"
46 #include "my_command.h"
47 #include "my_compiler.h"
48 #include "my_dbug.h"
49 #include "my_inttypes.h"
50 #include "my_io.h"
51 #include "my_loglevel.h"
52 #include "my_psi_config.h"
53 #include "my_thread_local.h"
57 #include "mysql/psi/mysql_mutex.h"
58 #include "mysql_com.h"
59 #include "sql/auth/sql_security_ctx.h" // Security_context
60 
61 class THD;
62 struct CHARSET_INFO;
63 struct TABLE_LIST;
64 
65 ////////////////////////////////////////////////////////////
66 //
67 // Slow/General Log
68 //
69 ////////////////////////////////////////////////////////////
70 
71 /*
72  System variables controlling logging:
73 
74  log_output (--log-output)
75  Values: NONE, FILE, TABLE
76  Select output destination. Does not enable logging.
77  Can set more than one (e.g. TABLE | FILE).
78 
79  general_log (--general_log)
80  slow_query_log (--slow_query_log)
81  Values: 0, 1
82  Enable/disable general/slow query log.
83 
84  general_log_file (--general-log-file)
85  slow_query_log_file (--slow-query-log-file)
86  Values: filename
87  Set name of general/slow query log file.
88 
89  sql_log_off
90  Values: ON, OFF
91  Enable/disable general query log (OPTION_LOG_OFF).
92 
93  log_queries_not_using_indexes (--log-queries-not-using-indexes)
94  Values: ON, OFF
95  Control slow query logging of queries that do not use indexes.
96 
97  --log-raw
98  Values: ON, OFF
99  Control query rewrite of passwords to the general log.
100 
101  --log-short-format
102  Values: ON, OFF
103  Write short format to the slow query log (and the binary log).
104 
105  --log-slow-admin-statements
106  Values: ON, OFF
107  Log statements such as OPTIMIZE TABLE, ALTER TABLE to the slow query log.
108 
109  --log-slow-replica-statements
110  Values: ON, OFF
111 
112  log_throttle_queries_not_using_indexes
113  Values: INT
114  Number of queries not using indexes logged to the slow query log per min.
115 */
116 
117 /**
118  Write a message to a log (for now just used for error log).
119  This is a variadic convenience interface to the logging components
120  (which use the log_line structure internally), e.g.
121 
122  log_message(LOG_TYPE_ERROR,
123  LOG_ITEM_LOG_PRIO, INFORMATION_LEVEL,
124  LOG_ITEM_LOG_MESSAGE, "file %s is %f %% yellow",
125  filename, yellowfication);
126 
127  For use by legacy sql_print_*(), legacy my_plugin_log_message();
128  also available via the log_builtins service as message().
129 
130  Wherever possible, use the fluent C++ wrapper LogErr()
131  (see log_builtins.h) instead.
132 
133  See log_shared.h for LOG_TYPEs as well as for allowed LOG_ITEM_ types.
134 */
135 int log_vmessage(int log_type, va_list lili);
136 int log_message(int log_type, ...);
137 
138 /**
139  A helper that we can stubify so we don't have to pull all of THD
140  into the unit tests.
141 
142  @param thd a thd
143  @retval its thread-ID
144 */
146 
147 /** Type of the log table */
152 };
153 
154 /**
155  Abstract superclass for handling logging to slow/general logs.
156  Currently has two subclasses, for table and file based logging.
157 */
159  public:
160  Log_event_handler() = default;
161  virtual ~Log_event_handler() = default;
162 
163  /**
164  Log a query to the slow log.
165 
166  @param thd THD of the query
167  @param current_utime Current timestamp in microseconds
168  @param query_start_arg Command start timestamp in microseconds
169  @param user_host The pointer to the string with user\@host info
170  @param user_host_len Length of the user_host string
171  @param query_utime Number of microseconds query execution took
172  @param lock_utime Number of microseconds the query was locked
173  @param is_command The flag which determines whether the sql_text
174  is a query or an administrator command
175  @param sql_text The query or administrator in textual form
176  @param sql_text_len The length of sql_text string
177  @param query_start_status Pointer to a snapshot of thd->status_var taken
178  at the start of execution
179 
180  @return true if error, false otherwise.
181  */
182  virtual bool log_slow(THD *thd, ulonglong current_utime,
183  ulonglong query_start_arg, const char *user_host,
184  size_t user_host_len, ulonglong query_utime,
185  ulonglong lock_utime, bool is_command,
186  const char *sql_text, size_t sql_text_len,
187  struct System_status_var *query_start_status) = 0;
188 
189  /**
190  Log command to the general log.
191 
192  @param thd THD of the query
193  @param event_utime Command start timestamp in micro seconds
194  @param user_host The pointer to the string with user\@host info
195  @param user_host_len Length of the user_host string. this is computed
196  once and passed to all general log event handlers
197  @param thread_id Id of the thread, issued a query
198  @param command_type The type of the command being logged
199  @param command_type_len The length of the string above
200  @param sql_text The very text of the query being executed
201  @param sql_text_len The length of sql_text string
202  @param client_cs Character set to use for strings
203 
204  @return This function attempts to never call my_error(). This is
205  necessary, because general logging happens already after a statement
206  status has been sent to the client, so the client can not see the
207  error anyway. Besides, the error is not related to the statement
208  being executed and is internal, and thus should be handled
209  internally (@todo: how?).
210  If a write to the table has failed, the function attempts to
211  write to a short error message to the file. The failure is also
212  indicated in the return value.
213 
214  @retval false OK
215  @retval true error occurred
216  */
217  virtual bool log_general(THD *thd, ulonglong event_utime,
218  const char *user_host, size_t user_host_len,
219  my_thread_id thread_id, const char *command_type,
220  size_t command_type_len, const char *sql_text,
221  size_t sql_text_len,
222  const CHARSET_INFO *client_cs) = 0;
223 };
224 
225 /** Class responsible for table based logging. */
227  public:
228  /** @see Log_event_handler::log_slow(). */
229  bool log_slow(THD *thd, ulonglong current_utime, ulonglong query_start_arg,
230  const char *user_host, size_t user_host_len,
231  ulonglong query_utime, ulonglong lock_utime, bool is_command,
232  const char *sql_text, size_t sql_text_len,
233  struct System_status_var *query_start_status) override;
234 
235  /** @see Log_event_handler::log_general(). */
236  bool log_general(THD *thd, ulonglong event_utime, const char *user_host,
237  size_t user_host_len, my_thread_id thread_id,
238  const char *command_type, size_t command_type_len,
239  const char *sql_text, size_t sql_text_len,
240  const CHARSET_INFO *client_cs) override;
241 
242  private:
243  /**
244  Check if log table for given log type exists and can be opened.
245 
246  @param thd Thread handle
247  @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
248 
249  @return true if table could not be opened, false otherwise.
250  */
252 
253  friend class Query_logger;
254 };
255 
256 /* Log event handler flags */
257 static const uint LOG_NONE = 1;
258 static const uint LOG_FILE = 2;
259 static const uint LOG_TABLE = 4;
260 
262 
263 /** Class which manages slow and general log event handlers. */
265  /**
266  Currently we have only 2 kinds of logging functions: old-fashioned
267  file logs and csv logging routines.
268  */
269  static const uint MAX_LOG_HANDLERS_NUM = 2;
270 
271  /**
272  RW-lock protecting Query_logger.
273  R-lock taken when writing to slow/general query log.
274  W-lock taken when activating/deactivating logs.
275  */
277 
278  /** Available log handlers. */
281 
282  /** NULL-terminated arrays of log handlers. */
285 
286  private:
287  /**
288  Setup log event handlers for the given log_type.
289 
290  @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
291  @param log_printer Bitmap of LOG_NONE, LOG_FILE, LOG_TABLE
292  */
294 
295  public:
297 
298  /**
299  Check if table logging is turned on for the given log_type.
300 
301  @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
302 
303  @return true if table logging is on, false otherwise.
304  */
306 
307  /**
308  Check if file logging is turned on for the given log type.
309 
310  @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
311 
312  @return true if the file logging is on, false otherwise.
313  */
315 
316  /**
317  Perform basic log initialization: create file-based log handler.
318 
319  We want to initialize all log mutexes as soon as possible,
320  but we cannot do it in constructor, as safe_mutex relies on
321  initialization, performed by MY_INIT(). This why this is done in
322  this function.
323  */
324  void init();
325 
326  /** Free memory. Nothing could be logged after this function is called. */
327  void cleanup();
328 
329  /**
330  Log slow query with all enabled log event handlers.
331 
332  @param thd THD of the statement being logged.
333  @param query The query string being logged.
334  @param query_length The length of the query string.
335  @param query_start_status Pointer to a snapshot of thd->status_var taken
336  at the start of execution
337 
338  @return true if error, false otherwise.
339  */
340  bool slow_log_write(THD *thd, const char *query, size_t query_length,
341  struct System_status_var *query_start_status);
342 
343  /**
344  Write printf style message to general query log.
345 
346  @param thd THD of the statement being logged.
347  @param command COM of statement being logged.
348  @param format Printf style format of message.
349  @param ... Printf parameters to write.
350 
351  @return true if error, false otherwise.
352  */
354  const char *format, ...)
355  MY_ATTRIBUTE((format(printf, 4, 5)));
356 
357  /**
358  Write query to general query log.
359 
360  @param thd THD of the statement being logged.
361  @param command COM of statement being logged.
362  @param query The query string being logged.
363  @param query_length The length of the query string.
364 
365  @return true if error, false otherwise.
366  */
368  const char *query, size_t query_length);
369 
370  /**
371  Enable log event handlers for slow/general log.
372 
373  @param log_printer Bitmask of log event handlers.
374 
375  @note Acceptable values are LOG_NONE, LOG_FILE, LOG_TABLE
376  */
377  void set_handlers(ulonglong log_printer);
378 
379  /**
380  Activate log handlers for the given log type.
381 
382  @param thd Thread handle
383  @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
384 
385  @return true if error, false otherwise.
386  */
388 
389  /**
390  Close file log for the given log type.
391 
392  @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
393  */
395 
396  /**
397  Close file log for the given log type and the reopen it.
398 
399  @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
400  */
402 
403  /**
404  Read log file name from global variable opt_*_logname.
405  If called from a sys_var update function, the caller
406  must hold a lock protecting the sys_var
407  (LOCK_global_system_variables, a polylock for the
408  variable, etc.).
409 
410  @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
411  */
413 
414  /**
415  Check if given TABLE_LIST has a query log table name and
416  optionally check if the query log is currently enabled.
417 
418  @param table_list TABLE_LIST representing the table to check
419  @param check_if_opened Always return QUERY_LOG_NONE unless the
420  query log table is enabled.
421 
422  @retval QUERY_LOG_NONE, QUERY_LOG_SLOW or QUERY_LOG_GENERAL
423  */
425  bool check_if_opened) const;
426 };
427 
429 
430 /**
431  Create the name of the query log specified.
432 
433  This method forms a new path + file name for the log specified.
434 
435  @param[in] buff Location for building new string.
436  @param[in] log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
437 
438  @returns Pointer to new string containing the name.
439 */
441 
442 /**
443  Check given log name against certain blacklisted names/extensions.
444 
445  @param name Log name to check
446  @param len Length of log name
447 
448  @returns true if name is valid, false otherwise.
449 */
450 bool is_valid_log_name(const char *name, size_t len);
451 
452 /**
453  Check whether we need to write the current statement (or its rewritten
454  version if it exists) to the slow query log.
455  As a side-effect, a digest of suppressed statements may be written.
456 
457  @param thd thread handle
458 
459  @retval
460  true statement needs to be logged
461  @retval
462  false statement does not need to be logged
463 */
464 bool log_slow_applicable(THD *thd);
465 
466 /**
467  Unconditionally writes the current statement (or its rewritten version if it
468  exists) to the slow query log.
469 
470  @param thd thread handle
471  @param query_start_status Pointer to a snapshot of thd->status_var taken
472  at the start of execution
473 */
474 void log_slow_do(THD *thd, struct System_status_var *query_start_status);
475 
476 /**
477  Check whether we need to write the current statement to the slow query
478  log. If so, do so. This is a wrapper for the two functions above;
479  most callers should use this wrapper. Only use the above functions
480  directly if you have expensive rewriting that you only need to do if
481  the query actually needs to be logged (e.g. SP variables / NAME_CONST
482  substitution when executing a PROCEDURE).
483  A digest of suppressed statements may be logged instead of the current
484  statement.
485 
486  @param thd thread handle
487  @param query_start_status Pointer to a snapshot of thd->status_var taken
488  at the start of execution
489 */
490 void log_slow_statement(THD *thd, struct System_status_var *query_start_status);
491 
492 /**
493  @class Log_throttle
494  @brief Base class for rate-limiting a log (slow query log etc.)
495 */
496 
498  /**
499  When will/did current window end?
500  */
502 
503  /**
504  Log no more than rate lines of a given type per window_size
505  (e.g. per minute, usually LOG_THROTTLE_WINDOW_SIZE).
506  */
507  const ulong window_size;
508 
509  /**
510  There have been this many lines of this type in this window,
511  including those that we suppressed. (We don't simply stop
512  counting once we reach the threshold as we'll write a summary
513  of the suppressed lines later.)
514  */
515  ulong count;
516 
517  protected:
518  /**
519  Template for the summary line. Should contain %lu as the only
520  conversion specification.
521  */
522  const char *summary_template;
523 
524  /**
525  Start a new window.
526  */
527  void new_window(ulonglong now);
528 
529  /**
530  Increase count of logs we're handling.
531 
532  @param rate Limit on records to be logged during the throttling window.
533 
534  @retval true - log rate limit is exceeded, so record should be supressed.
535  @retval false - log rate limit is not exceeded, record should be logged.
536  */
537  bool inc_log_count(ulong rate) { return (++count > rate); }
538 
539  /**
540  Check whether we're still in the current window. (If not, the caller
541  will want to print a summary (if the logging of any lines was suppressed),
542  and start a new window.)
543  */
544  bool in_window(ulonglong now) const { return (now < window_end); }
545 
546  /**
547  Prepare a summary of suppressed lines for logging.
548  This function returns the number of queries that were qualified for
549  inclusion in the log, but were not printed because of the rate-limiting.
550  The summary will contain this count as well as the respective totals for
551  lock and execution time.
552  This function assumes that the caller already holds the necessary locks.
553 
554  @param rate Limit on records logged during the throttling window.
555  */
556  ulong prepare_summary(ulong rate);
557 
558  /**
559  @param window_usecs ... in this many micro-seconds
560  @param msg use this template containing %lu as only non-literal
561  */
562  Log_throttle(ulong window_usecs, const char *msg)
563  : window_end(0),
564  window_size(window_usecs),
565  count(0),
566  summary_template(msg) {}
567 
568  public:
569  /**
570  We're rate-limiting messages per minute; 60,000,000 microsecs = 60s
571  Debugging is less tedious with a window in the region of 5000000
572  */
573  static const ulong LOG_THROTTLE_WINDOW_SIZE = 60000000;
574 };
575 
576 /**
577  @class Slow_log_throttle
578  @brief Used for rate-limiting the slow query log.
579 */
580 
582  private:
583  /**
584  We're using our own (empty) security context during summary generation.
585  That way, the aggregate value of the suppressed queries isn't printed
586  with a specific user's name (i.e. the user who sent a query when or
587  after the time-window closes), as that would be misleading.
588  */
590 
591  /**
592  Total of the execution times of queries in this time-window for which
593  we suppressed logging. For use in summary printing.
594  */
596 
597  /**
598  Total of the lock times of queries in this time-window for which
599  we suppressed logging. For use in summary printing.
600  */
602 
603  /**
604  A reference to the threshold ("no more than n log lines per ...").
605  References a (system-?) variable in the server.
606  */
607  ulong *rate;
608 
609  /**
610  The routine we call to actually log a line (i.e. our summary).
611  The signature miraculously coincides with slow_log_print().
612  */
613  bool (*log_summary)(THD *, const char *, size_t, struct System_status_var *);
614 
615  /**
616  Slow_log_throttle is shared between THDs.
617  */
619 
620  /**
621  Start a new window.
622  */
623  void new_window(ulonglong now);
624 
625  /**
626  Actually print the prepared summary to log.
627  */
628  void print_summary(THD *thd, ulong suppressed, ulonglong print_lock_time,
629  ulonglong print_exec_time);
630 
631  public:
632  /**
633  @param threshold suppress after this many queries ...
634  @param lock mutex to use for consistency of calculations
635  @param window_usecs ... in this many micro-seconds
636  @param logger call this function to log a single line (our summary)
637  @param msg use this template containing %lu as only non-literal
638  */
639  Slow_log_throttle(ulong *threshold, mysql_mutex_t *lock, ulong window_usecs,
640  bool (*logger)(THD *, const char *, size_t,
641  struct System_status_var *),
642  const char *msg);
643 
644  /**
645  Prepare and print a summary of suppressed lines to log.
646  (For now, slow query log.)
647  The summary states the number of queries that were qualified for
648  inclusion in the log, but were not printed because of the rate-limiting,
649  and their respective totals for lock and execution time.
650  This wrapper for prepare_summary() and print_summary() handles the
651  locking/unlocking.
652 
653  @param thd The THD that tries to log the statement.
654  @retval false Logging was not supressed, no summary needed.
655  @retval true Logging was supressed; a summary was printed.
656  */
657  bool flush(THD *thd);
658 
659  /**
660  Top-level function.
661  @param thd The THD that tries to log the statement.
662  @param eligible Is the statement of the type we might suppress?
663  @retval true Logging should be supressed.
664  @retval false Logging should not be supressed.
665  */
666  bool log(THD *thd, bool eligible);
667 };
668 
669 /**
670  @class Slow_log_throttle
671  @brief Used for rate-limiting a error logs.
672 */
673 
675  private:
678  const char *subsys;
679 
680  /**
681  Actually print the prepared summary to log.
682  */
683  void print_summary(ulong suppressed) {
686  subsys, LOG_ITEM_LOG_MESSAGE, summary_template,
687  (unsigned long)suppressed);
688  }
689 
690  public:
691  /**
692  @param window_usecs ... in this many micro-seconds (see Log_throttle)
693  @param lvl severity of the incident (error, warning, info)
694  @param errcode MySQL error code (e.g. ER_STARTUP)
695  @param subsystem subsystem tag, or nullptr for none
696  @param msg use this message template containing %lu as only
697  non-literal (for "number of suppressed events",
698  see Log_throttle)
699  */
700  Error_log_throttle(ulong window_usecs, loglevel lvl, uint errcode,
701  const char *subsystem, const char *msg)
702  : Log_throttle(window_usecs, msg),
703  ll(lvl),
704  err_code(errcode),
705  subsys(subsystem) {}
706 
707  /**
708  Prepare and print a summary of suppressed lines to log.
709  (For now, slow query log.)
710  The summary states the number of queries that were qualified for
711  inclusion in the log, but were not printed because of the rate-limiting.
712 
713  @retval false Logging was not suppressed, no summary needed.
714  @retval true Logging was suppressed; a summary was printed.
715  */
716  bool flush();
717 
718  /**
719  Top-level function.
720  @retval true Logging should be suppressed.
721  @retval false Logging should not be suppressed.
722  */
723  bool log();
724 };
725 
727 
728 ////////////////////////////////////////////////////////////
729 //
730 // Error Log
731 //
732 ////////////////////////////////////////////////////////////
733 
734 /*
735  Set up some convenience defines to help us while we change
736  old-style ("sql_print_...()") calls to new-style ones
737  ("LogErr(...)"). New code should not use these, nor should
738  it use sql_print_...().
739 */
740 
741 /**
742  Set up basics, fetch message for "errcode", insert any va_args,
743  call the new error stack. A helper for the transition to the
744  new stack.
745 */
746 #define log_errlog(level, errcode, ...) \
747  log_message(LOG_TYPE_ERROR, LOG_ITEM_LOG_PRIO, (longlong)level, \
748  LOG_ITEM_SRV_SUBSYS, LOG_SUBSYSTEM_TAG, LOG_ITEM_SRC_LINE, \
749  (longlong)__LINE__, LOG_ITEM_SRC_FILE, MY_BASENAME, \
750  LOG_ITEM_LOG_LOOKUP, (longlong)errcode, ##__VA_ARGS__)
751 
752 /**
753  Default tags + freeform message. A helper for re#defining sql_print_*()
754  to go through the new error log service stack.
755 
756  Remember to never blindly LOG_MESSAGE a string you that may contain
757  user input as it may contain % which will be treated as substitutions.
758 
759  BAD: LOG_ITEM_LOG_MESSAGE, dodgy_message
760  OK: LOG_ITEM_LOG_MESSAGE, "%s", dodgy_message
761  GOOD: LOG_ITEM_LOG_VERBATIM, dodgy_message
762 */
763 #define log_errlog_formatted(level, ...) \
764  log_message(LOG_TYPE_ERROR, LOG_ITEM_LOG_PRIO, (longlong)level, \
765  LOG_ITEM_SRV_SUBSYS, LOG_SUBSYSTEM_TAG, LOG_ITEM_SRC_LINE, \
766  (longlong)__LINE__, LOG_ITEM_SRC_FILE, MY_BASENAME, \
767  LOG_ITEM_LOG_MESSAGE, ##__VA_ARGS__)
768 
769 /**
770  Set up the default tags, then let us add/override any key/value we like,
771  call the new error stack. A helper for the transition to the new stack.
772 */
773 #define log_errlog_rich(level, ...) \
774  log_message(LOG_TYPE_ERROR, LOG_ITEM_LOG_PRIO, (longlong)level, \
775  LOG_ITEM_SRV_SUBSYS, LOG_SUBSYSTEM_TAG, LOG_ITEM_SRC_LINE, \
776  (longlong)__LINE__, LOG_ITEM_SRC_FILE, MY_BASENAME, __VA_ARGS__)
777 
778 /**
779  Define sql_print_*() so they use the new log_message()
780  variadic convenience interface to logging. This lets
781  us switch over the bulk of the messages right away until
782  we can attend to them individually; it also verifies that
783  we no longer use function pointers to log functions.
784 
785  As before, sql_print_*() only accepts a printf-style
786  format string, and the arguments to same, if any.
787 */
788 #define sql_print_information(...) \
789  log_errlog_formatted(INFORMATION_LEVEL, ##__VA_ARGS__)
790 
791 #define sql_print_warning(...) \
792  log_errlog_formatted(WARNING_LEVEL, ##__VA_ARGS__)
793 
794 #define sql_print_error(...) log_errlog_formatted(ERROR_LEVEL, ##__VA_ARGS__)
795 
796 /**
797  Prints a printf style message to the error log.
798 
799  A thin wrapper around log_message() for local_message_hook,
800  Table_check_intact::report_error, and others.
801 
802  @param level The level of the msg significance
803  @param ecode Error code of the error message.
804  @param args va_list list of arguments for the message
805 */
806 void error_log_print(enum loglevel level, uint ecode, va_list args);
807 
808 /**
809  Initialize structures (e.g. mutex) needed by the error log.
810 
811  @note This function accesses shared resources without protection, so
812  it should only be called while the server is running single-threaded.
813 
814  @note The error log can still be used before this function is called,
815  but that should only be done single-threaded.
816 
817  @retval true an error occurred
818  @retval false basic error logging is now available in multi-threaded mode
819 */
820 bool init_error_log();
821 
822 /**
823  Open the error log and redirect stderr and optionally stdout
824  to the error log file. The streams are reopened only for
825  appending (writing at end of file).
826 
827  @note
828  On error, my_error() is not called here.
829  So, caller of this function should call my_error() to keep the protocol.
830 
831  @note This function also writes any error log messages that
832  have been buffered by calling flush_error_log_messages().
833 
834  @param filename Name of error log file
835  @param get_lock Should we acquire LOCK_error_log?
836 */
837 bool open_error_log(const char *filename, bool get_lock);
838 
839 /**
840  Free any error log resources.
841 
842  @note This function accesses shared resources without protection, so
843  it should only be called while the server is running single-threaded.
844 
845  @note The error log can still be used after this function is called,
846  but that should only be done single-threaded. All buffered messages
847  should be flushed before calling this function.
848 */
849 void destroy_error_log();
850 
851 /**
852  Flush any pending data to disk and reopen the error log.
853 */
854 bool reopen_error_log();
855 
856 /**
857  Discard all buffered messages and deallocate buffer without printing
858  anything. Needed when terminating launching process after daemon
859  has started. At this point we may have messages in the error log,
860  but we don't want to show them to stderr (the daemon will output
861  them in its error log).
862  */
864 
865 /**
866  We buffer all error log messages that have been printed before the
867  error log has been opened. This allows us to write them to the
868  correct file once the error log has been opened.
869 
870  This function will explicitly flush buffered messages to stderr.
871  It is only needed in cases where open_error_log() is not called
872  as it otherwise will be done there.
873 
874  This function also turns buffering off (there is no way to turn
875  buffering back on).
876 */
878 
879 /**
880  Modular logger: log line and key/value manipulation helpers.
881  Server-internal. External services should access these via
882  the log_builtins service API (cf. preamble for this file).
883 */
884 
885 /**
886  Compare two NUL-terminated byte strings
887 
888  Note that when comparing without length limit, the long string
889  is greater if they're equal up to the length of the shorter
890  string, but the shorter string will be considered greater if
891  its "value" up to that point is greater:
892 
893  compare 'abc','abcd': -100 (longer wins if otherwise same)
894  compare 'abca','abcd': -3 (higher value wins)
895  compare 'abcaaaaa','abcd': -3 (higher value wins)
896 
897  @param a the first string
898  @param b the second string
899  @param len compare at most this many characters --
900  0 for no limit
901  @param case_insensitive ignore upper/lower case in comparison
902 
903  @retval -1 a < b
904  @retval 0 a == b
905  @retval 1 a > b
906 */
907 int log_string_compare(const char *a, const char *b, size_t len,
908  bool case_insensitive);
909 
910 /**
911  Predicate used to determine whether a type is generic
912  (generic string, generic float, generic integer) rather
913  than a well-known type.
914 
915  @param t log item type to examine
916 
917  @retval true if generic type
918  @retval false if wellknown type
919 */
921 
922 /**
923  Predicate used to determine whether a class is a string
924  class (C-string or Lex-string).
925 
926  @param c log item class to examine
927 
928  @retval true if of a string class
929  @retval false if not of a string class
930 */
932 
933 /**
934  Predicate used to determine whether a class is a numeric
935  class (integer or float).
936 
937  @param c log item class to examine
938 
939  @retval true if of a numeric class
940  @retval false if not of a numeric class
941 */
943 
944 /**
945  Get an integer value from a log-item of float or integer type.
946 
947  @param li log item to get the value from
948  @param i longlong to store the value in
949 */
950 void log_item_get_int(log_item *li, longlong *i);
951 
952 /**
953  Get a float value from a log-item of float or integer type.
954 
955  @param li log item to get the value from
956  @param f float to store the value in
957 */
958 void log_item_get_float(log_item *li, double *f);
959 
960 /**
961  Get a string value from a log-item of C-string or Lex string type.
962 
963  @param li log item to get the value from
964  @param str char-pointer to store the pointer to the value in
965  @param len size_t pointer to store the length of the value in
966 */
967 void log_item_get_string(log_item *li, char **str, size_t *len);
968 
969 /**
970  Set an integer value on a log_item.
971  Fails gracefully if not log_item_data is supplied, so it can safely
972  wrap log_line_item_set[_with_key]().
973 
974  @param lid log_item_data struct to set the value on
975  @param i integer to set
976 
977  @retval true lid was nullptr (possibly: OOM, could not set up log_item)
978  @retval false all's well
979 */
981 
982 /**
983  Set a floating point value on a log_item.
984  Fails gracefully if not log_item_data is supplied, so it can safely
985  wrap log_line_item_set[_with_key]().
986 
987  @param lid log_item_data struct to set the value on
988  @param f float to set
989 
990  @retval true lid was nullptr (possibly: OOM, could not set up log_item)
991  @retval false all's well
992 */
993 bool log_item_set_float(log_item_data *lid, double f);
994 
995 /**
996  Set a string value on a log_item.
997  Fails gracefully if not log_item_data is supplied, so it can safely
998  wrap log_line_item_set[_with_key]().
999 
1000  @param lid log_item_data struct to set the value on
1001  @param s pointer to string
1002  @param s_len length of string
1003 
1004  @retval true lid was nullptr (possibly: OOM, could not set up log_item)
1005  @retval false all's well
1006 */
1007 bool log_item_set_lexstring(log_item_data *lid, const char *s, size_t s_len);
1008 
1009 /**
1010  Set a string value on a log_item.
1011  Fails gracefully if not log_item_data is supplied, so it can safely
1012  wrap log_line_item_set[_with_key]().
1013 
1014  @param lid log_item_data struct to set the value on
1015  @param s pointer to NTBS
1016 
1017  @retval true lid was nullptr (possibly: OOM, could not set up log_item)
1018  @retval false all's well
1019 */
1020 bool log_item_set_cstring(log_item_data *lid, const char *s);
1021 
1022 /**
1023  See whether a string is a wellknown field name.
1024 
1025  @param key potential key starts here
1026  @param len length of the string to examine
1027 
1028  @retval LOG_ITEM_TYPE_RESERVED: reserved, but not "wellknown" key
1029  @retval LOG_ITEM_TYPE_NOT_FOUND: key not found
1030  @retval >0: index in array of wellknowns
1031 */
1032 int log_item_wellknown_by_name(const char *key, size_t len);
1033 
1034 /**
1035  See whether a type is wellknown.
1036 
1037  @param t log item type to examine
1038 
1039  @retval LOG_ITEM_TYPE_NOT_FOUND: key not found
1040  @retval >0: index in array of wellknowns
1041 */
1043 
1044 /**
1045  Accessor: from a record describing a wellknown key, get its name
1046 
1047  @param idx index in array of wellknowns, see log_item_wellknown_by_...()
1048 
1049  @retval name (NTBS)
1050 */
1051 const char *log_item_wellknown_get_name(uint idx);
1052 
1053 /**
1054  Accessor: from a record describing a wellknown key, get its type
1055 
1056  @param idx index in array of wellknowns, see log_item_wellknown_by_...()
1057 
1058  @retval the log item type for the wellknown key
1059 */
1061 
1062 /**
1063  Accessor: from a record describing a wellknown key, get its class
1064 
1065  @param idx index in array of wellknowns, see log_item_wellknown_by_...()
1066 
1067  @retval the log item class for the wellknown key
1068 */
1070 
1071 /**
1072  Release any of key and value on a log-item that were dynamically allocated.
1073 
1074  @param li log-item to release the payload of
1075 */
1076 void log_item_free(log_item *li);
1077 
1078 /**
1079  Predicate indicating whether a log line is "willing" to accept any more
1080  key/value pairs.
1081 
1082  @param ll the log-line to examine
1083 
1084  @retval false if not full / if able to accept another log_item
1085  @retval true if full
1086 */
1087 bool log_line_full(log_line *ll);
1088 
1089 /**
1090  How many items are currently set on the given log_line?
1091 
1092  @param ll the log-line to examine
1093 
1094  @retval the number of items set
1095 */
1096 int log_line_item_count(log_line *ll);
1097 
1098 /**
1099  Test whether a given type is presumed present on the log line.
1100 
1101  @param ll the log_line to examine
1102  @param m the log_type to test for
1103 
1104  @retval 0 not present
1105  @retval !=0 present
1106 */
1108 
1109 /**
1110  Initialize a log_line.
1111 
1112  @retval nullptr could not set up buffer (too small?)
1113  @retval other address of the newly initialized log_line
1114 */
1116 
1117 /**
1118  Release a log_line allocated with log_line_init.
1119 
1120  @retval nullptr could not set up buffer (too small?)
1121  @retval other address of the newly initialized log_line
1122 */
1123 void log_line_exit(log_line *ll);
1124 
1125 /**
1126  Get log-line's output buffer.
1127  If the logger core provides this buffer, the log-service may use it
1128  to assemble its output therein and implicitly return it to the core.
1129  Participation is required for services that support populating
1130  performance_schema.error_log, and optional for all others.
1131 
1132  @param ll the log_line to examine
1133 
1134  @retval nullptr success, an output buffer is available
1135  @retval otherwise failure, no output buffer is available
1136 */
1138 
1139 /**
1140  Release log line item (key/value pair) with the index elem in log line ll.
1141  This frees whichever of key and value were dynamically allocated.
1142  This leaves a "gap" in the bag that may immediately be overwritten
1143  with an updated element. If the intention is to remove the item without
1144  replacing it, use log_line_item_remove() instead!
1145 
1146  @param ll log_line
1147  @param elem index of the key/value pair to release
1148 */
1149 void log_line_item_free(log_line *ll, size_t elem);
1150 
1151 /**
1152  Release all log line items (key/value pairs) in log line ll.
1153  This frees whichever keys and values were dynamically allocated.
1154 
1155  @param ll log_line
1156 */
1158 
1159 /**
1160  Release log line item (key/value pair) with the index elem in log line ll.
1161  This frees whichever of key and value were dynamically allocated.
1162  This moves any trailing items to fill the "gap" and decreases the counter
1163  of elements in the log line. If the intention is to leave a "gap" in the
1164  bag that may immediately be overwritten with an updated element, use
1165  log_line_item_free() instead!
1166 
1167  @param ll log_line
1168  @param elem index of the key/value pair to release
1169 */
1170 void log_line_item_remove(log_line *ll, int elem);
1171 
1172 /**
1173  Find the (index of the) last key/value pair of the given name
1174  in the log line.
1175 
1176  @param ll log line
1177  @param key the key to look for
1178 
1179  @retval -1: none found
1180  @retval -2: invalid search-key given
1181  @retval -3: no log_line given
1182  @retval >=0: index of the key/value pair in the log line
1183 */
1184 int log_line_index_by_name(log_line *ll, const char *key);
1185 
1186 /**
1187  Find the last item matching the given key in the log line.
1188 
1189  @param ll log line
1190  @param key the key to look for
1191 
1192  @retval nullptr item not found
1193  @retval otherwise pointer to the item (not a copy thereof!)
1194 */
1195 log_item *log_line_item_by_name(log_line *ll, const char *key);
1196 
1197 /**
1198  Find the (index of the) first key/value pair of the given type
1199  in the log line.
1200 
1201  @param ll log line
1202  @param t the log item type to look for
1203 
1204  @retval <0: none found
1205  @retval >=0: index of the key/value pair in the log line
1206 */
1208 
1209 /**
1210  Find the (index of the) first key/value pair of the given type
1211  in the log line. This variant accepts a reference item and looks
1212  for an item that is of the same type (for wellknown types), or
1213  one that is of a generic type, and with the same key name (for
1214  generic types). For example, a reference item containing a
1215  generic string with key "foo" will a generic string, integer, or
1216  float with the key "foo".
1217 
1218  @param ll log line
1219  @param ref a reference item of the log item type to look for
1220 
1221  @retval <0: none found
1222  @retval >=0: index of the key/value pair in the log line
1223 */
1225 
1226 /**
1227  Initializes a log entry for use. This simply puts it in a defined
1228  state; if you wish to reset an existing item, see log_item_free().
1229 
1230  @param li the log-item to initialize
1231 */
1232 void log_item_init(log_item *li);
1233 
1234 /**
1235  Initializes an entry in a log line for use. This simply puts it in
1236  a defined state; if you wish to reset an existing item, see
1237  log_item_free().
1238  This resets the element beyond the last. The element count is not
1239  adjusted; this is for the caller to do once it sets up a valid
1240  element to suit its needs in the cleared slot. Finally, it is up
1241  to the caller to make sure that an element can be allocated.
1242 
1243  @param ll the log-line to initialize a log_item in
1244 
1245  @retval the address of the cleared log_item
1246 */
1248 
1249 /**
1250  Create new log item with key name "key", and allocation flags of
1251  "alloc" (see enum_log_item_free).
1252  Will return a pointer to the item's log_item_data struct for
1253  convenience.
1254  This is mostly interesting for filters and other services that create
1255  items that are not part of a log_line; sources etc. that intend to
1256  create an item for a log_line (the more common case) should usually
1257  use the below line_item_set_with_key() which creates an item (like
1258  this function does), but also correctly inserts it into a log_line.
1259 
1260  @param li the log_item to work on
1261  @param t the item-type
1262  @param key the key to set on the item.
1263  ignored for non-generic types (may pass nullptr for those)
1264  see alloc
1265  @param alloc LOG_ITEM_FREE_KEY if key was allocated by caller
1266  LOG_ITEM_FREE_NONE if key was not allocated
1267  Allocated keys will automatically free()d when the
1268  log_item is.
1269  The log_item's alloc flags will be set to the
1270  submitted value; specifically, any pre-existing
1271  value will be clobbered. It is therefore WRONG
1272  a) to use this on a log_item that already has a key;
1273  it should only be used on freshly init'd log_items;
1274  b) to use this on a log_item that already has a
1275  value (specifically, an allocated one); the correct
1276  order is to init a log_item, then set up type and
1277  key, and finally to set the value. If said value is
1278  an allocated string, the log_item's alloc should be
1279  bitwise or'd with LOG_ITEM_FREE_VALUE.
1280 
1281  @retval a pointer to the log_item's log_data, for easy chaining:
1282  log_item_set_with_key(...)->data_integer= 1;
1283 */
1285  const char *key, uint32 alloc);
1286 
1287 /**
1288  Create new log item in log line "ll", with key name "key", and
1289  allocation flags of "alloc" (see enum_log_item_free).
1290  It is up to the caller to ensure the log_line can accept more items
1291  (e.g. by using log_line_full(ll)).
1292  On success, the number of registered items on the log line is increased,
1293  the item's type is added to the log_line's "seen" property,
1294  and a pointer to the item's log_item_data struct is returned for
1295  convenience.
1296 
1297  @param ll the log_line to work on
1298  @param t the item-type
1299  @param key the key to set on the item.
1300  ignored for non-generic types (may pass nullptr for those)
1301  see alloc
1302  @param alloc LOG_ITEM_FREE_KEY if key was allocated by caller
1303  LOG_ITEM_FREE_NONE if key was not allocated
1304  Allocated keys will automatically free()d when the
1305  log_item is.
1306  The log_item's alloc flags will be set to the
1307  submitted value; specifically, any pre-existing
1308  value will be clobbered. It is therefore WRONG
1309  a) to use this on a log_item that already has a key;
1310  it should only be used on freshly init'd log_items;
1311  b) to use this on a log_item that already has a
1312  value (specifically, an allocated one); the correct
1313  order is to init a log_item, then set up type and
1314  key, and finally to set the value. If said value is
1315  an allocated string, the log_item's alloc should be
1316  bitwise or'd with LOG_ITEM_FREE_VALUE.
1317 
1318  @retval a pointer to the log_item's log_data, for easy chaining:
1319  log_line_item_set_with_key(...)->data_integer= 1;
1320 */
1322  const char *key, uint32 alloc);
1323 
1324 /**
1325  As log_item_set_with_key(), except that the key is automatically
1326  derived from the wellknown log_item_type t.
1327 
1328  Create new log item with type "t".
1329  Will return a pointer to the item's log_item_data struct for
1330  convenience.
1331  This is mostly interesting for filters and other services that create
1332  items that are not part of a log_line; sources etc. that intend to
1333  create an item for a log_line (the more common case) should usually
1334  use the below line_item_set_with_key() which creates an item (like
1335  this function does), but also correctly inserts it into a log_line.
1336 
1337  The allocation of this item will be LOG_ITEM_FREE_NONE;
1338  specifically, any pre-existing value will be clobbered.
1339  It is therefore WRONG
1340  a) to use this on a log_item that already has a key;
1341  it should only be used on freshly init'd log_items;
1342  b) to use this on a log_item that already has a
1343  value (specifically, an allocated one); the correct
1344  order is to init a log_item, then set up type and
1345  key, and finally to set the value. If said value is
1346  an allocated string, the log_item's alloc should be
1347  bitwise or'd with LOG_ITEM_FREE_VALUE.
1348 
1349  @param li the log_item to work on
1350  @param t the item-type
1351 
1352  @retval a pointer to the log_item's log_data, for easy chaining:
1353  log_item_set_with_key(...)->data_integer= 1;
1354 */
1356 
1357 /**
1358  Create a new log item of well-known type "t" in log line "ll".
1359  On success, the number of registered items on the log line is increased,
1360  the item's type is added to the log_line's "seen" property,
1361  and a pointer to the item's log_item_data struct is returned for
1362  convenience.
1363 
1364  It is up to the caller to ensure the log_line can accept more items
1365  (e.g. by using log_line_full(ll)).
1366 
1367  The allocation of this item will be LOG_ITEM_FREE_NONE;
1368  specifically, any pre-existing value will be clobbered.
1369  It is therefore WRONG
1370  a) to use this on a log_item that already has a key;
1371  it should only be used on freshly init'd log_items;
1372  b) to use this on a log_item that already has a
1373  value (specifically, an allocated one); the correct
1374  order is to init a log_item, then set up type and
1375  key, and finally to set the value. If said value is
1376  an allocated string, the log_item's alloc should be
1377  bitwise or'd with LOG_ITEM_FREE_VALUE.
1378 
1379  @param ll the log_line to work on
1380  @param t the item-type
1381 
1382  @retval a pointer to the log_item's log_data, for easy chaining:
1383  log_line_item_set_with_key(...)->data_integer= 1;
1384 */
1386 
1387 /**
1388  Convenience function: Derive a log label ("error", "warning",
1389  "information") from a severity.
1390 
1391  @param prio the severity/prio in question
1392 
1393  @return a label corresponding to that priority.
1394  @retval "ERROR" for prio of ERROR_LEVEL or higher
1395  @retval "Warning" for prio of WARNING_LEVEL
1396  @retval "Note" otherwise
1397 */
1398 const char *log_label_from_prio(int prio);
1399 
1400 /**
1401  Derive the event's priority (SYSTEM_LEVEL, ERROR_LEVEL, ...)
1402  from a textual label. If the label can not be identified,
1403  default to ERROR_LEVEL as it is better to keep something
1404  that needn't be kept than to discard something that shouldn't
1405  be.
1406 
1407  @param label The prio label as a \0 terminated C-string.
1408 
1409  @retval the priority (as an enum loglevel)
1410 */
1411 enum loglevel log_prio_from_label(const char *label);
1412 
1413 /**
1414  Complete, filter, and write submitted log items.
1415 
1416  This expects a log_line collection of log-related key/value pairs,
1417  e.g. from log_message().
1418 
1419  Where missing, timestamp, priority, thread-ID (if any) and so forth
1420  are added.
1421 
1422  Log item source services, log item filters, and log item sinks are
1423  then called.
1424 
1425  @param ll key/value pairs describing info to log
1426 
1427  @retval int number of fields in created log line
1428 */
1429 int log_line_submit(log_line *ll);
1430 
1431 /**
1432  Whether to generate a UTC timestamp, or one following system-time.
1433  These values are not arbitrary; they must correspond to the range
1434  and meaning of opt_log_timestamps.
1435 */
1437  iso8601_sysvar_logtimestamps = -1, /**< use value of opt_log_timestamps */
1438  iso8601_utc = 0, /**< create UTC timestamp */
1439  iso8601_system_time = 1 /**< use system time */
1440 };
1441 
1442 /**
1443  Make and return an ISO 8601 / RFC 3339 compliant timestamp.
1444  Accepts the log_timestamps global variable in its third parameter.
1445 
1446  @param buf A buffer of at least iso8601_size bytes to store
1447  the timestamp in. The timestamp will be \0 terminated.
1448  @param utime Microseconds since the epoch
1449  @param mode if 0, use UTC; if 1, use local time
1450 
1451  @retval length of timestamp (excluding \0)
1452 */
1453 int make_iso8601_timestamp(char *buf, ulonglong utime,
1454  enum enum_iso8601_tzmode mode);
1455 
1456 /**
1457  Parse a ISO8601 timestamp and return the number of microseconds
1458  since the epoch. Heeds +/- timezone info if present.
1459 
1460  @see make_iso8601_timestamp()
1461 
1462  @param timestamp an ASCII string containing an ISO8601 timestamp
1463  @param len Length in bytes of the aforementioned string
1464 
1465  @return microseconds since the epoch
1466 */
1467 ulonglong iso8601_timestamp_to_microseconds(const char *timestamp, size_t len);
1468 
1469 /**
1470  Well-known values returned by log_error_stack().
1471 */
1473  /// success
1475 
1476  /// expected delimiter not found
1478 
1479  /// one or more services not found
1481 
1482  /// couldn't create service cache entry
1484 
1485  /// tried to multi-open singleton
1487 
1488  /// couldn't create service instance entry
1490 
1491  /// last element in pipeline should be a sink
1493 
1494  /// service only available during start-up (may not be set by the user)
1496 
1497  /// check-only warning: no sink supporting pfs given
1499 
1500  /// check-only warning: no log-parser given
1502 
1503  /// check-only warning: more than one log-filter given
1505 
1506  /// service name may not start with a delimiter
1508 
1509  /// delimiters ',' and ';' may not be mixed
1512 
1513 /**
1514  Set up custom error logging stack.
1515 
1516  @param conf The configuration string
1517  @param check_only If true, report on whether configuration is valid
1518  (i.e. whether all requested services are available),
1519  but do not apply the new configuration.
1520  if false, set the configuration (acquire the
1521  necessary services, update the hash by
1522  adding/deleting entries as necessary)
1523  @param[out] pos If an error occurs and this pointer is non-null,
1524  the position in the configuration string where
1525  the error occurred will be written to the
1526  pointed-to size_t.
1527 
1528  @retval LOG_ERROR_STACK_SUCCESS success
1529 
1530  @retval LOG_ERROR_STACK_DELIMITER_MISSING expected delimiter not found
1531 
1532  @retval LOG_ERROR_STACK_SERVICE_MISSING one or more services not found
1533 
1534  @retval LOG_ERROR_STACK_CACHE_ENTRY_OOM couldn't create service cache
1535  entry
1536 
1537  @retval LOG_ERROR_STACK_MULTITON_DENIED tried to multi-open singleton
1538 
1539  @retval LOG_ERROR_STACK_SERVICE_INSTANCE_OOM couldn't create service
1540  instance entry
1541 
1542  @retval LOG_ERROR_STACK_ENDS_IN_NON_SINK last element should be a sink
1543 
1544  @retval LOG_ERROR_STACK_SERVICE_UNAVAILABLE service only available during
1545  start-up (may not be set by the
1546  user)
1547 
1548 
1549  @retval LOG_ERROR_STACK_NO_PFS_SUPPORT (check_only warning)
1550  no sink with performance_schema
1551  support selected
1552 
1553  @retval LOG_ERROR_STACK_NO_LOG_PARSER (check_only warning)
1554  no sink providing a log-parser
1555  selected
1556 
1557  @retval LOG_ERROR_MULTIPLE_FILTERS (check_only warning)
1558  more than one filter service
1559  selected
1560 
1561  @retval LOG_ERROR_UNEXPECTED_DELIMITER_FOUND service starts with a delimiter
1562 
1563  @retval LOG_ERROR_MIXED_DELIMITERS use ',' or ';', not both!
1564 */
1566  bool check_only, size_t *pos);
1567 
1568 /**
1569  Call flush() on all log_services.
1570  flush() function must not try to log anything, as we hold an
1571  exclusive lock on the stack.
1572 
1573  @returns 0 if no problems occurred, otherwise the negative count
1574  of the components that failed to flush
1575 */
1577 
1578 /**
1579  Initialize the structured logging subsystem.
1580 
1581  Since we're initializing various locks here, we must call this late enough
1582  so this is clean, but early enough so it still happens while we're running
1583  single-threaded -- this specifically also means we must call it before we
1584  start plug-ins / storage engines / external components!
1585 
1586  @retval 0 no errors
1587  @retval -1 couldn't initialize stack lock
1588  @retval -2 couldn't initialize built-in default filter
1589  @retval -3 couldn't set up service hash
1590  @retval -4 couldn't initialize syseventlog lock
1591  @retval -5 couldn't set service pipeline
1592  @retval -6 couldn't initialize buffered logging lock
1593 */
1594 int log_builtins_init();
1595 
1596 /**
1597  De-initialize the structured logging subsystem.
1598 
1599  @retval 0 no errors
1600  @retval -1 not stopping, never started
1601 */
1602 int log_builtins_exit();
1603 
1604 /**
1605  Interim helper: write to the default error stream
1606 
1607  @param buffer buffer containing serialized error message
1608  @param length number of bytes in buffer
1609 */
1610 void log_write_errstream(const char *buffer, size_t length);
1611 
1612 #endif /* LOG_H */
Definition: log.h:674
void print_summary(ulong suppressed)
Actually print the prepared summary to log.
Definition: log.h:683
const char * subsys
Definition: log.h:678
loglevel ll
Definition: log.h:676
Error_log_throttle(ulong window_usecs, loglevel lvl, uint errcode, const char *subsystem, const char *msg)
Definition: log.h:700
uint err_code
Definition: log.h:677
Abstract superclass for handling logging to slow/general logs.
Definition: log.h:158
virtual ~Log_event_handler()=default
virtual bool log_general(THD *thd, ulonglong event_utime, const char *user_host, size_t user_host_len, my_thread_id thread_id, const char *command_type, size_t command_type_len, const char *sql_text, size_t sql_text_len, const CHARSET_INFO *client_cs)=0
Log command to the general log.
virtual bool log_slow(THD *thd, ulonglong current_utime, ulonglong query_start_arg, const char *user_host, size_t user_host_len, ulonglong query_utime, ulonglong lock_utime, bool is_command, const char *sql_text, size_t sql_text_len, struct System_status_var *query_start_status)=0
Log a query to the slow log.
Log_event_handler()=default
Base class for rate-limiting a log (slow query log etc.)
Definition: log.h:497
bool in_window(ulonglong now) const
Check whether we're still in the current window.
Definition: log.h:544
const ulong window_size
Log no more than rate lines of a given type per window_size (e.g.
Definition: log.h:507
Log_throttle(ulong window_usecs, const char *msg)
Definition: log.h:562
bool inc_log_count(ulong rate)
Increase count of logs we're handling.
Definition: log.h:537
ulong count
There have been this many lines of this type in this window, including those that we suppressed.
Definition: log.h:515
ulonglong window_end
When will/did current window end?
Definition: log.h:501
const char * summary_template
Template for the summary line.
Definition: log.h:522
Class responsible for table based logging.
Definition: log.h:226
bool log_slow(THD *thd, ulonglong current_utime, ulonglong query_start_arg, const char *user_host, size_t user_host_len, ulonglong query_utime, ulonglong lock_utime, bool is_command, const char *sql_text, size_t sql_text_len, struct System_status_var *query_start_status) override
Definition: log.cc:987
bool activate_log(THD *thd, enum_log_table_type log_type)
Check if log table for given log type exists and can be opened.
Definition: log.cc:1160
bool log_general(THD *thd, ulonglong event_utime, const char *user_host, size_t user_host_len, my_thread_id thread_id, const char *command_type, size_t command_type_len, const char *sql_text, size_t sql_text_len, const CHARSET_INFO *client_cs) override
Definition: log.cc:861
Class responsible for file based logging.
Definition: log.cc:1196
Class which manages slow and general log event handlers.
Definition: log.h:264
bool is_log_file_enabled(enum_log_table_type log_type) const
Check if file logging is turned on for the given log type.
Definition: log.cc:1577
enum_log_table_type check_if_log_table(TABLE_LIST *table_list, bool check_if_opened) const
Check if given TABLE_LIST has a query log table name and optionally check if the query log is current...
Definition: log.cc:1553
mysql_rwlock_t LOCK_logger
RW-lock protecting Query_logger.
Definition: log.h:276
Query_logger()
Definition: log.h:296
Log_event_handler * slow_log_handler_list[MAX_LOG_HANDLERS_NUM+1]
NULL-terminated arrays of log handlers.
Definition: log.h:283
bool general_log_write(THD *thd, enum_server_command command, const char *query, size_t query_length)
Write query to general query log.
Definition: log.cc:1378
bool activate_log_handler(THD *thd, enum_log_table_type log_type)
Activate log handlers for the given log type.
Definition: log.cc:1504
bool reopen_log_file(enum_log_table_type log_type)
Close file log for the given log type and the reopen it.
Definition: log.cc:1545
Log_to_file_event_handler * file_log_handler
Definition: log.h:280
bool general_log_print(THD *thd, enum_server_command command, const char *format,...)
Write printf style message to general query log.
Definition: log.cc:1414
void cleanup()
Free memory.
Definition: log.cc:1288
void deactivate_log_handler(enum_log_table_type log_type)
Close file log for the given log type.
Definition: log.cc:1517
Log_event_handler * general_log_handler_list[MAX_LOG_HANDLERS_NUM+1]
Definition: log.h:284
Log_to_csv_event_handler table_log_handler
Available log handlers.
Definition: log.h:279
void set_handlers(ulonglong log_printer)
Enable log event handlers for slow/general log.
Definition: log.cc:1495
bool set_log_file(enum_log_table_type log_type)
Read log file name from global variable opt_*_logname.
Definition: log.cc:1524
bool is_log_table_enabled(enum_log_table_type log_type) const
Check if table logging is turned on for the given log_type.
Definition: log.cc:1274
static const uint MAX_LOG_HANDLERS_NUM
Currently we have only 2 kinds of logging functions: old-fashioned file logs and csv logging routines...
Definition: log.h:269
void init()
Perform basic log initialization: create file-based log handler.
Definition: log.cc:1283
void init_query_log(enum_log_table_type log_type, ulonglong log_printer)
Setup log event handlers for the given log_type.
Definition: log.cc:1447
bool slow_log_write(THD *thd, const char *query, size_t query_length, struct System_status_var *query_start_status)
Log slow query with all enabled log event handlers.
Definition: log.cc:1297
A set of THD members describing the current authenticated user.
Definition: sql_security_ctx.h:53
Used for rate-limiting the slow query log.
Definition: log.h:581
mysql_mutex_t * LOCK_log_throttle
Slow_log_throttle is shared between THDs.
Definition: log.h:618
ulong * rate
A reference to the threshold ("no more than n log lines per ...").
Definition: log.h:607
Security_context aggregate_sctx
We're using our own (empty) security context during summary generation.
Definition: log.h:589
ulonglong total_lock_time
Total of the lock times of queries in this time-window for which we suppressed logging.
Definition: log.h:601
ulonglong total_exec_time
Total of the execution times of queries in this time-window for which we suppressed logging.
Definition: log.h:595
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
Dialog Client Authentication nullptr
Definition: dialog.cc:352
char * pos
Definition: do_ctype.cc:76
int log_item_wellknown_by_type(log_item_type t)
See whether a type is wellknown.
Definition: log_builtins.cc:401
log_item_data * log_item_set_with_key(log_item *li, log_item_type t, const char *key, uint32 alloc)
Create new log item with key name "key", and allocation flags of "alloc" (see enum_log_item_free).
Definition: log_builtins.cc:780
int log_string_compare(const char *a, const char *b, size_t len, bool case_insensitive)
Modular logger: log line and key/value manipulation helpers.
Definition: log_builtins.cc:284
void log_item_free(log_item *li)
Release any of key and value on a log-item that were dynamically allocated.
Definition: log_builtins.cc:507
my_thread_id log_get_thread_id(THD *thd)
A helper that we can stubify so we don't have to pull all of THD into the unit tests.
Definition: log.cc:2021
enum_iso8601_tzmode
Whether to generate a UTC timestamp, or one following system-time.
Definition: log.h:1436
@ iso8601_utc
create UTC timestamp
Definition: log.h:1438
@ iso8601_sysvar_logtimestamps
use value of opt_log_timestamps
Definition: log.h:1437
@ iso8601_system_time
use system time
Definition: log.h:1439
bool log_item_set_lexstring(log_item_data *lid, const char *s, size_t s_len)
Set a string value on a log_item.
Definition: log_builtins.cc:901
bool log_item_generic_type(log_item_type t)
Predicate used to determine whether a type is generic (generic string, generic float,...
Definition: log_builtins.cc:312
int log_line_index_by_item(log_line *ll, log_item *ref)
Find the (index of the) first key/value pair of the given type in the log line.
Definition: log_builtins.cc:699
const char * log_item_wellknown_get_name(uint idx)
Accessor: from a record describing a wellknown key, get its name.
Definition: log_builtins.cc:420
void log_write_errstream(const char *buffer, size_t length)
Interim helper: write to the default error stream.
Definition: log.cc:2004
bool init_error_log()
Initialize structures (e.g.
Definition: log.cc:1885
Query_logger query_logger
Definition: log.cc:1581
log_item * log_line_item_by_name(log_line *ll, const char *key)
Find the last item matching the given key in the log line.
Definition: log_builtins.cc:681
log_item * log_line_get_output_buffer(log_line *ll)
Get log-line's output buffer.
Definition: log_builtins.cc:551
void log_line_item_free(log_line *ll, size_t elem)
Release log line item (key/value pair) with the index elem in log line ll.
Definition: log_builtins.cc:603
bool log_item_set_float(log_item_data *lid, double f)
Set a floating point value on a log_item.
Definition: log_builtins.cc:867
enum loglevel log_prio_from_label(const char *label)
Derive the event's priority (SYSTEM_LEVEL, ERROR_LEVEL, ...) from a textual label.
Definition: log_builtins.cc:946
bool log_item_string_class(log_item_class c)
Predicate used to determine whether a class is a string class (C-string or Lex-string).
Definition: log_builtins.cc:327
void destroy_error_log()
Free any error log resources.
Definition: log.cc:1952
log_item_data * log_item_set(log_item *li, log_item_type t)
As log_item_set_with_key(), except that the key is automatically derived from the wellknown log_item_...
Definition: log_builtins.cc:851
enum enum_log_error_stack_error log_error_stack_error
Well-known values returned by log_error_stack().
bool log_item_numeric_class(log_item_class c)
Predicate used to determine whether a class is a numeric class (integer or float).
Definition: log_builtins.cc:340
int log_line_submit(log_line *ll)
Complete, filter, and write submitted log items.
Definition: log_builtins.cc:970
static const uint LOG_FILE
Definition: log.h:258
void log_item_init(log_item *li)
Initializes a log entry for use.
Definition: log_builtins.cc:725
bool reopen_error_log()
Flush any pending data to disk and reopen the error log.
Definition: log.cc:1964
log_line * log_line_init()
Initialize a log_line.
Definition: log_builtins.cc:522
void log_line_exit(log_line *ll)
Release a log_line allocated with log_line_init.
Definition: log_builtins.cc:535
void log_item_get_int(log_item *li, longlong *i)
Get an integer value from a log-item of float or integer type.
Definition: log_builtins.cc:344
log_error_stack_error log_builtins_error_stack(const char *conf, bool check_only, size_t *pos)
Set up custom error logging stack.
Definition: log_builtins.cc:1709
char * make_query_log_name(char *buff, enum_log_table_type log_type)
Create the name of the query log specified.
Definition: log.cc:1583
log_item_class log_item_wellknown_get_class(uint idx)
Accessor: from a record describing a wellknown key, get its class.
Definition: log_builtins.cc:442
enum_log_error_stack_error
Well-known values returned by log_error_stack().
Definition: log.h:1472
@ LOG_ERROR_STACK_SERVICE_MISSING
one or more services not found
Definition: log.h:1480
@ LOG_ERROR_STACK_NO_PFS_SUPPORT
check-only warning: no sink supporting pfs given
Definition: log.h:1498
@ LOG_ERROR_STACK_SERVICE_INSTANCE_OOM
couldn't create service instance entry
Definition: log.h:1489
@ LOG_ERROR_STACK_MULTITON_DENIED
tried to multi-open singleton
Definition: log.h:1486
@ LOG_ERROR_STACK_CACHE_ENTRY_OOM
couldn't create service cache entry
Definition: log.h:1483
@ LOG_ERROR_STACK_SUCCESS
success
Definition: log.h:1474
@ LOG_ERROR_MULTIPLE_FILTERS
check-only warning: more than one log-filter given
Definition: log.h:1504
@ LOG_ERROR_STACK_ENDS_IN_NON_SINK
last element in pipeline should be a sink
Definition: log.h:1492
@ LOG_ERROR_UNEXPECTED_DELIMITER_FOUND
service name may not start with a delimiter
Definition: log.h:1507
@ LOG_ERROR_STACK_DELIMITER_MISSING
expected delimiter not found
Definition: log.h:1477
@ LOG_ERROR_STACK_NO_LOG_PARSER
check-only warning: no log-parser given
Definition: log.h:1501
@ LOG_ERROR_STACK_SERVICE_UNAVAILABLE
service only available during start-up (may not be set by the user)
Definition: log.h:1495
@ LOG_ERROR_MIXED_DELIMITERS
delimiters ',' and ';' may not be mixed
Definition: log.h:1510
void log_item_get_float(log_item *li, double *f)
Get a float value from a log-item of float or integer type.
Definition: log_builtins.cc:352
int log_message(int log_type,...)
Variadic convenience function for logging.
Definition: log.cc:2332
static const uint LOG_NONE
Definition: log.h:257
bool log_line_full(log_line *ll)
Predicate indicating whether a log line is "willing" to accept any more key/value pairs.
Definition: log_builtins.cc:566
int log_line_item_count(log_line *ll)
How many items are currently set on the given log_line?
Definition: log_builtins.cc:577
int log_line_index_by_type(log_line *ll, log_item_type t)
Find the (index of the) first key/value pair of the given type in the log line.
Definition: log_builtins.cc:686
void log_slow_do(THD *thd, struct System_status_var *query_start_status)
Unconditionally writes the current statement (or its rewritten version if it exists) to the slow quer...
Definition: log.cc:1638
bool log_slow_applicable(THD *thd)
Check whether we need to write the current statement (or its rewritten version if it exists) to the s...
Definition: log.cc:1597
log_item * log_line_item_init(log_line *ll)
Initializes an entry in a log line for use.
Definition: log_builtins.cc:740
int log_builtins_init()
Initialize the structured logging subsystem.
Definition: log_builtins.cc:1992
void log_line_item_remove(log_line *ll, int elem)
Release log line item (key/value pair) with the index elem in log line ll.
Definition: log_builtins.cc:630
void log_slow_statement(THD *thd, struct System_status_var *query_start_status)
Check whether we need to write the current statement to the slow query log.
Definition: log.cc:1665
int log_line_index_by_name(log_line *ll, const char *key)
Find the (index of the) last key/value pair of the given name in the log line.
Definition: log_builtins.cc:654
enum_log_table_type
Type of the log table.
Definition: log.h:148
@ QUERY_LOG_NONE
Definition: log.h:149
@ QUERY_LOG_GENERAL
Definition: log.h:151
@ QUERY_LOG_SLOW
Definition: log.h:150
log_item_type log_item_wellknown_get_type(uint idx)
Accessor: from a record describing a wellknown key, get its type.
Definition: log_builtins.cc:431
void error_log_print(enum loglevel level, uint ecode, va_list args)
Prints a printf style message to the error log.
Definition: log.cc:2309
log_item_data * log_line_item_set(log_line *ll, log_item_type t)
Create a new log item of well-known type "t" in log line "ll".
Definition: log_builtins.cc:855
Slow_log_throttle log_throttle_qni
int log_vmessage(int log_type, va_list lili)
Write a message to a log (for now just used for error log).
log_item_type_mask log_line_item_types_seen(log_line *ll, log_item_type_mask m)
Test whether a given type is presumed present on the log line.
Definition: log_builtins.cc:588
int make_iso8601_timestamp(char *buf, ulonglong utime, enum enum_iso8601_tzmode mode)
Make and return an ISO 8601 / RFC 3339 compliant timestamp.
Definition: log_builtins.cc:1281
const char * log_label_from_prio(int prio)
Convenience function: Derive a log label ("error", "warning", "information") from a severity.
Definition: log_builtins.cc:919
int log_builtins_exit()
De-initialize the structured logging subsystem.
Definition: log_builtins.cc:1950
int log_builtins_error_stack_flush()
Call flush() on all log_services.
Definition: log_builtins.cc:1590
bool log_item_set_cstring(log_item_data *lid, const char *s)
Set a string value on a log_item.
Definition: log_builtins.cc:910
bool is_valid_log_name(const char *name, size_t len)
Check given log name against certain blacklisted names/extensions.
Definition: log.cc:394
void discard_error_log_messages()
Discard all buffered messages and deallocate buffer without printing anything.
Definition: log.cc:1877
bool log_item_set_int(log_item_data *lid, longlong i)
Set an integer value on a log_item.
Definition: log_builtins.cc:859
void flush_error_log_messages()
We buffer all error log messages that have been printed before the error log has been opened.
Definition: log.cc:1881
void log_item_get_string(log_item *li, char **str, size_t *len)
Get a string value from a log-item of C-string or Lex string type.
Definition: log_builtins.cc:359
log_item_data * log_line_item_set_with_key(log_line *ll, log_item_type t, const char *key, uint32 alloc)
Create new log item in log line "ll", with key name "key", and allocation flags of "alloc" (see enum_...
Definition: log_builtins.cc:805
static const uint LOG_TABLE
Definition: log.h:259
ulonglong iso8601_timestamp_to_microseconds(const char *timestamp, size_t len)
Parse a ISO8601 timestamp and return the number of microseconds since the epoch.
Definition: log_builtins.cc:1351
bool open_error_log(const char *filename, bool get_lock)
Open the error log and redirect stderr and optionally stdout to the error log file.
Definition: log.cc:1902
void log_line_item_free_all(log_line *ll)
Release all log line items (key/value pairs) in log line ll.
Definition: log_builtins.cc:614
int log_item_wellknown_by_name(const char *key, size_t len)
See whether a string is a wellknown field name.
Definition: log_builtins.cc:378
#define window_size
Definition: log_event.cc:155
enum enum_log_item_class log_item_class
@ LOG_TYPE_ERROR
Definition: log_shared.h:67
@ LOG_ITEM_LOG_MESSAGE
the message, format string
Definition: log_shared.h:144
@ LOG_ITEM_LOG_PRIO
log priority (error, warn, ...)
Definition: log_shared.h:141
@ LOG_ITEM_SRV_SUBSYS
log called from subsystem ...
Definition: log_shared.h:134
@ LOG_ITEM_SQL_ERRCODE
mysql error code (numeric)
Definition: log_shared.h:126
enum enum_log_item_type log_item_type
item_type – what to log
uint64 log_item_type_mask
a bit mask of log_types.
Definition: log_shared.h:215
static int log_type
Definition: mi_log.cc:46
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:47
Header for compiler-dependent features.
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
long long int longlong
Definition: my_inttypes.h:54
uint32_t uint32
Definition: my_inttypes.h:66
Common #defines and includes for file and socket I/O.
Definition of the global "loglevel" enumeration.
loglevel
Definition: my_loglevel.h:40
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
static my_thread_id thread_id
Definition: my_thr_init.cc:62
uint32 my_thread_id
Definition: my_thread_local.h:33
static int count
Definition: myisam_ftdump.cc:42
static char * query
Definition: myisam_ftdump.cc:44
Common definition between mysql server & client.
ABI for instrumented mutexes.
Instrumentation helpers for rwlock.
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1056
Definition: buf0block_hint.cc:29
PT & ref(PT *tp)
Definition: tablespace_impl.cc:358
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:75
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:78
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:391
static mysql_service_status_t flush(reference_caching_cache cache) noexcept
Definition: component.cc:121
mode
Definition: file_handle.h:59
static Logger logger
The "top-level" logger used when no connection context is given.
Definition: test_trace_plugin.cc:295
const string timestamp("\"Timestamp\"")
const char * filename
Definition: pfs_example_component_population.cc:64
Instrumentation helpers for mutexes.
Performance schema instrumentation interface.
required string key
Definition: replication_asynchronous_connection_failover.proto:59
case opt name
Definition: sslopt-case.h:32
Definition: m_ctype.h:354
Per thread status variables.
Definition: system_variables.h:437
Definition: table.h:2694
Definition: log_shared.h:195
log_line ("log event")
Definition: keyring_log_builtins_definition.cc:70
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
An instrumented rwlock structure.
Definition: mysql_rwlock_bits.h:50
unsigned int uint
Definition: uca-dump.cc:29
Definition: log_shared.h:184
command
Definition: version_token.cc:279