MySQL 8.4.3
Source Code Documentation
|
logging of commands More...
#include "sql/log.h"
#include "my_config.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "m_string.h"
#include "my_sys.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/log_shared.h"
#include "mysql/psi/mysql_rwlock.h"
#include "mysql/strings/int2str.h"
#include "mysql_time.h"
#include "nulls.h"
#include "server_component/log_sink_buffer.h"
#include "sql_string.h"
#include "strxnmov.h"
#include <sys/time.h>
#include <unistd.h>
#include <algorithm>
#include <atomic>
#include <new>
#include <sstream>
#include <string>
#include <utility>
#include "lex_string.h"
#include "my_base.h"
#include "my_dbug.h"
#include "my_dir.h"
#include "my_double2ulonglong.h"
#include "my_time.h"
#include "mysql/plugin.h"
#include "mysql/psi/mysql_file.h"
#include "mysql/service_my_plugin_log.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/strings/m_ctype.h"
#include "mysql_version.h"
#include "mysqld_error.h"
#include "mysys_err.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/current_thd.h"
#include "sql/debug_sync.h"
#include "sql/derror.h"
#include "sql/discrete_interval.h"
#include "sql/error_handler.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/mysqld.h"
#include "sql/protocol_classic.h"
#include "sql/psi_memory_key.h"
#include "sql/query_options.h"
#include "sql/sql_audit.h"
#include "sql/sql_base.h"
#include "sql/sql_class.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_parse.h"
#include "sql/sql_plugin_ref.h"
#include "sql/sql_time.h"
#include "sql/system_variables.h"
#include "sql/table.h"
#include "string_with_len.h"
#include "strmake.h"
#include "strxmov.h"
#include "thr_lock.h"
#include "thr_mutex.h"
#include "sql/server_component/log_builtins_imp.h"
Classes | |
class | Query_log_table_intact |
class | Silence_log_table_errors |
Silence all errors and warnings reported when performing a write to a log table. More... | |
class | File_query_log |
class | Log_to_file_event_handler |
Class responsible for file based logging. More... | |
Functions | |
static void | ull2timeval (ulonglong utime, my_timeval *tv) |
bool | is_valid_log_name (const char *name, size_t len) |
Check given log name against certain blacklisted names/extensions. More... | |
static File | mysql_file_real_name_reopen (File file, PSI_file_key log_file_key, int open_flags, const char *opened_file_name, char *real_file_name) |
Get the real log file name, and possibly reopen file. More... | |
static bool | log_command (THD *thd, enum_server_command command) |
Check if a given command should be logged to the general log. More... | |
char * | make_query_log_name (char *buff, enum_log_table_type log_type) |
Create the name of the query log specified. More... | |
bool | log_slow_applicable (THD *thd) |
Check whether we need to write the current statement (or its rewritten version if it exists) to the slow query log. More... | |
void | log_slow_do (THD *thd) |
Unconditionally writes the current statement (or its rewritten version if it exists) to the slow query log. More... | |
void | log_slow_statement (THD *thd) |
Check whether we need to write the current statement to the slow query log. More... | |
static bool | slow_log_write (THD *thd, const char *query, size_t query_length, bool aggregate, ulonglong time_usec, ulonglong lock_usec) |
Slow_log_throttle log_throttle_qni & | opt_log_throttle_queries_not_using_indexes (s) suppressed." |
void | discard_error_log_messages () |
Discard all buffered messages and deallocate buffer without printing anything. More... | |
void | flush_error_log_messages () |
We buffer all error log messages that have been printed before the error log has been opened. More... | |
bool | init_error_log () |
Initialize structures (e.g. More... | |
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. More... | |
void | destroy_error_log () |
Free any error log resources. More... | |
bool | reopen_error_log () |
Flush any pending data to disk and reopen the error log. More... | |
void | log_write_errstream (const char *buffer, size_t length) |
Interim helper: write to the default error stream. More... | |
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. More... | |
int | log_vmessage (int log_type, va_list fili) |
Variadic convenience function for logging. More... | |
void | error_log_print (enum loglevel level, uint ecode, va_list args) |
Prints a printf style message to the error log. More... | |
int | log_message (int log_type,...) |
Variadic convenience function for logging. More... | |
int | my_plugin_log_message (MYSQL_PLUGIN *plugin_ptr, plugin_log_level level, const char *format,...) |
Variables | |
static const TABLE_FIELD_TYPE | slow_query_log_table_fields [SQLT_FIELD_COUNT] |
static const TABLE_FIELD_DEF | slow_query_log_table_def |
static const TABLE_FIELD_TYPE | general_log_table_fields [GLT_FIELD_COUNT] |
static const TABLE_FIELD_DEF | general_log_table_def |
static Query_log_table_intact | log_table_intact |
In case of an error, a message is printed to the error log. More... | |
Query_logger | query_logger |
static bool | error_log_initialized = false |
static mysql_mutex_t | LOCK_error_log |
static const char * | error_log_file = nullptr |
logging of commands
void destroy_error_log | ( | ) |
Free any error log resources.
void discard_error_log_messages | ( | ) |
Discard all buffered messages and deallocate buffer without printing anything.
Needed when terminating launching process after daemon has started. At this point we may have messages in the error log, but we don't want to show them to stderr (the daemon will output them in its error log).
void error_log_print | ( | enum loglevel | level, |
uint | ecode, | ||
va_list | args | ||
) |
Prints a printf style message to the error log.
A thin wrapper around log_message() for local_message_hook, Table_check_intact::report_error, and others.
level | The level of the msg significance |
ecode | Error code of the error message. |
args | va_list list of arguments for the message |
void flush_error_log_messages | ( | ) |
We buffer all error log messages that have been printed before the error log has been opened.
This allows us to write them to the correct file once the error log has been opened.
This function will explicitly flush buffered messages to stderr. It is only needed in cases where open_error_log() is not called as it otherwise will be done there.
This function also turns buffering off (there is no way to turn buffering back on).
bool init_error_log | ( | ) |
Initialize structures (e.g.
mutex) needed by the error log.
true | an error occurred |
false | basic error logging is now available in multi-threaded mode |
bool is_valid_log_name | ( | const char * | name, |
size_t | len | ||
) |
Check given log name against certain blacklisted names/extensions.
name | Log name to check |
len | Length of log name |
|
static |
Check if a given command should be logged to the general log.
thd | Thread handle |
command | SQL command |
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.
thd | a thd |
its | thread-ID |
int log_message | ( | int | log_type, |
... | |||
) |
Variadic convenience function for logging.
This fills in the array that is used by the filter and log-writer services. Where missing, timestamp, priority, and thread-ID (if any) are added. Log item source services, log item filters, and log item writers are called.
see log_vmessage() for more information.
log_type | what log should this go to? |
... | fields: LOG_ITEM_* tag, [[key], value] |
int | return value of log_vmessage() |
bool log_slow_applicable | ( | THD * | thd | ) |
Check whether we need to write the current statement (or its rewritten version if it exists) to the slow query log.
As a side-effect, a digest of suppressed statements may be written.
thd | thread handle |
true | statement needs to be logged |
false | statement does not need to be logged |
void log_slow_do | ( | THD * | thd | ) |
Unconditionally writes the current statement (or its rewritten version if it exists) to the slow query log.
thd | thread handle |
void log_slow_statement | ( | THD * | thd | ) |
Check whether we need to write the current statement to the slow query log.
If so, do so. This is a wrapper for the two functions above; most callers should use this wrapper. Only use the above functions directly if you have expensive rewriting that you only need to do if the query actually needs to be logged (e.g. SP variables / NAME_CONST substitution when executing a PROCEDURE). A digest of suppressed statements may be logged instead of the current statement.
thd | thread handle |
int log_vmessage | ( | int | log_type, |
va_list | fili | ||
) |
Variadic convenience function for logging.
Write a message to a log (for now just used for error log).
This fills in the array that is used by the filter and log-writer services. Where missing, timestamp, priority, and thread-ID (if any) are added. Log item source services, log item filters, and log item writers are called.
For convenience, any number of fields may be added:
Newer items (further to the right/bottom) overwrite older ones (further to the left/top).
If a message is given, it must be the last tag in the argument list. The message may be given verbatim as a C format string, followed by its arguments:
LOG_ITEM_LOG_MESSAGE, "format string %s %d abc", "arg1", 12345
To avoid substitutions, use
LOG_ITEM_LOG_VERBATIM, "message from other subsys containing %user input"
Alternatively, an error code may be specified – the corresponding error message will be looked up and inserted –, followed by any arguments required by the error message:
LOG_ITEM_LOG_LOOKUP, ER_CANT_SET_DATA_DIR, filename, errno, strerror(errno)
If no message is to be included (this should never be the case for the error log), LOG_ITEM_END may be used instead to terminate the list.
log_type | what log should this go to? |
fili | field list: LOG_ITEM_* tag, [[key], value] |
int | return value of log_line_submit() |
void log_write_errstream | ( | const char * | buffer, |
size_t | length | ||
) |
Interim helper: write to the default error stream.
buffer | buffer containing serialized error message |
length | number of bytes in buffer |
char * make_query_log_name | ( | char * | buff, |
enum_log_table_type | log_type | ||
) |
Create the name of the query log specified.
This method forms a new path + file name for the log specified.
[in] | buff | Location for building new string. |
[in] | log_type | QUERY_LOG_SLOW or QUERY_LOG_GENERAL |
int my_plugin_log_message | ( | MYSQL_PLUGIN * | plugin_ptr, |
plugin_log_level | level, | ||
const char * | format, | ||
... | |||
) |
|
static |
Get the real log file name, and possibly reopen file.
The implementation is platform dependent due to differences in how this is supported:
On Windows, we get the actual path based on the file descriptor. This path is copied into the supplied buffer. The 'file' parameter is returned without re-opening.
On other platforms, we use realpath() to get the path with symbolic links expanded. Then, we close the file, and reopen the real path using the O_NOFOLLOW flag. This will reject following symbolic links.
file | File descriptor. | |
log_file_key | Key for P_S instrumentation. | |
open_flags | Flags to use for opening the file. | |
opened_file_name | Name of the open fd. | |
[out] | real_file_name | Buffer for actual name of the fd. |
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.
The streams are reopened only for appending (writing at end of file).
filename | Name of error log file |
get_lock | Should we acquire LOCK_error_log? |
Make sure, file is writable if it exists. If file does not exists then make sure directory path exists and it is writable.
Slow_log_throttle log_throttle_qni & opt_log_throttle_queries_not_using_indexes | ( | s | ) |
bool reopen_error_log | ( | ) |
Flush any pending data to disk and reopen the error log.
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
In case of an error, a message is printed to the error log.
Query_logger query_logger |
|
static |
|
static |