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