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