MySQL 9.1.0
Source Code Documentation
log.cc File Reference

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...
 

Enumerations

enum  enum_slow_query_log_table_field {
  SQLT_FIELD_START_TIME = 0 , SQLT_FIELD_USER_HOST , SQLT_FIELD_QUERY_TIME , SQLT_FIELD_LOCK_TIME ,
  SQLT_FIELD_ROWS_SENT , SQLT_FIELD_ROWS_EXAMINED , SQLT_FIELD_DATABASE , SQLT_FIELD_LAST_INSERT_ID ,
  SQLT_FIELD_INSERT_ID , SQLT_FIELD_SERVER_ID , SQLT_FIELD_SQL_TEXT , SQLT_FIELD_THREAD_ID ,
  SQLT_FIELD_COUNT
}
 
enum  enum_general_log_table_field {
  GLT_FIELD_EVENT_TIME = 0 , GLT_FIELD_USER_HOST , GLT_FIELD_THREAD_ID , GLT_FIELD_SERVER_ID ,
  GLT_FIELD_COMMAND_TYPE , GLT_FIELD_ARGUMENT , GLT_FIELD_COUNT
}
 

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_qniopt_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
 

Detailed Description

logging of commands

Enumeration Type Documentation

◆ enum_general_log_table_field

Enumerator
GLT_FIELD_EVENT_TIME 
GLT_FIELD_USER_HOST 
GLT_FIELD_THREAD_ID 
GLT_FIELD_SERVER_ID 
GLT_FIELD_COMMAND_TYPE 
GLT_FIELD_ARGUMENT 
GLT_FIELD_COUNT 

◆ enum_slow_query_log_table_field

Enumerator
SQLT_FIELD_START_TIME 
SQLT_FIELD_USER_HOST 
SQLT_FIELD_QUERY_TIME 
SQLT_FIELD_LOCK_TIME 
SQLT_FIELD_ROWS_SENT 
SQLT_FIELD_ROWS_EXAMINED 
SQLT_FIELD_DATABASE 
SQLT_FIELD_LAST_INSERT_ID 
SQLT_FIELD_INSERT_ID 
SQLT_FIELD_SERVER_ID 
SQLT_FIELD_SQL_TEXT 
SQLT_FIELD_THREAD_ID 
SQLT_FIELD_COUNT 

Function Documentation

◆ destroy_error_log()

void destroy_error_log ( )

Free any error log resources.

Note
This function accesses shared resources without protection, so it should only be called while the server is running single-threaded.
The error log can still be used after this function is called, but that should only be done single-threaded. All buffered messages should be flushed before calling this function.

◆ discard_error_log_messages()

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).

◆ error_log_print()

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.

Parameters
levelThe level of the msg significance
ecodeError code of the error message.
argsva_list list of arguments for the message

◆ flush_error_log_messages()

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).

◆ init_error_log()

bool init_error_log ( )

Initialize structures (e.g.

mutex) needed by the error log.

Note
This function accesses shared resources without protection, so it should only be called while the server is running single-threaded.
The error log can still be used before this function is called, but that should only be done single-threaded.
Return values
truean error occurred
falsebasic error logging is now available in multi-threaded mode

◆ is_valid_log_name()

bool is_valid_log_name ( const char *  name,
size_t  len 
)

Check given log name against certain blacklisted names/extensions.

Parameters
nameLog name to check
lenLength of log name
Returns
true if name is valid, false otherwise.

◆ log_command()

static bool log_command ( THD thd,
enum_server_command  command 
)
static

Check if a given command should be logged to the general log.

Parameters
thdThread handle
commandSQL command
Returns
true if command should be logged, false otherwise.

◆ log_get_thread_id()

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.

Parameters
thda thd
Return values
itsthread-ID

◆ log_message()

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.

Parameters
log_typewhat log should this go to?
...fields: LOG_ITEM_* tag, [[key], value]
Return values
intreturn value of log_vmessage()

◆ log_slow_applicable()

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.

Parameters
thdthread handle
Return values
truestatement needs to be logged
falsestatement does not need to be logged

◆ log_slow_do()

void log_slow_do ( THD thd)

Unconditionally writes the current statement (or its rewritten version if it exists) to the slow query log.

Parameters
thdthread handle

◆ log_slow_statement()

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.

Parameters
thdthread handle

