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