MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
log.h
Go to the documentation of this file.
1/* Copyright (c) 2005, 2025, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/**
25 @file sql/log.h
26
27 Error logging, slow query logging, general query logging:
28 If it's server-internal, and it's logging, it's here.
29
30 Components/services should NOT include this, but include
31 include/mysql/components/services/log_builtins.h instead
32 to gain access to the error logging stack.
33
34 Legacy plugins (pre-"services") will likely include
35 include/mysql/service_my_plugin_log.h instead.
36*/
37
38#ifndef LOG_H
39#define LOG_H
40
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 */
353 bool general_log_print(THD *thd, enum_server_command command,
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 */
367 bool general_log_write(THD *thd, enum_server_command command,
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 @param log_type Error log, diagnostic log or both.
834*/
835bool open_error_log(const char *filename, bool get_lock, uint log_type);
836
837/**
838 Free any error log resources.
839
840 @note This function accesses shared resources without protection, so
841 it should only be called while the server is running single-threaded.
842
843 @note The error log can still be used after this function is called,
844 but that should only be done single-threaded. All buffered messages
845 should be flushed before calling this function.
846*/
847void destroy_error_log();
848
849/**
850 Flush any pending data to disk and reopen the error log.
851*/
852bool reopen_error_log();
853
854/**
855 Discard all buffered messages and deallocate buffer without printing
856 anything. Needed when terminating launching process after daemon
857 has started. At this point we may have messages in the error log,
858 but we don't want to show them to stderr (the daemon will output
859 them in its error log).
860 */
862
863/**
864 We buffer all error log messages that have been printed before the
865 error log has been opened. This allows us to write them to the
866 correct file once the error log has been opened.
867
868 This function will explicitly flush buffered messages to stderr.
869 It is only needed in cases where open_error_log() is not called
870 as it otherwise will be done there.
871
872 This function also turns buffering off (there is no way to turn
873 buffering back on).
874*/
876
877/**
878 Modular logger: log line and key/value manipulation helpers.
879 Server-internal. External services should access these via
880 the log_builtins service API (cf. preamble for this file).
881*/
882
883/**
884 Compare two NUL-terminated byte strings
885
886 Note that when comparing without length limit, the long string
887 is greater if they're equal up to the length of the shorter
888 string, but the shorter string will be considered greater if
889 its "value" up to that point is greater:
890
891 compare 'abc','abcd': -100 (longer wins if otherwise same)
892 compare 'abca','abcd': -3 (higher value wins)
893 compare 'abcaaaaa','abcd': -3 (higher value wins)
894
895 @param a the first string
896 @param b the second string
897 @param len compare at most this many characters --
898 0 for no limit
899 @param case_insensitive ignore upper/lower case in comparison
900
901 @retval -1 a < b
902 @retval 0 a == b
903 @retval 1 a > b
904*/
905int log_string_compare(const char *a, const char *b, size_t len,
906 bool case_insensitive);
907
908/**
909 Predicate used to determine whether a type is generic
910 (generic string, generic float, generic integer) rather
911 than a well-known type.
912
913 @param t log item type to examine
914
915 @retval true if generic type
916 @retval false if wellknown type
917*/
919
920/**
921 Predicate used to determine whether a class is a string
922 class (C-string or Lex-string).
923
924 @param c log item class to examine
925
926 @retval true if of a string class
927 @retval false if not of a string class
928*/
930
931/**
932 Predicate used to determine whether a class is a numeric
933 class (integer or float).
934
935 @param c log item class to examine
936
937 @retval true if of a numeric class
938 @retval false if not of a numeric class
939*/
941
942/**
943 Get an integer value from a log-item of float or integer type.
944
945 @param li log item to get the value from
946 @param i longlong to store the value in
947*/
949
950/**
951 Get a float value from a log-item of float or integer type.
952
953 @param li log item to get the value from
954 @param f float to store the value in
955*/
956void log_item_get_float(log_item *li, double *f);
957
958/**
959 Get a string value from a log-item of C-string or Lex string type.
960
961 @param li log item to get the value from
962 @param str char-pointer to store the pointer to the value in
963 @param len size_t pointer to store the length of the value in
964*/
965void log_item_get_string(log_item *li, char **str, size_t *len);
966
967/**
968 Set an integer value on a log_item.
969 Fails gracefully if not log_item_data is supplied, so it can safely
970 wrap log_line_item_set[_with_key]().
971
972 @param lid log_item_data struct to set the value on
973 @param i integer to set
974
975 @retval true lid was nullptr (possibly: OOM, could not set up log_item)
976 @retval false all's well
977*/
979
980/**
981 Set a floating point value on a log_item.
982 Fails gracefully if not log_item_data is supplied, so it can safely
983 wrap log_line_item_set[_with_key]().
984
985 @param lid log_item_data struct to set the value on
986 @param f float to set
987
988 @retval true lid was nullptr (possibly: OOM, could not set up log_item)
989 @retval false all's well
990*/
991bool log_item_set_float(log_item_data *lid, double f);
992
993/**
994 Set a string value on a log_item.
995 Fails gracefully if not log_item_data is supplied, so it can safely
996 wrap log_line_item_set[_with_key]().
997
998 @param lid log_item_data struct to set the value on
999 @param s pointer to string
1000 @param s_len length of string
1001
1002 @retval true lid was nullptr (possibly: OOM, could not set up log_item)
1003 @retval false all's well
1004*/
1005bool log_item_set_lexstring(log_item_data *lid, const char *s, size_t s_len);
1006
1007/**
1008 Set a string value on a log_item.
1009 Fails gracefully if not log_item_data is supplied, so it can safely
1010 wrap log_line_item_set[_with_key]().
1011
1012 @param lid log_item_data struct to set the value on
1013 @param s pointer to NTBS
1014
1015 @retval true lid was nullptr (possibly: OOM, could not set up log_item)
1016 @retval false all's well
1017*/
1018bool log_item_set_cstring(log_item_data *lid, const char *s);
1019
1020/**
1021 See whether a string is a wellknown field name.
1022
1023 @param key potential key starts here
1024 @param len length of the string to examine
1025
1026 @retval LOG_ITEM_TYPE_RESERVED: reserved, but not "wellknown" key
1027 @retval LOG_ITEM_TYPE_NOT_FOUND: key not found
1028 @retval >0: index in array of wellknowns
1029*/
1030int log_item_wellknown_by_name(const char *key, size_t len);
1031
1032/**
1033 See whether a type is wellknown.
1034
1035 @param t log item type to examine
1036
1037 @retval LOG_ITEM_TYPE_NOT_FOUND: key not found
1038 @retval >0: index in array of wellknowns
1039*/
1041
1042/**
1043 Accessor: from a record describing a wellknown key, get its name
1044
1045 @param idx index in array of wellknowns, see log_item_wellknown_by_...()
1046
1047 @retval name (NTBS)
1048*/
1049const char *log_item_wellknown_get_name(uint idx);
1050
1051/**
1052 Accessor: from a record describing a wellknown key, get its type
1053
1054 @param idx index in array of wellknowns, see log_item_wellknown_by_...()
1055
1056 @retval the log item type for the wellknown key
1057*/
1059
1060/**
1061 Accessor: from a record describing a wellknown key, get its class
1062
1063 @param idx index in array of wellknowns, see log_item_wellknown_by_...()
1064
1065 @retval the log item class for the wellknown key
1066*/
1068
1069/**
1070 Release any of key and value on a log-item that were dynamically allocated.
1071
1072 @param li log-item to release the payload of
1073*/
1074void log_item_free(log_item *li);
1075
1076/**
1077 Predicate indicating whether a log line is "willing" to accept any more
1078 key/value pairs.
1079
1080 @param ll the log-line to examine
1081
1082 @retval false if not full / if able to accept another log_item
1083 @retval true if full
1084*/
1085bool log_line_full(log_line *ll);
1086
1087/**
1088 How many items are currently set on the given log_line?
1089
1090 @param ll the log-line to examine
1091
1092 @retval the number of items set
1093*/
1095
1096/**
1097 Test whether a given type is presumed present on the log line.
1098
1099 @param ll the log_line to examine
1100 @param m the log_type to test for
1101
1102 @retval 0 not present
1103 @retval !=0 present
1104*/
1106
1107/**
1108 Initialize a log_line.
1109
1110 @retval nullptr could not set up buffer (too small?)
1111 @retval other address of the newly initialized log_line
1112*/
1114
1115/**
1116 Release a log_line allocated with log_line_init.
1117*/
1118void log_line_exit(log_line *ll);
1119
1120/**
1121 Get log-line's output buffer.
1122 If the logger core provides this buffer, the log-service may use it
1123 to assemble its output therein and implicitly return it to the core.
1124 Participation is required for services that support populating
1125 performance_schema.error_log, and optional for all others.
1126
1127 @param ll the log_line to examine
1128
1129 @retval nullptr success, an output buffer is available
1130 @retval otherwise failure, no output buffer is available
1131*/
1133
1134/**
1135 Release log line item (key/value pair) with the index elem in log line ll.
1136 This frees whichever of key and value were dynamically allocated.
1137 This leaves a "gap" in the bag that may immediately be overwritten
1138 with an updated element. If the intention is to remove the item without
1139 replacing it, use log_line_item_remove() instead!
1140
1141 @param ll log_line
1142 @param elem index of the key/value pair to release
1143*/
1144void log_line_item_free(log_line *ll, size_t elem);
1145
1146/**
1147 Release all log line items (key/value pairs) in log line ll.
1148 This frees whichever keys and values were dynamically allocated.
1149
1150 @param ll log_line
1151*/
1153
1154/**
1155 Release log line item (key/value pair) with the index elem in log line ll.
1156 This frees whichever of key and value were dynamically allocated.
1157 This moves any trailing items to fill the "gap" and decreases the counter
1158 of elements in the log line. If the intention is to leave a "gap" in the
1159 bag that may immediately be overwritten with an updated element, use
1160 log_line_item_free() instead!
1161
1162 @param ll log_line
1163 @param elem index of the key/value pair to release
1164*/
1165void log_line_item_remove(log_line *ll, int elem);
1166
1167/**
1168 Find the (index of the) last key/value pair of the given name
1169 in the log line.
1170
1171 @param ll log line
1172 @param key the key to look for
1173
1174 @retval -1: none found
1175 @retval -2: invalid search-key given
1176 @retval -3: no log_line given
1177 @retval >=0: index of the key/value pair in the log line
1178*/
1179int log_line_index_by_name(log_line *ll, const char *key);
1180
1181/**
1182 Find the last item matching the given key in the log line.
1183
1184 @param ll log line
1185 @param key the key to look for
1186
1187 @retval nullptr item not found
1188 @retval otherwise pointer to the item (not a copy thereof!)
1189*/
1190log_item *log_line_item_by_name(log_line *ll, const char *key);
1191
1192/**
1193 Find the (index of the) first key/value pair of the given type
1194 in the log line.
1195
1196 @param ll log line
1197 @param t the log item type to look for
1198
1199 @retval <0: none found
1200 @retval >=0: index of the key/value pair in the log line
1201*/
1203
1204/**
1205 Find the (index of the) first key/value pair of the given type
1206 in the log line. This variant accepts a reference item and looks
1207 for an item that is of the same type (for wellknown types), or
1208 one that is of a generic type, and with the same key name (for
1209 generic types). For example, a reference item containing a
1210 generic string with key "foo" will a generic string, integer, or
1211 float with the key "foo".
1212
1213 @param ll log line
1214 @param ref a reference item of the log item type to look for
1215
1216 @retval <0: none found
1217 @retval >=0: index of the key/value pair in the log line
1218*/
1220
1221/**
1222 Initializes a log entry for use. This simply puts it in a defined
1223 state; if you wish to reset an existing item, see log_item_free().
1224
1225 @param li the log-item to initialize
1226*/
1227void log_item_init(log_item *li);
1228
1229/**
1230 Initializes an entry in a log line for use. This simply puts it in
1231 a defined state; if you wish to reset an existing item, see
1232 log_item_free().
1233 This resets the element beyond the last. The element count is not
1234 adjusted; this is for the caller to do once it sets up a valid
1235 element to suit its needs in the cleared slot. Finally, it is up
1236 to the caller to make sure that an element can be allocated.
1237
1238 @param ll the log-line to initialize a log_item in
1239
1240 @retval the address of the cleared log_item
1241*/
1243
1244/**
1245 Create new log item with key name "key", and allocation flags of
1246 "alloc" (see enum_log_item_free).
1247 Will return a pointer to the item's log_item_data struct for
1248 convenience.
1249 This is mostly interesting for filters and other services that create
1250 items that are not part of a log_line; sources etc. that intend to
1251 create an item for a log_line (the more common case) should usually
1252 use the below line_item_set_with_key() which creates an item (like
1253 this function does), but also correctly inserts it into a log_line.
1254
1255 @param li the log_item to work on
1256 @param t the item-type
1257 @param key the key to set on the item.
1258 ignored for non-generic types (may pass nullptr for those)
1259 see alloc
1260 @param alloc LOG_ITEM_FREE_KEY if key was allocated by caller
1261 LOG_ITEM_FREE_NONE if key was not allocated
1262 Allocated keys will automatically free()d when the
1263 log_item is.
1264 The log_item's alloc flags will be set to the
1265 submitted value; specifically, any pre-existing
1266 value will be clobbered. It is therefore WRONG
1267 a) to use this on a log_item that already has a key;
1268 it should only be used on freshly init'd log_items;
1269 b) to use this on a log_item that already has a
1270 value (specifically, an allocated one); the correct
1271 order is to init a log_item, then set up type and
1272 key, and finally to set the value. If said value is
1273 an allocated string, the log_item's alloc should be
1274 bitwise or'd with LOG_ITEM_FREE_VALUE.
1275
1276 @retval a pointer to the log_item's log_data, for easy chaining:
1277 log_item_set_with_key(...)->data_integer= 1;
1278*/
1280 const char *key, uint32 alloc);
1281
1282/**
1283 Create new log item in log line "ll", with key name "key", and
1284 allocation flags of "alloc" (see enum_log_item_free).
1285 It is up to the caller to ensure the log_line can accept more items
1286 (e.g. by using log_line_full(ll)).
1287 On success, the number of registered items on the log line is increased,
1288 the item's type is added to the log_line's "seen" property,
1289 and a pointer to the item's log_item_data struct is returned for
1290 convenience.
1291
1292 @param ll the log_line to work on
1293 @param t the item-type
1294 @param key the key to set on the item.
1295 ignored for non-generic types (may pass nullptr for those)
1296 see alloc
1297 @param alloc LOG_ITEM_FREE_KEY if key was allocated by caller
1298 LOG_ITEM_FREE_NONE if key was not allocated
1299 Allocated keys will automatically free()d when the
1300 log_item is.
1301 The log_item's alloc flags will be set to the
1302 submitted value; specifically, any pre-existing
1303 value will be clobbered. It is therefore WRONG
1304 a) to use this on a log_item that already has a key;
1305 it should only be used on freshly init'd log_items;
1306 b) to use this on a log_item that already has a
1307 value (specifically, an allocated one); the correct
1308 order is to init a log_item, then set up type and
1309 key, and finally to set the value. If said value is
1310 an allocated string, the log_item's alloc should be
1311 bitwise or'd with LOG_ITEM_FREE_VALUE.
1312
1313 @retval a pointer to the log_item's log_data, for easy chaining:
1314 log_line_item_set_with_key(...)->data_integer= 1;
1315*/
1317 const char *key, uint32 alloc);
1318
1319/**
1320 As log_item_set_with_key(), except that the key is automatically
1321 derived from the wellknown log_item_type t.
1322
1323 Create new log item with type "t".
1324 Will return a pointer to the item's log_item_data struct for
1325 convenience.
1326 This is mostly interesting for filters and other services that create
1327 items that are not part of a log_line; sources etc. that intend to
1328 create an item for a log_line (the more common case) should usually
1329 use the below line_item_set_with_key() which creates an item (like
1330 this function does), but also correctly inserts it into a log_line.
1331
1332 The allocation of this item will be LOG_ITEM_FREE_NONE;
1333 specifically, any pre-existing value will be clobbered.
1334 It is therefore WRONG
1335 a) to use this on a log_item that already has a key;
1336 it should only be used on freshly init'd log_items;
1337 b) to use this on a log_item that already has a
1338 value (specifically, an allocated one); the correct
1339 order is to init a log_item, then set up type and
1340 key, and finally to set the value. If said value is
1341 an allocated string, the log_item's alloc should be
1342 bitwise or'd with LOG_ITEM_FREE_VALUE.
1343
1344 @param li the log_item to work on
1345 @param t the item-type
1346
1347 @retval a pointer to the log_item's log_data, for easy chaining:
1348 log_item_set_with_key(...)->data_integer= 1;
1349*/
1351
1352/**
1353 Create a new log item of well-known type "t" in log line "ll".
1354 On success, the number of registered items on the log line is increased,
1355 the item's type is added to the log_line's "seen" property,
1356 and a pointer to the item's log_item_data struct is returned for
1357 convenience.
1358
1359 It is up to the caller to ensure the log_line can accept more items
1360 (e.g. by using log_line_full(ll)).
1361
1362 The allocation of this item will be LOG_ITEM_FREE_NONE;
1363 specifically, any pre-existing value will be clobbered.
1364 It is therefore WRONG
1365 a) to use this on a log_item that already has a key;
1366 it should only be used on freshly init'd log_items;
1367 b) to use this on a log_item that already has a
1368 value (specifically, an allocated one); the correct
1369 order is to init a log_item, then set up type and
1370 key, and finally to set the value. If said value is
1371 an allocated string, the log_item's alloc should be
1372 bitwise or'd with LOG_ITEM_FREE_VALUE.
1373
1374 @param ll the log_line to work on
1375 @param t the item-type
1376
1377 @retval a pointer to the log_item's log_data, for easy chaining:
1378 log_line_item_set_with_key(...)->data_integer= 1;
1379*/
1381
1382/**
1383 Convenience function: Derive a log label ("error", "warning",
1384 "information") from a severity.
1385
1386 @param prio the severity/prio in question
1387
1388 @return a label corresponding to that priority.
1389 @retval "ERROR" for prio of ERROR_LEVEL or higher
1390 @retval "Warning" for prio of WARNING_LEVEL
1391 @retval "Note" otherwise
1392*/
1393const char *log_label_from_prio(int prio);
1394
1395/**
1396 Derive the event's priority (SYSTEM_LEVEL, ERROR_LEVEL, ...)
1397 from a textual label. If the label can not be identified,
1398 default to ERROR_LEVEL as it is better to keep something
1399 that needn't be kept than to discard something that shouldn't
1400 be.
1401
1402 @param label The prio label as a \0 terminated C-string.
1403
1404 @retval the priority (as an enum loglevel)
1405*/
1406enum loglevel log_prio_from_label(const char *label);
1407
1408/**
1409 Complete, filter, and write submitted log items.
1410
1411 This expects a log_line collection of log-related key/value pairs,
1412 e.g. from log_message().
1413
1414 Where missing, timestamp, priority, thread-ID (if any) and so forth
1415 are added.
1416
1417 Log item source services, log item filters, and log item sinks are
1418 then called.
1419
1420 @param ll key/value pairs describing info to log
1421
1422 @retval int number of fields in created log line
1423*/
1424int log_line_submit(log_line *ll);
1425
1426/**
1427 Set/reset one or more log line flags.
1428
1429 Example to set the flag:
1430 log_line_set_flag(ll, LOG_LINE_EMIT_TELEMETRY, LOG_LINE_EMIT_TELEMETRY);
1431 to reset the flag:
1432 log_line_set_flag(ll, LOG_LINE_EMIT_TELEMETRY, 0);
1433
1434 @param ll log line structure
1435 @param mask mask that defines flags to be changed
1436 @param value value to set to selected flags
1437*/
1440
1441/**
1442 Whether to generate a UTC timestamp, or one following system-time.
1443 These values are not arbitrary; they must correspond to the range
1444 and meaning of opt_log_timestamps.
1445*/
1447 iso8601_sysvar_logtimestamps = -1, /**< use value of opt_log_timestamps */
1448 iso8601_utc = 0, /**< create UTC timestamp */
1449 iso8601_system_time = 1 /**< use system time */
1451
1452/**
1453 Make and return an ISO 8601 / RFC 3339 compliant timestamp.
1454 Accepts the log_timestamps global variable in its third parameter.
1455
1456 @param buf A buffer of at least iso8601_size bytes to store
1457 the timestamp in. The timestamp will be \0 terminated.
1458 @param utime Microseconds since the epoch
1459 @param mode if 0, use UTC; if 1, use local time
1460
1461 @retval length of timestamp (excluding \0)
1462*/
1463int make_iso8601_timestamp(char *buf, ulonglong utime,
1465
1466/**
1467 Parse a ISO8601 timestamp and return the number of microseconds
1468 since the epoch. Heeds +/- timezone info if present.
1469
1470 @see make_iso8601_timestamp()
1471
1472 @param timestamp an ASCII string containing an ISO8601 timestamp
1473 @param len Length in bytes of the aforementioned string
1474
1475 @return microseconds since the epoch
1476*/
1478
1479/**
1480 Well-known values returned by log_error_stack().
1481*/
1483 /// success
1485
1486 /// expected delimiter not found
1488
1489 /// one or more services not found
1491
1492 /// couldn't create service cache entry
1494
1495 /// tried to multi-open singleton
1497
1498 /// couldn't create service instance entry
1500
1501 /// last element in pipeline should be a sink
1503
1504 /// service only available during start-up (may not be set by the user)
1506
1507 /// check-only warning: no sink supporting pfs given
1509
1510 /// check-only warning: no log-parser given
1512
1513 /// check-only warning: more than one log-filter given
1515
1516 /// service name may not start with a delimiter
1518
1519 /// delimiters ',' and ';' may not be mixed
1522
1523/**
1524 Set up custom error logging stack.
1525
1526 @param conf The configuration string
1527 @param check_only If true, report on whether configuration is valid
1528 (i.e. whether all requested services are available),
1529 but do not apply the new configuration.
1530 if false, set the configuration (acquire the
1531 necessary services, update the hash by
1532 adding/deleting entries as necessary)
1533 @param[out] pos If an error occurs and this pointer is non-null,
1534 the position in the configuration string where
1535 the error occurred will be written to the
1536 pointed-to size_t.
1537
1538 @retval LOG_ERROR_STACK_SUCCESS success
1539
1540 @retval LOG_ERROR_STACK_DELIMITER_MISSING expected delimiter not found
1541
1542 @retval LOG_ERROR_STACK_SERVICE_MISSING one or more services not found
1543
1544 @retval LOG_ERROR_STACK_CACHE_ENTRY_OOM couldn't create service cache
1545 entry
1546
1547 @retval LOG_ERROR_STACK_MULTITON_DENIED tried to multi-open singleton
1548
1549 @retval LOG_ERROR_STACK_SERVICE_INSTANCE_OOM couldn't create service
1550 instance entry
1551
1552 @retval LOG_ERROR_STACK_ENDS_IN_NON_SINK last element should be a sink
1553
1554 @retval LOG_ERROR_STACK_SERVICE_UNAVAILABLE service only available during
1555 start-up (may not be set by the
1556 user)
1557
1558
1559 @retval LOG_ERROR_STACK_NO_PFS_SUPPORT (check_only warning)
1560 no sink with performance_schema
1561 support selected
1562
1563 @retval LOG_ERROR_STACK_NO_LOG_PARSER (check_only warning)
1564 no sink providing a log-parser
1565 selected
1566
1567 @retval LOG_ERROR_MULTIPLE_FILTERS (check_only warning)
1568 more than one filter service
1569 selected
1570
1571 @retval LOG_ERROR_UNEXPECTED_DELIMITER_FOUND service starts with a delimiter
1572
1573 @retval LOG_ERROR_MIXED_DELIMITERS use ',' or ';', not both!
1574*/
1576 bool check_only, size_t *pos);
1577
1578/**
1579 Call flush() on all log_services.
1580 flush() function must not try to log anything, as we hold an
1581 exclusive lock on the stack.
1582
1583 @returns 0 if no problems occurred, otherwise the negative count
1584 of the components that failed to flush
1585*/
1587
1588/**
1589 Initialize the structured logging subsystem.
1590
1591 Since we're initializing various locks here, we must call this late enough
1592 so this is clean, but early enough so it still happens while we're running
1593 single-threaded -- this specifically also means we must call it before we
1594 start plug-ins / storage engines / external components!
1595
1596 @retval 0 no errors
1597 @retval -1 couldn't initialize stack lock
1598 @retval -2 couldn't initialize built-in default filter
1599 @retval -3 couldn't set up service hash
1600 @retval -4 couldn't initialize syseventlog lock
1601 @retval -5 couldn't initialize buffered logging lock
1602*/
1603int log_builtins_init();
1604
1605/**
1606 De-initialize the structured logging subsystem.
1607
1608 @retval 0 no errors
1609 @retval -1 not stopping, never started
1610*/
1611int log_builtins_exit();
1612
1613/**
1614 Interim helper: write to the default error stream
1615
1616 @param buffer buffer containing serialized error message
1617 @param length number of bytes in buffer
1618 @param log_type type of log, e.g. error or diagnostic log
1619*/
1620void log_write_errstream(const char *buffer, size_t length,
1621 enum enum_log_type log_type);
1622
1623#endif /* LOG_H */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
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: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: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:1583
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:1383
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:1559
bool activate_log_handler(THD *thd, enum_log_table_type log_type)
Activate log handlers for the given log type.
Definition: log.cc:1509
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:1300
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:1551
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:1419
void cleanup()
Free memory.
Definition: log.cc:1291
void deactivate_log_handler(enum_log_table_type log_type)
Close file log for the given log type.
Definition: log.cc:1522
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:1500
bool set_log_file(enum_log_table_type log_type)
Read log file name from global variable opt_*_logname.
Definition: log.cc:1529
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:1277
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:1286
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:1452
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:2904
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: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: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:2039
char * make_query_log_name(char *buff, enum_log_table_type log_type)
Create the name of the query log specified.
Definition: log.cc:1589
enum_iso8601_tzmode
Whether to generate a UTC timestamp, or one following system-time.
Definition: log.h:1446
@ iso8601_utc
create UTC timestamp
Definition: log.h:1448
@ iso8601_sysvar_logtimestamps
use value of opt_log_timestamps
Definition: log.h:1447
@ iso8601_system_time
use system time
Definition: log.h:1449
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:1649
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
bool init_error_log()
Initialize structures (e.g.
Definition: log.cc:1880
Query_logger query_logger
Definition: log.cc:1587
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:1955
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:1169
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:1147
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:2010
void log_slow_statement(THD *thd)
Check whether we need to write the current statement to the slow query log.
Definition: log.cc:1672
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:1948
void log_write_errstream(const char *buffer, size_t length, enum enum_log_type log_type)
Interim helper: write to the default error stream.
Definition: log.cc:2018
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:1482
@ LOG_ERROR_STACK_SERVICE_MISSING
one or more services not found
Definition: log.h:1490
@ LOG_ERROR_STACK_NO_PFS_SUPPORT
check-only warning: no sink supporting pfs given
Definition: log.h:1508
@ LOG_ERROR_STACK_SERVICE_INSTANCE_OOM
couldn't create service instance entry
Definition: log.h:1499
@ LOG_ERROR_STACK_MULTITON_DENIED
tried to multi-open singleton
Definition: log.h:1496
@ LOG_ERROR_STACK_CACHE_ENTRY_OOM
couldn't create service cache entry
Definition: log.h:1493
@ LOG_ERROR_STACK_SUCCESS
success
Definition: log.h:1484
@ LOG_ERROR_MULTIPLE_FILTERS
check-only warning: more than one log-filter given
Definition: log.h:1514
@ LOG_ERROR_STACK_ENDS_IN_NON_SINK
last element in pipeline should be a sink
Definition: log.h:1502
@ LOG_ERROR_UNEXPECTED_DELIMITER_FOUND
service name may not start with a delimiter
Definition: log.h:1517
@ LOG_ERROR_STACK_DELIMITER_MISSING
expected delimiter not found
Definition: log.h:1487
@ LOG_ERROR_STACK_NO_LOG_PARSER
check-only warning: no log-parser given
Definition: log.h:1511
@ LOG_ERROR_STACK_SERVICE_UNAVAILABLE
service only available during start-up (may not be set by the user)
Definition: log.h:1505
@ LOG_ERROR_MIXED_DELIMITERS
delimiters ',' and ';' may not be mixed
Definition: log.h:1520
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:2351
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:1603
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:2311
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:2328
bool open_error_log(const char *filename, bool get_lock, uint log_type)
Open the error log and redirect stderr and optionally stdout to the error log file.
Definition: log.cc:1898
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:2084
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:1462
int log_builtins_exit()
De-initialize the structured logging subsystem.
Definition: log_builtins.cc:2277
int log_builtins_error_stack_flush()
Call flush() on all log_services.
Definition: log_builtins.cc:1829
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:400
void discard_error_log_messages()
Discard all buffered messages and deallocate buffer without printing anything.
Definition: log.cc:1872
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:1876
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:1532
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:175
uint64 log_line_flags_mask
a bit mask with flags describing a log line
Definition: log_shared.h:231
enum enum_log_item_class log_item_class
enum_log_type
log_type – which log to send data to check vs enum_log_table_type and LOG_FILE/LOG_TABLE/LOG_NONE
Definition: log_shared.h:66
@ LOG_TYPE_ERROR
Definition: log_shared.h:68
@ LOG_ITEM_LOG_MESSAGE
the message, format string
Definition: log_shared.h:146
@ LOG_ITEM_LOG_PRIO
log priority (error, warn, ...)
Definition: log_shared.h:143
@ LOG_ITEM_SRV_SUBSYS
log called from subsystem ...
Definition: log_shared.h:136
@ LOG_ITEM_SQL_ERRCODE
mysql error code (numeric)
Definition: log_shared.h:128
enum enum_log_item_type log_item_type
item_type – what to log
uint64 log_item_type_mask
a bit mask of log_types.
Definition: log_shared.h:222
static int log_type
Definition: mi_log.cc:47
static mi_bit_type mask[]
Definition: mi_packrec.cc:141
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:48
Header for compiler-dependent features.
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
long long int longlong
Definition: my_inttypes.h:55
uint32_t uint32
Definition: my_inttypes.h:67
Common #defines and includes for file and socket I/O.
Definition of the global "loglevel" enumeration.
loglevel
Definition: my_loglevel.h:41
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
static my_thread_id thread_id
Definition: my_thr_init.cc: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:1084
std::string format(const routing_guidelines::Session_info &session_info, bool extended_session_info)
Definition: dest_metadata_cache.cc:170
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
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
static mysql_service_status_t flush(reference_caching_cache cache) noexcept
Definition: component.cc:114
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:421
Definition: log_shared.h:202
log_line ("log event")
Definition: keyring_log_builtins_definition.cc:72
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50
An instrumented rwlock structure.
Definition: mysql_rwlock_bits.h:51
Definition: log_shared.h:191