◆ log_vmessage()

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:

  • "well-known" field types require a type tag and the payload: LOG_ITEM_LOG_LABEL, "ohai"
  • "generic" field types require a type tag, a key (C-string), and the payload: LOG_ITEM_GEN_FLOAT, "myPi", 3.1415926927

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.

Parameters
log_typewhat log should this go to?
filifield list: LOG_ITEM_* tag, [[key], value]
Return values
intreturn value of log_line_submit()

◆ log_write_errstream()

void log_write_errstream ( const char *  buffer,
size_t  length 
)

Interim helper: write to the default error stream.

Parameters
bufferbuffer containing serialized error message
lengthnumber of bytes in buffer

◆ make_query_log_name()

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.

Parameters
[in]buffLocation for building new string.
[in]log_typeQUERY_LOG_SLOW or QUERY_LOG_GENERAL
Returns
Pointer to new string containing the name.

◆ my_plugin_log_message()

int my_plugin_log_message ( MYSQL_PLUGIN plugin_ptr,
plugin_log_level  level,
const char *  format,
  ... 
)

◆ mysql_file_real_name_reopen()

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 
)
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.

Parameters
fileFile descriptor.
log_file_keyKey for P_S instrumentation.
open_flagsFlags to use for opening the file.
opened_file_nameName of the open fd.
[out]real_file_nameBuffer for actual name of the fd.
Returns
file descriptor to open file with 'real_file_name', or '-1' in case of errors.

◆ open_error_log()

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).

Note
On error, my_error() is not called here. So, caller of this function should call my_error() to keep the protocol.
This function also writes any error log messages that have been buffered by calling flush_error_log_messages().
Parameters
filenameName of error log file
get_lockShould 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.

◆ opt_log_throttle_queries_not_using_indexes()

Slow_log_throttle log_throttle_qni & opt_log_throttle_queries_not_using_indexes ( )

◆ reopen_error_log()

bool reopen_error_log ( )

Flush any pending data to disk and reopen the error log.

◆ slow_log_write()

static bool slow_log_write ( THD thd,
const char *  query,
size_t  query_length,
bool  aggregate,
ulonglong  time_usec,
ulonglong  lock_usec 
)
static

◆ ull2timeval()

static void ull2timeval ( ulonglong  utime,
my_timeval tv 
)
static

Variable Documentation

◆ error_log_file

const char* error_log_file = nullptr
static

◆ error_log_initialized

bool error_log_initialized = false
static

◆ general_log_table_def

const TABLE_FIELD_DEF general_log_table_def
static
Initial value:
@ GLT_FIELD_COUNT
Definition: log.cc:178
static const TABLE_FIELD_TYPE general_log_table_fields[GLT_FIELD_COUNT]
Definition: log.cc:181

◆ general_log_table_fields

const TABLE_FIELD_TYPE general_log_table_fields[GLT_FIELD_COUNT]
static
Initial value:
= {
{{STRING_WITH_LEN("event_time")},
{STRING_WITH_LEN("timestamp(6)")},
{nullptr, 0}},
{{STRING_WITH_LEN("user_host")},
{STRING_WITH_LEN("mediumtext")},
{STRING_WITH_LEN("utf8mb3")}},
{{STRING_WITH_LEN("thread_id")},
{STRING_WITH_LEN("bigint unsigned")},
{nullptr, 0}},
{{STRING_WITH_LEN("server_id")},
{STRING_WITH_LEN("int unsigned")},
{nullptr, 0}},
{{STRING_WITH_LEN("command_type")},
{STRING_WITH_LEN("varchar(64)")},
{STRING_WITH_LEN("utf8mb3")}},
{{STRING_WITH_LEN("argument")},
{STRING_WITH_LEN("mediumblob")},
{nullptr, 0}}}
#define STRING_WITH_LEN(X)
Definition: string_with_len.h:29

◆ LOCK_error_log

mysql_mutex_t LOCK_error_log
static

◆ log_table_intact

Query_log_table_intact log_table_intact
static

In case of an error, a message is printed to the error log.

◆ query_logger

Query_logger query_logger

◆ slow_query_log_table_def

const TABLE_FIELD_DEF slow_query_log_table_def
static
Initial value:
= {
static const TABLE_FIELD_TYPE slow_query_log_table_fields[SQLT_FIELD_COUNT]
Definition: log.cc:134
@ SQLT_FIELD_COUNT
Definition: log.cc:131

◆ slow_query_log_table_fields

const TABLE_FIELD_TYPE slow_query_log_table_fields[SQLT_FIELD_COUNT]
static