MySQL 9.6.0
Source Code Documentation
log.h
Go to the documentation of this file.
1/* Copyright (c) 2005, 2025, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/**
25 @file sql/log.h
26
27 Error logging, slow query logging, general query logging:
28 If it's server-internal, and it's logging, it's here.
29
30 Components/services should NOT include this, but include
31 include/mysql/components/services/log_builtins.h instead
32 to gain access to the error logging stack.
33
34 Legacy plugins (pre-"services") will likely include
35 include/mysql/service_my_plugin_log.h instead.
36*/
37
38#ifndef LOG_H
39#define LOG_H
40
43#include <stdarg.h>
44#include <stddef.h>
45#include <sys/types.h>
46
47#include "lex_string.h"
48#include "my_command.h"
49#include "my_compiler.h"
50#include "my_dbug.h"
51#include "my_inttypes.h"
52#include "my_io.h"
53#include "my_psi_config.h"
54#include "my_thread_local.h"
58#include "mysql/my_loglevel.h"
60#include "mysql_com.h"
61#include "sql/auth/sql_security_ctx.h" // Security_context
62
63class THD;
64struct CHARSET_INFO;
65class Table_ref;
66
67////////////////////////////////////////////////////////////
68//
69// Slow/General Log
70//
71////////////////////////////////////////////////////////////
72
73/*
74 System variables controlling logging:
75
76 log_output (--log-output)
77 Values: NONE, FILE, TABLE
78 Select output destination. Does not enable logging.
79 Can set more than one (e.g. TABLE | FILE).
80
81 general_log (--general_log)
82 slow_query_log (--slow_query_log)
83 Values: 0, 1
84 Enable/disable general/slow query log.
85
86 general_log_file (--general-log-file)
87 slow_query_log_file (--slow-query-log-file)
88 Values: filename
89 Set name of general/slow query log file.
90
91 sql_log_off
92 Values: ON, OFF
93 Enable/disable general query log (OPTION_LOG_OFF).
94
95 log_queries_not_using_indexes (--log-queries-not-using-indexes)
96 Values: ON, OFF
97 Control slow query logging of queries that do not use indexes.
98
99 --log-raw
100 Values: ON, OFF
101 Control query rewrite of passwords to the general log.
102
103 --log-short-format
104 Values: ON, OFF
105 Write short format to the slow query log (and the binary log).
106
107 --log-slow-admin-statements
108 Values: ON, OFF
109 Log statements such as OPTIMIZE TABLE, ALTER TABLE to the slow query log.
110
111 --log-slow-replica-statements
112 Values: ON, OFF
113
114 log_throttle_queries_not_using_indexes
115 Values: INT
116 Number of queries not using indexes logged to the slow query log per min.
117*/
118
119/**
120 Write a message to a log (for now just used for error log).
121 This is a variadic convenience interface to the logging components
122 (which use the log_line structure internally), e.g.
123
124 log_message(LOG_TYPE_ERROR,
125 LOG_ITEM_LOG_PRIO, INFORMATION_LEVEL,
126 LOG_ITEM_LOG_MESSAGE, "file %s is %f %% yellow",
127 filename, yellowfication);
128
129 For use by legacy sql_print_*(), legacy my_plugin_log_message();
130 also available via the log_builtins service as message().
131
132 Wherever possible, use the fluent C++ wrapper LogErr()
133 (see log_builtins.h) instead.
134
135 See log_shared.h for LOG_TYPEs as well as for allowed LOG_ITEM_ types.
136*/
137int log_vmessage(int log_type, va_list lili);
138int log_message(int log_type, ...);
139
140/**
141 A helper that we can stubify so we don't have to pull all of THD
142 into the unit tests.
143
144 @param thd a thd
145 @retval its thread-ID
146*/
148
149/** Type of the log table */
155
156/**
157 Abstract superclass for handling logging to slow/general logs.
158 Currently has two subclasses, for table and file based logging.
159*/
161 public:
162 Log_event_handler() = default;
163 virtual ~Log_event_handler() = default;
164
165 /**
166 Log a query to the slow log.
167
168 @param thd THD of the query
169 @param current_utime Current timestamp in microseconds
170 @param query_start_arg Command start timestamp in microseconds
171 @param user_host The pointer to the string with user\@host info
172 @param user_host_len Length of the user_host string
173 @param query_utime Number of microseconds query execution took
174 @param lock_utime Number of microseconds the query was locked
175 @param is_command The flag which determines whether the sql_text
176 is a query or an administrator command
177 @param sql_text The query or administrator in textual form
178 @param sql_text_len The length of sql_text string
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) = 0;
187
188 /**
189 Log command to the general log.
190
191 @param thd THD of the query
192 @param event_utime Command start timestamp in micro seconds
193 @param user_host The pointer to the string with user\@host info
194 @param user_host_len Length of the user_host string. this is computed
195 once and passed to all general log event handlers
196 @param thread_id Id of the thread, issued a query
197 @param command_type The type of the command being logged
198 @param command_type_len The length of the string above
199 @param sql_text The very text of the query being executed
200 @param sql_text_len The length of sql_text string
201 @param client_cs Character set to use for strings
202
203 @return This function attempts to never call my_error(). This is
204 necessary, because general logging happens already after a statement
205 status has been sent to the client, so the client can not see the
206 error anyway. Besides, the error is not related to the statement
207 being executed and is internal, and thus should be handled
208 internally (@todo: how?).
209 If a write to the table has failed, the function attempts to
210 write to a short error message to the file. The failure is also
211 indicated in the return value.
212
213 @retval false OK
214 @retval true error occurred
215 */
216 virtual bool log_general(THD *thd, ulonglong event_utime,
217 const char *user_host, size_t user_host_len,
218 my_thread_id thread_id, const char *command_type,
219 size_t command_type_len, const char *sql_text,
220 size_t sql_text_len,
221 const CHARSET_INFO *client_cs) = 0;
222};
223
224/** Class responsible for table based logging. */
226 public:
227 /** @see Log_event_handler::log_slow(). */
228 bool log_slow(THD *thd, ulonglong current_utime, ulonglong query_start_arg,
229 const char *user_host, size_t user_host_len,
230 ulonglong query_utime, ulonglong lock_utime, bool is_command,
231 const char *sql_text, size_t sql_text_len) override;
232
233 /** @see Log_event_handler::log_general(). */
234 bool log_general(THD *thd, ulonglong event_utime, const char *user_host,
235 size_t user_host_len, my_thread_id thread_id,
236 const char *command_type, size_t command_type_len,
237 const char *sql_text, size_t sql_text_len,
238 const CHARSET_INFO *client_cs) override;
239
240 private:
241 /**
242 Check if log table for given log type exists and can be opened.
243
244 @param thd Thread handle
245 @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
246
247 @return true if table could not be opened, false otherwise.
248 */
250
251 friend class Query_logger;
252};
253
254/* Log event handler flags */
255static const uint LOG_NONE = 1;
256static const uint LOG_FILE = 2;
257static const uint LOG_TABLE = 4;
258
260class Log_to_telemetry_event_handler;
261
262/** Class which manages slow and general log event handlers. */
264 /**
265 Currently we have 2 main kinds of logging functions: old-fashioned
266 file logs and csv logging routines.
267 Additional open telemetry log export is completely independent of
268 these and does not depend on their configuration.
269 */
270 static const uint MAX_LOG_HANDLERS_NUM = 2;
271
272 /**
273 RW-lock protecting Query_logger.
274 R-lock taken when writing to slow/general query log.
275 W-lock taken when activating/deactivating logs.
276 */
278
279 /** Available log handlers. */
282 Log_to_telemetry_event_handler *telemetry_log_handler;
283
284 /** NULL-terminated arrays of log handlers. */
287
288 private:
289 /**
290 Setup log event handlers for the given log_type.
291
292 @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
293 @param log_printer Bitmap of LOG_NONE, LOG_FILE, LOG_TABLE
294 */
296
297 public:
299
300 /**
301 Check if table logging is turned on for the given log_type.
302
303 @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
304
305 @return true if table logging is on, false otherwise.
306 */
308
309 /**
310 Check if file logging is turned on for the given log type.
311
312 @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
313
314 @return true if the file logging is on, false otherwise.
315 */
317
318 /**
319 Perform basic log initialization: create file-based log handler.
320
321 We want to initialize all log mutexes as soon as possible,
322 but we cannot do it in constructor, as safe_mutex relies on
323 initialization, performed by MY_INIT(). This why this is done in
324 this function.
325 */
326 void init();
327
328 /** Free memory. Nothing could be logged after this function is called. */
329 void cleanup();
330
331 /**
332 Log slow query with all enabled log event handlers.
333
334 @param thd THD of the statement being logged.
335 @param query The query string being logged.
336 @param query_length The length of the query string.
337 @param aggregate True if writing log throttle record
338 @param lock_usec Lock time, in microseconds.
339 Only used when aggregate is true.
340 @param exec_usec Execution time, in microseconds.
341 Only used when aggregate is true.
342
343 @return true if error, false otherwise.
344 */
345 bool slow_log_write(THD *thd, const char *query, size_t query_length,
346 bool aggregate, ulonglong lock_usec, ulonglong exec_usec);
347
348 /**
349 Write printf style message to general query log.
350
351 @param thd THD of the statement being logged.
352 @param command COM of statement being logged.
353 @param format Printf style format of message.
354 @param ... Printf parameters to write.
355
356 @return true if error, false otherwise.
357 */
358 bool general_log_print(THD *thd, enum_server_command command,
359 const char *format, ...)
360 MY_ATTRIBUTE((format(printf, 4, 5)));
361
362 /**
363 Write query to general query log.
364
365 @param thd THD of the statement being logged.
366 @param command COM of statement being logged.
367 @param query The query string being logged.
368 @param query_length The length of the query string.
369
370 @return true if error, false otherwise.
371 */
372 bool general_log_write(THD *thd, enum_server_command command,
373 const char *query, size_t query_length);
374
375 /**
376 Enable log event handlers for slow/general log.
377
378 @param log_printer Bitmask of log event handlers.
379
380 @note Acceptable values are LOG_NONE, LOG_FILE, LOG_TABLE
381 */
382 void set_handlers(ulonglong log_printer);
383
384 /**
385 Activate log handlers for the given log type.
386
387 @param thd Thread handle
388 @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
389
390 @return true if error, false otherwise.
391 */
393
394 /**
395 Close file log for the given log type.
396
397 @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
398 */
400
401 /**
402 Close file log for the given log type and the reopen it.
403
404 @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
405 */
407
408 /**
409 Read log file name from global variable opt_*_logname.
410 If called from a sys_var update function, the caller
411 must hold a lock protecting the sys_var
412 (LOCK_global_system_variables, a polylock for the
413 variable, etc.).
414
415 @param log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
416 */
418
419 /**
420 Check if given Table_ref has a query log table name and
421 optionally check if the query log is currently enabled.
422
423 @param table_list Table_ref representing the table to check
424 @param check_if_opened Always return QUERY_LOG_NONE unless the
425 query log table is enabled.
426
427 @retval QUERY_LOG_NONE, QUERY_LOG_SLOW or QUERY_LOG_GENERAL
428 */
430 bool check_if_opened) const;
431};
432
434
435/**
436 Create the name of the query log specified.
437
438 This method forms a new path + file name for the log specified.
439
440 @param[in] buff Location for building new string.
441 @param[in] log_type QUERY_LOG_SLOW or QUERY_LOG_GENERAL
442
443 @returns Pointer to new string containing the name.
444*/
446
447/**
448 Check given log name against certain blacklisted names/extensions.
449
450 @param name Log name to check
451 @param len Length of log name
452
453 @returns true if name is valid, false otherwise.
454*/
455bool is_valid_log_name(const char *name, size_t len);
456
457/**
458 Check whether we need to write the current statement (or its rewritten
459 version if it exists) to the slow query log.
460 As a side-effect, a digest of suppressed statements may be written.
461
462 @param thd thread handle
463
464 @retval
465 true statement needs to be logged
466 @retval
467 false statement does not need to be logged
468*/
469bool log_slow_applicable(THD *thd);
470
471/**
472 Unconditionally writes the current statement (or its rewritten version if it
473 exists) to the slow query log.
474
475 @param thd thread handle
476*/
477void log_slow_do(THD *thd);
478
479/**
480 Check whether we need to write the current statement to the slow query
481 log. If so, do so. This is a wrapper for the two functions above;
482 most callers should use this wrapper. Only use the above functions
483 directly if you have expensive rewriting that you only need to do if
484 the query actually needs to be logged (e.g. SP variables / NAME_CONST
485 substitution when executing a PROCEDURE).
486 A digest of suppressed statements may be logged instead of the current
487 statement.
488
489 @param thd thread handle
490*/
491void log_slow_statement(THD *thd);
492
493/**
494 @class Log_throttle
495 @brief Base class for rate-limiting a log (slow query log etc.)
496*/
497
499 /**
500 When will/did current window end?
501 */
503
504 /**
505 Log no more than rate lines of a given type per window_size
506 (e.g. per minute, usually LOG_THROTTLE_WINDOW_SIZE).
507 */
508 const ulong window_size;
509
510 /**
511 There have been this many lines of this type in this window,
512 including those that we suppressed. (We don't simply stop
513 counting once we reach the threshold as we'll write a summary
514 of the suppressed lines later.)
515 */
516 ulong count;
517
518 protected:
519 /**
520 Template for the summary line. Should contain %lu as the only
521 conversion specification.
522 */
523 const char *summary_template;
524
525 /**
526 Start a new window.
527 */
528 void new_window(ulonglong now);
529
530 /**
531 Increase count of logs we're handling.
532
533 @param rate Limit on records to be logged during the throttling window.
534
535 @retval true - log rate limit is exceeded, so record should be suppressed.
536 @retval false - log rate limit is not exceeded, record should be logged.
537 */
538 bool inc_log_count(ulong rate) { return (++count > rate); }
539
540 /**
541 Check whether we're still in the current window. (If not, the caller
542 will want to print a summary (if the logging of any lines was suppressed),
543 and start a new window.)
544 */
545 bool in_window(ulonglong now) const { return (now < window_end); }
546
547 /**
548 Prepare a summary of suppressed lines for logging.
549 This function returns the number of queries that were qualified for
550 inclusion in the log, but were not printed because of the rate-limiting.
551 The summary will contain this count as well as the respective totals for
552 lock and execution time.
553 This function assumes that the caller already holds the necessary locks.
554
555 @param rate Limit on records logged during the throttling window.
556 */
557 ulong prepare_summary(ulong rate);
558
559 /**
560 @param window_usecs ... in this many micro-seconds
561 @param msg use this template containing %lu as only non-literal
562 */
563 Log_throttle(ulong window_usecs, const char *msg)
564 : window_end(0),
565 window_size(window_usecs),
566 count(0),
567 summary_template(msg) {}
568
569 public:
570 /**
571 We're rate-limiting messages per minute; 60,000,000 microsecs = 60s
572 Debugging is less tedious with a window in the region of 5000000
573 */
574 static const ulong LOG_THROTTLE_WINDOW_SIZE = 60000000;
575};
576
577typedef bool (*log_summary_t)(THD *thd, const char *query, size_t query_length,
578 bool aggregate, ulonglong lock_usec,
579 ulonglong exec_usec);
580
581/**
582 @class Slow_log_throttle
583 @brief Used for rate-limiting the slow query log.
584*/
585
587 private:
588 /**
589 We're using our own (empty) security context during summary generation.
590 That way, the aggregate value of the suppressed queries isn't printed
591 with a specific user's name (i.e. the user who sent a query when or
592 after the time-window closes), as that would be misleading.
593 */
595
596 /**
597 Total of the execution times of queries in this time-window for which
598 we suppressed logging. For use in summary printing.
599 */
601
602 /**
603 Total of the lock times of queries in this time-window for which
604 we suppressed logging. For use in summary printing.
605 */
607
608 /**
609 A reference to the threshold ("no more than n log lines per ...").
610 References a (system-?) variable in the server.
611 */
612 ulong *rate;
613
614 /**
615 The routine we call to actually log a line (our summary).
616 */
618
619 /**
620 Slow_log_throttle is shared between THDs.
621 */
623
624 /**
625 Start a new window.
626 */
627 void new_window(ulonglong now);
628
629 /**
630 Actually print the prepared summary to log.
631 */
632 void print_summary(THD *thd, ulong suppressed, ulonglong print_lock_time,
633 ulonglong print_exec_time);
634
635 public:
636 /**
637 @param threshold suppress after this many queries ...
638 @param lock mutex to use for consistency of calculations
639 @param window_usecs ... in this many micro-seconds
640 @param logger call this function to log a single line (our summary)
641 @param msg use this template containing %lu as only non-literal
642 */
643 Slow_log_throttle(ulong *threshold, mysql_mutex_t *lock, ulong window_usecs,
644 log_summary_t logger, const char *msg);
645
646 /**
647 Prepare and print a summary of suppressed lines to log.
648 (For now, slow query log.)
649 The summary states the number of queries that were qualified for
650 inclusion in the log, but were not printed because of the rate-limiting,
651 and their respective totals for lock and execution time.
652 This wrapper for prepare_summary() and print_summary() handles the
653 locking/unlocking.
654
655 @param thd The THD that tries to log the statement.
656 @retval false Logging was not suppressed, no summary needed.
657 @retval true Logging was suppressed; a summary was printed.
658 */
659 bool flush(THD *thd);
660
661 /**
662 Top-level function.
663 @param thd The THD that tries to log the statement.
664 @param eligible Is the statement of the type we might suppress?
665 @retval true Logging should be suppressed.
666 @retval false Logging should not be suppressed.
667 */
668 bool log(THD *thd, bool eligible);
669};
670
671/**
672 @class Slow_log_throttle
673 @brief Used for rate-limiting a error logs.
674*/
675
677 private:
680 const char *subsys;
681
682 /**
683 Actually print the prepared summary to log.
684 */
685 void print_summary(ulong suppressed) {
688 subsys, LOG_ITEM_LOG_MESSAGE, summary_template,
689 (unsigned long)suppressed);
690 }
691
692 public:
693 /**
694 @param window_usecs ... in this many micro-seconds (see Log_throttle)
695 @param lvl severity of the incident (error, warning, info)
696 @param errcode MySQL error code (e.g. ER_STARTUP)
697 @param subsystem subsystem tag, or nullptr for none
698 @param msg use this message template containing %lu as only
699 non-literal (for "number of suppressed events",
700 see Log_throttle)
701 */
702 Error_log_throttle(ulong window_usecs, loglevel lvl, uint errcode,
703 const char *subsystem, const char *msg)
704 : Log_throttle(window_usecs, msg),
705 ll(lvl),
706 err_code(errcode),
707 subsys(subsystem) {}
708
709 /**
710 Prepare and print a summary of suppressed lines to log.
711 (For now, slow query log.)
712 The summary states the number of queries that were qualified for
713 inclusion in the log, but were not printed because of the rate-limiting.
714
715 @retval false Logging was not suppressed, no summary needed.
716 @retval true Logging was suppressed; a summary was printed.
717 */
718 bool flush();
719
720 /**
721 Top-level function.
722 @retval true Logging should be suppressed.
723 @retval false Logging should not be suppressed.
724 */
725 bool log();
726};
727
729
730////////////////////////////////////////////////////////////
731//
732// Error Log
733//
734////////////////////////////////////////////////////////////
735
736/*
737 Set up some convenience defines to help us while we change
738 old-style ("sql_print_...()") calls to new-style ones
739 ("LogErr(...)"). New code should not use these, nor should
740 it use sql_print_...().
741*/
742
743/**
744 Set up basics, fetch message for "errcode", insert any va_args,
745 call the new error stack. A helper for the transition to the
746 new stack.
747*/
748#define log_errlog(level, errcode, ...) \
749 log_message(LOG_TYPE_ERROR, LOG_ITEM_LOG_PRIO, (longlong)level, \
750 LOG_ITEM_SRV_SUBSYS, LOG_SUBSYSTEM_TAG, LOG_ITEM_SRC_LINE, \
751 (longlong)__LINE__, LOG_ITEM_SRC_FILE, MY_BASENAME, \
752 LOG_ITEM_LOG_LOOKUP, (longlong)errcode, ##__VA_ARGS__)
753
754/**
755 Default tags + freeform message. A helper for re#defining sql_print_*()
756 to go through the new error log service stack.
757
758 Remember to never blindly LOG_MESSAGE a string you that may contain
759 user input as it may contain % which will be treated as substitutions.
760
761 BAD: LOG_ITEM_LOG_MESSAGE, dodgy_message
762 OK: LOG_ITEM_LOG_MESSAGE, "%s", dodgy_message
763 GOOD: LOG_ITEM_LOG_VERBATIM, dodgy_message
764*/
765#define log_errlog_formatted(level, ...) \
766 log_message(LOG_TYPE_ERROR, LOG_ITEM_LOG_PRIO, (longlong)level, \
767 LOG_ITEM_SRV_SUBSYS, LOG_SUBSYSTEM_TAG, LOG_ITEM_SRC_LINE, \
768 (longlong)__LINE__, LOG_ITEM_SRC_FILE, MY_BASENAME, \
769 LOG_ITEM_LOG_MESSAGE, ##__VA_ARGS__)
770
771/**
772 Set up the default tags, then let us add/override any key/value we like,
773 call the new error stack. A helper for the transition to the new stack.
774*/
775#define log_errlog_rich(level, ...) \
776 log_message(LOG_TYPE_ERROR, LOG_ITEM_LOG_PRIO, (longlong)level, \
777 LOG_ITEM_SRV_SUBSYS, LOG_SUBSYSTEM_TAG, LOG_ITEM_SRC_LINE, \
778 (longlong)__LINE__, LOG_ITEM_SRC_FILE, MY_BASENAME, __VA_ARGS__)
779
780/**
781 Define sql_print_*() so they use the new log_message()
782 variadic convenience interface to logging. This lets
783 us switch over the bulk of the messages right away until
784 we can attend to them individually; it also verifies that
785 we no longer use function pointers to log functions.
786
787 As before, sql_print_*() only accepts a printf-style
788 format string, and the arguments to same, if any.
789*/
790#define sql_print_information(...) \
791 log_errlog_formatted(INFORMATION_LEVEL, ##__VA_ARGS__)
792
793#define sql_print_warning(...) \
794 log_errlog_formatted(WARNING_LEVEL, ##__VA_ARGS__)
795
796#define sql_print_error(...) log_errlog_formatted(ERROR_LEVEL, ##__VA_ARGS__)
797
798/**
799 Prints a printf style message to the error log.
800
801 A thin wrapper around log_message() for local_message_hook,
802 Table_check_intact::report_error, and others.
803
804 @param level The level of the msg significance
805 @param ecode Error code of the error message.
806 @param args va_list list of arguments for the message
807*/
808void error_log_print(enum loglevel level, uint ecode, va_list args);
809
810/**
811 Initialize structures (e.g. mutex) needed by the error log.
812
813 @note This function accesses shared resources without protection, so
814 it should only be called while the server is running single-threaded.
815
816 @note The error log can still be used before this function is called,
817 but that should only be done single-threaded.
818
819 @retval true an error occurred
820 @retval false basic error logging is now available in multi-threaded mode
821*/
822bool init_error_log();
823
824/**
825 Open the error log and redirect stderr and optionally stdout
826 to the error log file. The streams are reopened only for
827 appending (writing at end of file).
828
829 @note
830 On error, my_error() is not called here.
831 So, caller of this function should call my_error() to keep the protocol.
832
833 @note This function also writes any error log messages that
834 have been buffered by calling flush_error_log_messages().
835
836 @param filename Name of error log file
837 @param get_lock Should we acquire LOCK_error_log?
838 @param log_type Error log, diagnostic log or both.
839*/
840bool open_error_log(const char *filename, bool get_lock, uint log_type);
841
842/**
843 Free any error log resources.
844
845 @note This function accesses shared resources without protection, so
846 it should only be called while the server is running single-threaded.
847
848 @note The error log can still be used after this function is called,
849 but that should only be done single-threaded. All buffered messages
850 should be flushed before calling this function.
851*/
852void destroy_error_log();
853
854/**
855 Flush any pending data to disk and reopen the error log.
856*/
857bool reopen_error_log();
858
859/**
860 Discard all buffered messages and deallocate buffer without printing
861 anything. Needed when terminating launching process after daemon
862 has started. At this point we may have messages in the error log,
863 but we don't want to show them to stderr (the daemon will output
864 them in its error log).
865 */
867
868/**
869 We buffer all error log messages that have been printed before the
870 error log has been opened. This allows us to write them to the
871 correct file once the error log has been opened.
872
873 This function will explicitly flush buffered messages to stderr.
874 It is only needed in cases where open_error_log() is not called
875 as it otherwise will be done there.
876
877 This function also turns buffering off (there is no way to turn
878 buffering back on).
879*/
881
882/**
883 Modular logger: log line and key/value manipulation helpers.
884 Server-internal. External services should access these via
885 the log_builtins service API (cf. preamble for this file).
886*/
887
888/**
889 Compare two NUL-terminated byte strings
890
891 Note that when comparing without length limit, the long string
892 is greater if they're equal up to the length of the shorter
893 string, but the shorter string will be considered greater if
894 its "value" up to that point is greater:
895
896 compare 'abc','abcd': -100 (longer wins if otherwise same)
897 compare 'abca','abcd': -3 (higher value wins)
898 compare 'abcaaaaa','abcd': -3 (higher value wins)
899
900 @param a the first string
901 @param b the second string
902 @param len compare at most this many characters --
903 0 for no limit
904 @param case_insensitive ignore upper/lower case in comparison
905
906 @retval -1 a < b
907 @retval 0 a == b
908 @retval 1 a > b
909*/
910int log_string_compare(const char *a, const char *b, size_t len,
911 bool case_insensitive);
912
913/**
914 Predicate used to determine whether a type is generic
915 (generic string, generic float, generic integer) rather
916 than a well-known type.
917
918 @param t log item type to examine
919
920 @retval true if generic type
921 @retval false if wellknown type
922*/
924
925/**
926 Predicate used to determine whether a class is a string
927 class (C-string or Lex-string).
928
929 @param c log item class to examine
930
931 @retval true if of a string class
932 @retval false if not of a string class
933*/
935
936/**
937 Predicate used to determine whether a class is a numeric
938 class (integer or float).
939
940 @param c log item class to examine
941
942 @retval true if of a numeric class
943 @retval false if not of a numeric class
944*/
946
947/**
948 Get an integer value from a log-item of float or integer type.
949
950 @param li log item to get the value from
951 @param i longlong to store the value in
952*/
954
955/**
956 Get a float value from a log-item of float or integer type.
957
958 @param li log item to get the value from
959 @param f float to store the value in
960*/
961void log_item_get_float(log_item *li, double *f);
962
963/**
964 Get a string value from a log-item of C-string or Lex string type.
965
966 @param li log item to get the value from
967 @param str char-pointer to store the pointer to the value in
968 @param len size_t pointer to store the length of the value in
969*/
970void log_item_get_string(log_item *li, char **str, size_t *len);
971
972/**
973 Set an integer value on a log_item.
974 Fails gracefully if not log_item_data is supplied, so it can safely
975 wrap log_line_item_set[_with_key]().
976
977 @param lid log_item_data struct to set the value on
978 @param i integer to set
979
980 @retval true lid was nullptr (possibly: OOM, could not set up log_item)
981 @retval false all's well
982*/
984
985/**
986 Set a floating point value on a log_item.
987 Fails gracefully if not log_item_data is supplied, so it can safely
988 wrap log_line_item_set[_with_key]().
989
990 @param lid log_item_data struct to set the value on
991 @param f float to set
992
993 @retval true lid was nullptr (possibly: OOM, could not set up log_item)
994 @retval false all's well
995*/
996bool log_item_set_float(log_item_data *lid, double f);
997
998/**
999 Set a string value on a log_item.
1000 Fails gracefully if not log_item_data is supplied, so it can safely
1001 wrap log_line_item_set[_with_key]().
1002
1003 @param lid log_item_data struct to set the value on
1004 @param s pointer to string
1005 @param s_len length of string
1006
1007 @retval true lid was nullptr (possibly: OOM, could not set up log_item)
1008 @retval false all's well
1009*/
1010bool log_item_set_lexstring(log_item_data *lid, const char *s, size_t s_len);
1011
1012/**
1013 Set a string value on a log_item.
1014 Fails gracefully if not log_item_data is supplied, so it can safely
1015 wrap log_line_item_set[_with_key]().
1016
1017 @param lid log_item_data struct to set the value on
1018 @param s pointer to NTBS
1019
1020 @retval true lid was nullptr (possibly: OOM, could not set up log_item)
1021 @retval false all's well
1022*/
1023bool log_item_set_cstring(log_item_data *lid, const char *s);
1024
1025/**
1026 See whether a string is a wellknown field name.
1027
1028 @param key potential key starts here
1029 @param len length of the string to examine
1030
1031 @retval LOG_ITEM_TYPE_RESERVED: reserved, but not "wellknown" key
1032 @retval LOG_ITEM_TYPE_NOT_FOUND: key not found
1033 @retval >0: index in array of wellknowns
1034*/
1035int log_item_wellknown_by_name(const char *key, size_t len);
1036
1037/**
1038 See whether a type is wellknown.
1039
1040 @param t log item type to examine
1041
1042 @retval LOG_ITEM_TYPE_NOT_FOUND: key not found
1043 @retval >0: index in array of wellknowns
1044*/
1046
1047/**
1048 Accessor: from a record describing a wellknown key, get its name
1049
1050 @param idx index in array of wellknowns, see log_item_wellknown_by_...()
1051
1052 @retval name (NTBS)
1053*/
1054const char *log_item_wellknown_get_name(uint idx);
1055
1056/**
1057 Accessor: from a record describing a wellknown key, get its type
1058
1059 @param idx index in array of wellknowns, see log_item_wellknown_by_...()
1060
1061 @retval the log item type for the wellknown key
1062*/
1064
1065/**
1066 Accessor: from a record describing a wellknown key, get its class
1067
1068 @param idx index in array of wellknowns, see log_item_wellknown_by_...()
1069
1070 @retval the log item class for the wellknown key
1071*/
1073
1074/**
1075 Release any of key and value on a log-item that were dynamically allocated.
1076
1077 @param li log-item to release the payload of
1078*/
1079void log_item_free(log_item *li);
1080
1081/**
1082 Predicate indicating whether a log line is "willing" to accept any more
1083 key/value pairs.
1084
1085 @param ll the log-line to examine
1086
1087 @retval false if not full / if able to accept another log_item
1088 @retval true if full
1089*/
1090bool log_line_full(log_line *ll);
1091
1092/**
1093 How many items are currently set on the given log_line?
1094
1095 @param ll the log-line to examine
1096
1097 @retval the number of items set
1098*/
1100
1101/**
1102 Test whether a given type is presumed present on the log line.
1103
1104 @param ll the log_line to examine
1105 @param m the log_type to test for
1106
1107 @retval 0 not present
1108 @retval !=0 present
1109*/
1111
1112/**
1113 Initialize a log_line.
1114
1115 @retval nullptr could not set up buffer (too small?)
1116 @retval other address of the newly initialized log_line
1117*/
1119
1120/**
1121 Release a log_line allocated with log_line_init.
1122*/
1123void 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*/
1149void 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*/
1170void 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*/
1184int 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*/
1195log_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*/
1232void 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*/
1398const 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*/
1411enum 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*/
1429int log_line_submit(log_line *ll);
1430
1431/**
1432 Set/reset one or more log line flags.
1433
1434 Example to set the flag:
1435 log_line_set_flag(ll, LOG_LINE_EMIT_TELEMETRY, LOG_LINE_EMIT_TELEMETRY);
1436 to reset the flag:
1437 log_line_set_flag(ll, LOG_LINE_EMIT_TELEMETRY, 0);
1438
1439 @param ll log line structure
1440 @param mask mask that defines flags to be changed
1441 @param value value to set to selected flags
1442*/
1445
1446/**
1447 Parse a ISO8601 timestamp and return the number of microseconds
1448 since the epoch. Heeds +/- timezone info if present.
1449
1450 @see make_iso8601_timestamp()
1451
1452 @param timestamp an ASCII string containing an ISO8601 timestamp
1453 @param len Length in bytes of the aforementioned string
1454
1455 @return microseconds since the epoch
1456*/
1458
1459/**
1460 Well-known values returned by log_error_stack().
1461*/
1463 /// success
1465
1466 /// expected delimiter not found
1468
1469 /// one or more services not found
1471
1472 /// couldn't create service cache entry
1474
1475 /// tried to multi-open singleton
1477
1478 /// couldn't create service instance entry
1480
1481 /// last element in pipeline should be a sink
1483
1484 /// service only available during start-up (may not be set by the user)
1486
1487 /// check-only warning: no sink supporting pfs given
1489
1490 /// check-only warning: no log-parser given
1492
1493 /// check-only warning: more than one log-filter given
1495
1496 /// service name may not start with a delimiter
1498
1499 /// delimiters ',' and ';' may not be mixed
1502
1503/**
1504 Set up custom error logging stack.
1505
1506 @param conf The configuration string
1507 @param check_only If true, report on whether configuration is valid
1508 (i.e. whether all requested services are available),
1509 but do not apply the new configuration.
1510 if false, set the configuration (acquire the
1511 necessary services, update the hash by
1512 adding/deleting entries as necessary)
1513 @param[out] pos If an error occurs and this pointer is non-null,
1514 the position in the configuration string where
1515 the error occurred will be written to the
1516 pointed-to size_t.
1517
1518 @retval LOG_ERROR_STACK_SUCCESS success
1519
1520 @retval LOG_ERROR_STACK_DELIMITER_MISSING expected delimiter not found
1521
1522 @retval LOG_ERROR_STACK_SERVICE_MISSING one or more services not found
1523
1524 @retval LOG_ERROR_STACK_CACHE_ENTRY_OOM couldn't create service cache
1525 entry
1526
1527 @retval LOG_ERROR_STACK_MULTITON_DENIED tried to multi-open singleton
1528
1529 @retval LOG_ERROR_STACK_SERVICE_INSTANCE_OOM couldn't create service
1530 instance entry
1531
1532 @retval LOG_ERROR_STACK_ENDS_IN_NON_SINK last element should be a sink
1533
1534 @retval LOG_ERROR_STACK_SERVICE_UNAVAILABLE service only available during
1535 start-up (may not be set by the
1536 user)
1537
1538
1539 @retval LOG_ERROR_STACK_NO_PFS_SUPPORT (check_only warning)
1540 no sink with performance_schema
1541 support selected
1542
1543 @retval LOG_ERROR_STACK_NO_LOG_PARSER (check_only warning)
1544 no sink providing a log-parser
1545 selected
1546
1547 @retval LOG_ERROR_MULTIPLE_FILTERS (check_only warning)
1548 more than one filter service
1549 selected
1550
1551 @retval LOG_ERROR_UNEXPECTED_DELIMITER_FOUND service starts with a delimiter
1552
1553 @retval LOG_ERROR_MIXED_DELIMITERS use ',' or ';', not both!
1554*/
1556 bool check_only, size_t *pos);
1557
1558/**
1559 Call flush() on all log_services.
1560 flush() function must not try to log anything, as we hold an
1561 exclusive lock on the stack.
1562
1563 @returns 0 if no problems occurred, otherwise the negative count
1564 of the components that failed to flush
1565*/
1567
1568/**
1569 Initialize the structured logging subsystem.
1570
1571 Since we're initializing various locks here, we must call this late enough
1572 so this is clean, but early enough so it still happens while we're running
1573 single-threaded -- this specifically also means we must call it before we
1574 start plug-ins / storage engines / external components!
1575
1576 @retval 0 no errors
1577 @retval -1 couldn't initialize stack lock
1578 @retval -2 couldn't initialize built-in default filter
1579 @retval -3 couldn't set up service hash
1580 @retval -4 couldn't initialize syseventlog lock
1581 @retval -5 couldn't initialize buffered logging lock
1582*/
1583int log_builtins_init();
1584
1585/**
1586 De-initialize the structured logging subsystem.
1587
1588 @retval 0 no errors
1589 @retval -1 not stopping, never started
1590*/
1591int log_builtins_exit();
1592
1593/**
1594 Interim helper: write to the default error stream
1595
1596 @param buffer buffer containing serialized error message
1597 @param length number of bytes in buffer
1598 @param log_type type of log, e.g. error or diagnostic log
1599*/
1600void log_write_errstream(const char *buffer, size_t length,
1601 enum enum_log_type log_type);
1602
1603#endif /* LOG_H */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
Definition: log.h:676
void print_summary(ulong suppressed)
Actually print the prepared summary to log.
Definition: log.h:685
const char * subsys
Definition: log.h:680
loglevel ll
Definition: log.h:678
Error_log_throttle(ulong window_usecs, loglevel lvl, uint errcode, const char *subsystem, const char *msg)
Definition: log.h:702
uint err_code
Definition: log.h:679
Abstract superclass for handling logging to slow/general logs.
Definition: log.h:160
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:498
bool in_window(ulonglong now) const
Check whether we're still in the current window.
Definition: log.h:545
const ulong window_size
Log no more than rate lines of a given type per window_size (e.g.
Definition: log.h:508
Log_throttle(ulong window_usecs, const char *msg)
Definition: log.h:563
bool inc_log_count(ulong rate)
Increase count of logs we're handling.
Definition: log.h:538
ulong count
There have been this many lines of this type in this window, including those that we suppressed.
Definition: log.h:516
ulonglong window_end
When will/did current window end?
Definition: log.h:502
const char * summary_template
Template for the summary line.
Definition: log.h:523
Class responsible for table based logging.
Definition: log.h:225
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:999
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:1171
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:873
Class responsible for file based logging.
Definition: log.cc:1207
Class which manages slow and general log event handlers.
Definition: log.h:263
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:1769
mysql_rwlock_t LOCK_logger
RW-lock protecting Query_logger.
Definition: log.h:277
Query_logger()
Definition: log.h:298
Log_event_handler * slow_log_handler_list[MAX_LOG_HANDLERS_NUM+1]
NULL-terminated arrays of log handlers.
Definition: log.h:285
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:1552
enum_log_table_type check_if_log_table(Table_ref *table_list, bool check_if_opened) const
Check if given Table_ref has a query log table name and optionally check if the query log is currentl...
Definition: log.cc:1745
bool activate_log_handler(THD *thd, enum_log_table_type log_type)
Activate log handlers for the given log type.
Definition: log.cc:1695
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:1457
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:1737
Log_to_file_event_handler * file_log_handler
Definition: log.h:281
bool general_log_print(THD *thd, enum_server_command command, const char *format,...)
Write printf style message to general query log.
Definition: log.cc:1602
void cleanup()
Free memory.
Definition: log.cc:1448
void deactivate_log_handler(enum_log_table_type log_type)
Close file log for the given log type.
Definition: log.cc:1708
Log_event_handler * general_log_handler_list[MAX_LOG_HANDLERS_NUM+1]
Definition: log.h:286
Log_to_csv_event_handler table_log_handler
Available log handlers.
Definition: log.h:280
void set_handlers(ulonglong log_printer)
Enable log event handlers for slow/general log.
Definition: log.cc:1686
Log_to_telemetry_event_handler * telemetry_log_handler
Definition: log.h:282
bool set_log_file(enum_log_table_type log_type)
Read log file name from global variable opt_*_logname.
Definition: log.cc:1715
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:1434
static const uint MAX_LOG_HANDLERS_NUM
Currently we have 2 main kinds of logging functions: old-fashioned file logs and csv logging routines...
Definition: log.h:270
void init()
Perform basic log initialization: create file-based log handler.
Definition: log.cc:1443
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:1638
A set of THD members describing the current authenticated user.
Definition: sql_security_ctx.h:54
Used for rate-limiting the slow query log.
Definition: log.h:586
log_summary_t log_summary
The routine we call to actually log a line (our summary).
Definition: log.h:617
mysql_mutex_t * LOCK_log_throttle
Slow_log_throttle is shared between THDs.
Definition: log.h:622
ulong * rate
A reference to the threshold ("no more than n log lines per ...").
Definition: log.h:612
Security_context aggregate_sctx
We're using our own (empty) security context during summary generation.
Definition: log.h:594
ulonglong total_lock_time
Total of the lock times of queries in this time-window for which we suppressed logging.
Definition: log.h:606
ulonglong total_exec_time
Total of the execution times of queries in this time-window for which we suppressed logging.
Definition: log.h:600
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: table.h:2952
int log_item_wellknown_by_type(log_item_type t)
See whether a type is wellknown.
Definition: log_builtins.cc:486
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:369
bool(* log_summary_t)(THD *thd, const char *query, size_t query_length, bool aggregate, ulonglong lock_usec, ulonglong exec_usec)
Definition: log.h:577
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:592
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:2226
char * make_query_log_name(char *buff, enum_log_table_type log_type)
Create the name of the query log specified.
Definition: log.cc:1775
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:988
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:1837
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:397
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:786
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:892
bool init_error_log()
Initialize structures (e.g.
Definition: log.cc:2067
Query_logger query_logger
Definition: log.cc:1773
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:690
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:867
bool log_item_set_float(log_item_data *lid, double f)
Set a floating point value on a log_item.
Definition: log_builtins.cc:954
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:1033
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:412
void destroy_error_log()
Free any error log resources.
Definition: log.cc:2142
log_item * log_line_item_init(log_line *ll)
Initializes an entry in a log line for use.
Definition: log_builtins.cc:827
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:425
log_line * log_line_init()
Initialize a log_line.
Definition: log_builtins.cc:607
int log_line_submit(log_line *ll)
Complete, filter, and write submitted log items.
Definition: log_builtins.cc:1172
static const uint LOG_FILE
Definition: log.h:256
void log_line_set_flag(log_line *ll, log_line_flags_mask mask, log_line_flags_mask value)
Set/reset one or more log line flags.
Definition: log_builtins.cc:1150
void log_item_init(log_item *li)
Initializes a log entry for use.
Definition: log_builtins.cc:812
bool reopen_error_log()
Flush any pending data to disk and reopen the error log.
Definition: log.cc:2197
void log_slow_statement(THD *thd)
Check whether we need to write the current statement to the slow query log.
Definition: log.cc:1860
void log_line_exit(log_line *ll)
Release a log_line allocated with log_line_init.
Definition: log_builtins.cc:622
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:429
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:1881
void log_write_errstream(const char *buffer, size_t length, enum enum_log_type log_type)
Interim helper: write to the default error stream.
Definition: log.cc:2205
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:527
enum_log_error_stack_error
Well-known values returned by log_error_stack().
Definition: log.h:1462
@ LOG_ERROR_STACK_SERVICE_MISSING
one or more services not found
Definition: log.h:1470
@ LOG_ERROR_STACK_NO_PFS_SUPPORT
check-only warning: no sink supporting pfs given
Definition: log.h:1488
@ LOG_ERROR_STACK_SERVICE_INSTANCE_OOM
couldn't create service instance entry
Definition: log.h:1479
@ LOG_ERROR_STACK_MULTITON_DENIED
tried to multi-open singleton
Definition: log.h:1476
@ LOG_ERROR_STACK_CACHE_ENTRY_OOM
couldn't create service cache entry
Definition: log.h:1473
@ LOG_ERROR_STACK_SUCCESS
success
Definition: log.h:1464
@ LOG_ERROR_MULTIPLE_FILTERS
check-only warning: more than one log-filter given
Definition: log.h:1494
@ LOG_ERROR_STACK_ENDS_IN_NON_SINK
last element in pipeline should be a sink
Definition: log.h:1482
@ LOG_ERROR_UNEXPECTED_DELIMITER_FOUND
service name may not start with a delimiter
Definition: log.h:1497
@ LOG_ERROR_STACK_DELIMITER_MISSING
expected delimiter not found
Definition: log.h:1467
@ LOG_ERROR_STACK_NO_LOG_PARSER
check-only warning: no log-parser given
Definition: log.h:1491
@ LOG_ERROR_STACK_SERVICE_UNAVAILABLE
service only available during start-up (may not be set by the user)
Definition: log.h:1485
@ LOG_ERROR_MIXED_DELIMITERS
delimiters ',' and ';' may not be mixed
Definition: log.h:1500
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:437
int log_message(int log_type,...)
Variadic convenience function for logging.
Definition: log.cc:2538
static const uint LOG_NONE
Definition: log.h:255
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:653
int log_line_item_count(log_line *ll)
How many items are currently set on the given log_line?
Definition: log_builtins.cc:664
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:773
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:1789
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:942
int log_builtins_init()
Initialize the structured logging subsystem.
Definition: log_builtins.cc:2244
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:717
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:741
enum_log_table_type
Type of the log table.
Definition: log.h:150
@ QUERY_LOG_NONE
Definition: log.h:151
@ QUERY_LOG_GENERAL
Definition: log.h:153
@ QUERY_LOG_SLOW
Definition: log.h:152
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:516
void error_log_print(enum loglevel level, uint ecode, va_list args)
Prints a printf style message to the error log.
Definition: log.cc:2515
bool open_error_log(const char *filename, bool get_lock, uint log_type)
Open the error log and redirect stderr and optionally stdout to the error log file.
Definition: log.cc:2085
const char * log_item_wellknown_get_name(uint idx)
Accessor: from a record describing a wellknown key, get its name.
Definition: log_builtins.cc:505
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:938
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:2271
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:675
int log_builtins_exit()
De-initialize the structured logging subsystem.
Definition: log_builtins.cc:2210
int log_builtins_error_stack_flush()
Call flush() on all log_services.
Definition: log_builtins.cc:1762
bool log_item_set_cstring(log_item_data *lid, const char *s)
Set a string value on a log_item.
Definition: log_builtins.cc:997
bool is_valid_log_name(const char *name, size_t len)
Check given log name against certain blacklisted names/extensions.
Definition: log.cc:405
void discard_error_log_messages()
Discard all buffered messages and deallocate buffer without printing anything.
Definition: log.cc:2059
bool log_item_set_int(log_item_data *lid, longlong i)
Set an integer value on a log_item.
Definition: log_builtins.cc:946
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:2063
const char * log_label_from_prio(int prio)
Convenience function: Derive a log label ("error", "warning", "information") from a severity.
Definition: log_builtins.cc:1006
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:768
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:444
log_item * log_line_get_output_buffer(log_line *ll)
Get log-line's output buffer.
Definition: log_builtins.cc:638
static const uint LOG_TABLE
Definition: log.h:257
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:1465
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:701
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:463
#define window_size
Definition: log_event.cc:177
uint64 log_line_flags_mask
a bit mask with flags describing a log line
Definition: log_shared.h:231
enum enum_log_item_class log_item_class
enum_log_type
log_type – which log to send data to check vs enum_log_table_type and LOG_FILE/LOG_TABLE/LOG_NONE
Definition: log_shared.h:66
@ LOG_TYPE_ERROR
Definition: log_shared.h:68
@ LOG_ITEM_LOG_MESSAGE
the message, format string
Definition: log_shared.h:146
@ LOG_ITEM_LOG_PRIO
log priority (error, warn, ...)
Definition: log_shared.h:143
@ LOG_ITEM_SRV_SUBSYS
log called from subsystem ...
Definition: log_shared.h:136
@ LOG_ITEM_SQL_ERRCODE
mysql error code (numeric)
Definition: log_shared.h:128
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:222
static int log_type
Definition: mi_log.cc:47
static mi_bit_type mask[]
Definition: mi_packrec.cc:141
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:48
Header for compiler-dependent features.
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
long long int longlong
Definition: my_inttypes.h:55
uint32_t uint32
Definition: my_inttypes.h:67
Common #defines and includes for file and socket I/O.
Definition of the global "loglevel" enumeration.
loglevel
Definition: my_loglevel.h:41
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:60
uint32 my_thread_id
Definition: my_thread_local.h:34
static int count
Definition: myisam_ftdump.cc:45
static char * query
Definition: myisam_ftdump.cc:47
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:1077
std::string format(const routing_guidelines::Session_info &session_info, bool extended_session_info)
Definition: dest_metadata_cache.cc:170
constexpr value_type timestamp
Definition: classic_protocol_constants.h:278
PT & ref(PT *tp)
Definition: tablespace_impl.cc:359
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:76
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
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:114
static Logger logger
The "top-level" logger used when no connection context is given.
Definition: test_trace_plugin.cc:296
const char * filename
Definition: pfs_example_component_population.cc:67
Instrumentation helpers for mutexes.
Performance schema instrumentation interface.
required string key
Definition: replication_asynchronous_connection_failover.proto:60
case opt name
Definition: sslopt-case.h:29
Definition: m_ctype.h:421
Definition: log_shared.h:202
log_line ("log event")
Definition: keyring_log_builtins_definition.cc:72
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50
An instrumented rwlock structure.
Definition: mysql_rwlock_bits.h:51
Definition: log_shared.h:191