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