MySQL  8.0.17
Source Code Documentation
log.h File Reference

Error logging, slow query logging, general query logging: If it's server-internal, and it's logging, it's here. More...

#include <mysql/components/services/log_shared.h>
#include <stdarg.h>
#include <stddef.h>
#include <sys/types.h>
#include "lex_string.h"
#include "my_command.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_loglevel.h"
#include "my_psi_config.h"
#include "my_thread_local.h"
#include "mysql/components/services/mysql_mutex_bits.h"
#include "mysql/components/services/mysql_rwlock_bits.h"
#include "mysql/components/services/psi_file_bits.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql_com.h"
#include "sql/auth/sql_security_ctx.h"

Go to the source code of this file.

Classes

class  Log_event_handler
 Abstract superclass for handling logging to slow/general logs. More...
 
class  Log_to_csv_event_handler
 Class responsible for table based logging. More...
 
class  Query_logger
 Class which manages slow and general log event handlers. More...
 
class  Log_throttle
 Base class for rate-limiting a log (slow query log etc.) More...
 
class  Slow_log_throttle
 Used for rate-limiting the slow query log. More...
 
class  Error_log_throttle
 

Macros

#define log_errlog(level, errcode, ...)
 Set up basics, fetch message for "errcode", insert any va_args, call the new error stack. More...
 
#define log_errlog_formatted(level, ...)
 Default tags + freeform message. More...
 
#define log_errlog_rich(level, ...)
 Set up the default tags, then let us add/override any key/value we like, call the new error stack. More...
 
#define sql_print_information(...)   log_errlog_formatted(INFORMATION_LEVEL, ##__VA_ARGS__)
 Define sql_print_*() so they use the new log_message() variadic convenience interface to logging. More...
 
#define sql_print_warning(...)   log_errlog_formatted(WARNING_LEVEL, ##__VA_ARGS__)
 
#define sql_print_error(...)   log_errlog_formatted(ERROR_LEVEL, ##__VA_ARGS__)
 

Enumerations

enum  enum_log_table_type { QUERY_LOG_NONE = 0, QUERY_LOG_SLOW = 1, QUERY_LOG_GENERAL = 2 }
 Type of the log table. More...
 

Functions

int log_vmessage (int log_type, va_list lili)
 Write a message to a log (for now just used for error log). More...
 
int log_message (int log_type,...)
 Variadic convenience function for logging. 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...
 
char * make_query_log_name (char *buff, enum_log_table_type log_type)
 Create the name of the query log specified. More...
 
bool is_valid_log_name (const char *name, size_t len)
 Check given log name against certain blacklisted names/extensions. 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, struct System_status_var *query_start_status)
 Unconditionally writes the current statement (or its rewritten version if it exists) to the slow query log. More...
 
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. More...
 
void error_log_print (enum loglevel level, uint ecode, va_list args)
 Prints a printf style message to the error log. 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 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...
 
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. More...
 
bool log_item_generic_type (log_item_type t)
 Predicate used to determine whether a type is generic (generic string, generic float, generic integer) rather than a well-known type. More...
 
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). More...
 
bool log_item_numeric_class (log_item_class c)
 Predicate used to determine whether a class is a numeric class (integer or float). More...
 
void log_item_get_int (log_item *li, longlong *i)
 Get an integer value from a log-item of float or integer type. More...
 
void log_item_get_float (log_item *li, double *f)
 Get a float value from a log-item of float or integer type. More...
 
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. More...
 
bool log_item_set_int (log_item_data *lid, longlong i)
 Set an integer value on a log_item. More...
 
bool log_item_set_float (log_item_data *lid, double f)
 Set a floating point value on a log_item. More...
 
bool log_item_set_lexstring (log_item_data *lid, const char *s, size_t s_len)
 Set a string value on a log_item. More...
 
bool log_item_set_cstring (log_item_data *lid, const char *s)
 Set a string value on a log_item. More...
 
int log_item_wellknown_by_name (const char *key, size_t len)
 See whether a string is a wellknown field name. More...
 
int log_item_wellknown_by_type (log_item_type t)
 See whether a type is wellknown. More...
 
const char * log_item_wellknown_get_name (uint idx)
 Accessor: from a record describing a wellknown key, get its name. More...
 
log_item_type log_item_wellknown_get_type (uint idx)
 Accessor: from a record describing a wellknown key, get its type. More...
 
log_item_class log_item_wellknown_get_class (uint idx)
 Accessor: from a record describing a wellknown key, get its class. More...
 
void log_item_free (log_item *li)
 Release any of key and value on a log-item that were dynamically allocated. More...
 
bool log_line_full (log_line *ll)
 Predicate indicating whether a log line is "willing" to accept any more key/value pairs. More...
 
int log_line_item_count (log_line *ll)
 How many items are currently set on the given log_line? More...
 
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. More...
 
log_linelog_line_init ()
 Initialize a log_line. More...
 
void log_line_exit (log_line *ll)
 Release a log_line allocated with log_line_init. More...
 
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. More...
 
void log_line_item_free_all (log_line *ll)
 Release all log line items (key/value pairs) in log line ll. More...
 
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. More...
 
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. More...
 
log_itemlog_line_item_by_name (log_line *ll, const char *key)
 Find the last item matching the given key in the log line. More...
 
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. More...
 
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. More...
 
void log_item_init (log_item *li)
 Initializes a log entry for use. More...
 
log_itemlog_line_item_init (log_line *ll)
 Initializes an entry in a log line for use. More...
 
log_item_datalog_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). More...
 
log_item_datalog_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_log_item_free). More...
 
log_item_datalog_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_type t. More...
 
log_item_datalog_line_item_set (log_line *ll, log_item_type t)
 Create a new log item of well-known type "t" in log line "ll". More...
 
const char * log_label_from_prio (int prio)
 Convenience function: Derive a log label ("error", "warning", "information") from a severity. More...
 
int log_line_submit (log_line *ll)
 Complete, filter, and write submitted log items. More...
 
int make_iso8601_timestamp (char *buf, ulonglong utime, int mode)
 Make and return an ISO 8601 / RFC 3339 compliant timestamp. More...
 
int log_builtins_error_stack (const char *conf, bool check_only, size_t *pos)
 Set up custom error logging stack. More...
 
int log_builtins_error_stack_flush ()
 Call flush() on all log_services. More...
 
int log_builtins_init ()
 Initialize the structured logging subsystem. More...
 
int log_builtins_exit ()
 De-initialize the structured logging subsystem. More...
 
void log_write_errstream (const char *buffer, size_t length)
 Interim helper: write to the default error stream. More...
 

Variables

static const uint LOG_NONE = 1
 
static const uint LOG_FILE = 2
 
static const uint LOG_TABLE = 4
 
Query_logger query_logger
 
Slow_log_throttle log_throttle_qni
 

Detailed Description

Error logging, slow query logging, general query logging: If it's server-internal, and it's logging, it's here.

Components/services should NOT include this, but include include/mysql/components/services/log_builtins.h instead to gain access to the error logging stack.

Legacy plugins (pre-"services") will likely include include/mysql/service_my_plugin_log.h instead.

Macro Definition Documentation

◆ log_errlog

#define log_errlog (   level,
  errcode,
  ... 
)
Value:
LOG_ITEM_LOG_LOOKUP, (longlong)errcode, ##__VA_ARGS__)
#define LOG_SUBSYSTEM_TAG
Definition: log_sink_test.cc:26
int log_message(int log_type,...)
Variadic convenience function for logging.
Definition: log.cc:2320
Definition: log_shared.h:70
log called from file ...
Definition: log_shared.h:134
insert message by error-code
Definition: log_shared.h:148
long long int longlong
Definition: my_inttypes.h:73
log called from line ...
Definition: log_shared.h:135
#define MY_BASENAME
Definition: my_basename.h:39
log priority (error, warn, ...)
Definition: log_shared.h:144
log called from subsystem ...
Definition: log_shared.h:137

Set up basics, fetch message for "errcode", insert any va_args, call the new error stack.

A helper for the transition to the new stack.

◆ log_errlog_formatted

#define log_errlog_formatted (   level,
  ... 
)
Value:
LOG_ITEM_LOG_MESSAGE, ##__VA_ARGS__)
#define LOG_SUBSYSTEM_TAG
Definition: log_sink_test.cc:26
int log_message(int log_type,...)
Variadic convenience function for logging.
Definition: log.cc:2320
Definition: log_shared.h:70
log called from file ...
Definition: log_shared.h:134
long long int longlong
Definition: my_inttypes.h:73
log called from line ...
Definition: log_shared.h:135
the message, format string
Definition: log_shared.h:147
#define MY_BASENAME
Definition: my_basename.h:39
log priority (error, warn, ...)
Definition: log_shared.h:144
log called from subsystem ...
Definition: log_shared.h:137

Default tags + freeform message.

A helper for re::defining sql_print_*() to go through the new error log service stack.

Remember to never blindly LOG_MESSAGE a string you that may contain user input as it may contain % which will be treated as substitutions.

BAD: LOG_ITEM_LOG_MESSAGE, dodgy_message OK: LOG_ITEM_LOG_MESSAGE, "%s", dodgy_message GOOD: LOG_ITEM_LOG_VERBATIM, dodgy_message

◆ log_errlog_rich

#define log_errlog_rich (   level,
  ... 
)
Value:
(longlong)__LINE__, LOG_ITEM_SRC_FILE, MY_BASENAME, __VA_ARGS__)
#define LOG_SUBSYSTEM_TAG
Definition: log_sink_test.cc:26
int log_message(int log_type,...)
Variadic convenience function for logging.
Definition: log.cc:2320
Definition: log_shared.h:70
log called from file ...
Definition: log_shared.h:134
long long int longlong
Definition: my_inttypes.h:73
log called from line ...
Definition: log_shared.h:135
#define MY_BASENAME
Definition: my_basename.h:39
log priority (error, warn, ...)
Definition: log_shared.h:144
log called from subsystem ...
Definition: log_shared.h:137

Set up the default tags, then let us add/override any key/value we like, call the new error stack.

A helper for the transition to the new stack.

◆ sql_print_error

#define sql_print_error (   ...)    log_errlog_formatted(ERROR_LEVEL, ##__VA_ARGS__)

◆ sql_print_information

#define sql_print_information (   ...)    log_errlog_formatted(INFORMATION_LEVEL, ##__VA_ARGS__)

Define sql_print_*() so they use the new log_message() variadic convenience interface to logging.

This lets us switch over the bulk of the messages right away until we can attend to them individually; it also verifies that we no longer use function pointers to log functions.

As before, sql_print_*() only accepts a printf-style format string, and the arguments to same, if any.

◆ sql_print_warning

#define sql_print_warning (   ...)    log_errlog_formatted(WARNING_LEVEL, ##__VA_ARGS__)

Enumeration Type Documentation

◆ enum_log_table_type

Type of the log table.

Enumerator
QUERY_LOG_NONE 
QUERY_LOG_SLOW 
QUERY_LOG_GENERAL 

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

Initialize structures (e.g.

Since we're initializing various locks here, we must call this late enough so this is clean, but early enough so it still happens while we're running single-threaded – this specifically also means we must call it before we start plug-ins / storage engines / external components!

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_builtins_error_stack()

int log_builtins_error_stack ( const char *  conf,
bool  check_only,
size_t *  pos 
)

Set up custom error logging stack.

Parameters
confThe configuration string
check_onlyIf true, report on whether configuration is valid (i.e. whether all requested services are available), but do not apply the new configuration. if false, set the configuration (acquire the necessary services, update the hash by adding/deleting entries as necessary)
[out]posIf an error occurs and this pointer is non-null, the position in the configuration string where the error occurred will be written to the pointed-to size_t.
Return values
0success
-1expected delimiter not found
-2one or more services not found
-3failed to create service cache entry
-4tried to open multiple instances of a singleton
-5failed to create service instance entry
-6last element in pipeline should be a sink
-101service name may not start with a delimiter
-102delimiters ',' and ';' may not be mixed
Parameters
confThe configuration string
check_onlyIf true, report on whether configuration is valid (i.e. whether all requested services are available), but do not apply the new configuration. if false, set the configuration (acquire the necessary services, update the hash by adding/deleting entries as necessary)
[out]posIf an error occurs and this pointer is non-null, the position in the configuration string where the error occurred will be written to the pointed-to size_t.
Return values
0success
-1expected delimiter not found
-2one or more services not found
-3failed to create service cache entry
-4tried to open multiple instances of a singleton
-5failed to create service instance entry
-6last element in pipeline should be a sink
-7service only available during start-up; may not be set by the user
-101service name may not start with a delimiter
-102delimiters ',' and ';' may not be mixed

◆ log_builtins_error_stack_flush()

int log_builtins_error_stack_flush ( )

Call flush() on all log_services.

flush() function must not try to log anything, as we hold an exclusive lock on the stack.

Return values
0no problems
-1error

◆ log_builtins_exit()

int log_builtins_exit ( )

De-initialize the structured logging subsystem.

Return values
0no errors
-1not stopping, never started

◆ log_builtins_init()

int log_builtins_init ( )

Initialize the structured logging subsystem.

Since we're initializing various locks here, we must call this late enough so this is clean, but early enough so it still happens while we're running single-threaded – this specifically also means we must call it before we start plug-ins / storage engines / external components!

Return values
0no errors
-1couldn't initialize stack lock
-2couldn't initialize built-in default filter
-3couldn't set up service hash
-4couldn't initialize syseventlog lock
-5couldn't set service pipeline
-6couldn't initialize buffered logging lock

◆ 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_item_free()

void log_item_free ( log_item li)

Release any of key and value on a log-item that were dynamically allocated.

Parameters
lilog-item to release the payload of

◆ log_item_generic_type()

bool log_item_generic_type ( log_item_type  t)

Predicate used to determine whether a type is generic (generic string, generic float, generic integer) rather than a well-known type.

Parameters
tlog item type to examine
Return values
trueif generic type
falseif wellknown type

◆ log_item_get_float()

void log_item_get_float ( log_item li,
double *  f 
)

Get a float value from a log-item of float or integer type.

Parameters
lilog item to get the value from
ffloat to store the value in
lilog item to get the value from
[out]ffloat to store the value in

◆ log_item_get_int()

void log_item_get_int ( log_item li,
longlong i 
)

Get an integer value from a log-item of float or integer type.

Parameters
lilog item to get the value from
ilonglong to store the value in
lilog item to get the value from
[out]ilonglong to store the value in

◆ log_item_get_string()

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.

Parameters
lilog item to get the value from
strchar-pointer to store the pointer to the value in
lensize_t pointer to store the length of the value in
lilog item to get the value from
[out]strchar-pointer to store the pointer to the value in
[out]lensize_t pointer to store the length of the value in

◆ log_item_init()

void log_item_init ( log_item li)

Initializes a log entry for use.

This simply puts it in a defined state; if you wish to reset an existing item, see log_item_free().

Parameters
lithe log-item to initialize

◆ log_item_numeric_class()

bool log_item_numeric_class ( log_item_class  c)

Predicate used to determine whether a class is a numeric class (integer or float).

Parameters
clog item class to examine
Return values
trueif of a numeric class
falseif not of a numeric class

◆ log_item_set()

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

Create new log item with type "t". Will return a pointer to the item's log_item_data struct for convenience. This is mostly interesting for filters and other services that create items that are not part of a log_line; sources etc. that intend to create an item for a log_line (the more common case) should usually use the below line_item_set_with_key() which creates an item (like this function does), but also correctly inserts it into a log_line.

The allocation of this item will be LOG_ITEM_FREE_NONE; specifically, any pre-existing value will be clobbered. It is therefore WRONG a) to use this on a log_item that already has a key; it should only be used on freshly init'd log_items; b) to use this on a log_item that already has a value (specifically, an allocated one); the correct order is to init a log_item, then set up type and key, and finally to set the value. If said value is an allocated string, the log_item's alloc should be bitwise or'd with LOG_ITEM_FREE_VALUE.

Parameters
lithe log_item to work on
tthe item-type
Return values
apointer to the log_item's log_data, for easy chaining: log_item_set_with_key(...)->data_integer= 1;

◆ log_item_set_cstring()

bool log_item_set_cstring ( log_item_data lid,
const char *  s 
)

Set a string value on a log_item.

Fails gracefully if not log_item_data is supplied, so it can safely wrap log_line_item_set[_with_key]().

Parameters
lidlog_item_data struct to set the value on
spointer to NTBS
Return values
truelid was nullptr (possibly: OOM, could not set up log_item)
falseall's well

Fails gracefully if no log_item_data is supplied, so it can safely wrap log_line_item_set[_with_key]().

Parameters
lidlog_item_data struct to set the value on
spointer to NTBS
Return values
truelid was nullptr (possibly: OOM, could not set up log_item)
falseall's well

◆ log_item_set_float()

bool log_item_set_float ( log_item_data lid,
double  f 
)

Set a floating point value on a log_item.

Fails gracefully if not log_item_data is supplied, so it can safely wrap log_line_item_set[_with_key]().

Parameters
lidlog_item_data struct to set the value on
ffloat to set
Return values
truelid was nullptr (possibly: OOM, could not set up log_item)
falseall's well

Fails gracefully if no log_item_data is supplied, so it can safely wrap log_line_item_set[_with_key]().

Parameters
lidlog_item_data struct to set the value on
ffloat to set
Return values
truelid was nullptr (possibly: OOM, could not set up log_item)
falseall's well

◆ log_item_set_int()

bool log_item_set_int ( log_item_data lid,
longlong  i 
)

Set an integer value on a log_item.

Fails gracefully if not log_item_data is supplied, so it can safely wrap log_line_item_set[_with_key]().

Parameters
lidlog_item_data struct to set the value on
iinteger to set
Return values
truelid was nullptr (possibly: OOM, could not set up log_item)
falseall's well

Fails gracefully if no log_item_data is supplied, so it can safely wrap log_line_item_set[_with_key]().

Parameters
lidlog_item_data struct to set the value on
iinteger to set
Return values
truelid was nullptr (possibly: OOM, could not set up log_item)
falseall's well

◆ log_item_set_lexstring()

bool log_item_set_lexstring ( log_item_data lid,
const char *  s,
size_t  s_len 
)

Set a string value on a log_item.

Fails gracefully if not log_item_data is supplied, so it can safely wrap log_line_item_set[_with_key]().

Parameters
lidlog_item_data struct to set the value on
spointer to string
s_lenlength of string
Return values
truelid was nullptr (possibly: OOM, could not set up log_item)
falseall's well

Fails gracefully if no log_item_data is supplied, so it can safely wrap log_line_item_set[_with_key]().

Parameters
lidlog_item_data struct to set the value on
spointer to string
s_lenlength of string
Return values
truelid was nullptr (possibly: OOM, could not set up log_item)
falseall's well

◆ log_item_set_with_key()

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

Will return a pointer to the item's log_item_data struct for convenience. This is mostly interesting for filters and other services that create items that are not part of a log_line; sources etc. that intend to create an item for a log_line (the more common case) should usually use the below line_item_set_with_key() which creates an item (like this function does), but also correctly inserts it into a log_line.

Parameters
lithe log_item to work on
tthe item-type
keythe key to set on the item. ignored for non-generic types (may pass nullptr for those) see alloc
allocLOG_ITEM_FREE_KEY if key was allocated by caller LOG_ITEM_FREE_NONE if key was not allocated Allocated keys will automatically free()d when the log_item is. The log_item's alloc flags will be set to the submitted value; specifically, any pre-existing value will be clobbered. It is therefore WRONG a) to use this on a log_item that already has a key; it should only be used on freshly init'd log_items; b) to use this on a log_item that already has a value (specifically, an allocated one); the correct order is to init a log_item, then set up type and key, and finally to set the value. If said value is an allocated string, the log_item's alloc should be bitwise or'd with LOG_ITEM_FREE_VALUE.
Return values
apointer to the log_item's log_data, for easy chaining: log_item_set_with_key(...)->data_integer= 1;

◆ log_item_string_class()

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

Parameters
clog item class to examine
Return values
trueif of a string class
falseif not of a string class

◆ log_item_wellknown_by_name()

int log_item_wellknown_by_name ( const char *  key,
size_t  len 
)

See whether a string is a wellknown field name.

Parameters
keypotential key starts here
lenlength of the string to examine
Return values
LOG_ITEM_TYPE_RESERVEDreserved, but not "wellknown" key
LOG_ITEM_TYPE_NOT_FOUNDkey not found
>0index in array of wellknowns

◆ log_item_wellknown_by_type()

int log_item_wellknown_by_type ( log_item_type  t)

See whether a type is wellknown.

Parameters
tlog item type to examine
Return values
LOG_ITEM_TYPE_NOT_FOUNDkey not found
>0index in array of wellknowns

◆ log_item_wellknown_get_class()

log_item_class log_item_wellknown_get_class ( uint  idx)

Accessor: from a record describing a wellknown key, get its class.

Parameters
idxindex in array of wellknowns, see log_item_wellknown_by_...()
Return values
thelog item class for the wellknown key

◆ log_item_wellknown_get_name()

const char* log_item_wellknown_get_name ( uint  idx)

Accessor: from a record describing a wellknown key, get its name.

Parameters
idxindex in array of wellknowns, see log_item_wellknown_by_...()
Return values
name(NTBS)

◆ log_item_wellknown_get_type()

log_item_type log_item_wellknown_get_type ( uint  idx)

Accessor: from a record describing a wellknown key, get its type.

Parameters
idxindex in array of wellknowns, see log_item_wellknown_by_...()
Return values
thelog item type for the wellknown key

◆ log_label_from_prio()

const char* log_label_from_prio ( int  prio)

Convenience function: Derive a log label ("error", "warning", "information") from a severity.

Parameters
priothe severity/prio in question
Returns
a label corresponding to that priority.
Return values
ERRORfor prio of ERROR_LEVEL or higher
Warningfor prio of WARNING_LEVEL
Noteotherwise
Parameters
priothe severity/prio in question
Returns
a label corresponding to that priority.
Return values
Systemfor prio of SYSTEM_LEVEL
Errorfor prio of ERROR_LEVEL
Warningfor prio of WARNING_LEVEL
Notefor prio of INFORMATION_LEVEL

◆ log_line_exit()

void log_line_exit ( log_line ll)

Release a log_line allocated with log_line_init.

Return values
nullptrcould not set up buffer (too small?)
otheraddress of the newly initialized log_line

Release a log_line allocated with log_line_init.

Parameters
lla log_line previously allocated with line_init()

◆ log_line_full()

bool log_line_full ( log_line ll)

Predicate indicating whether a log line is "willing" to accept any more key/value pairs.

Parameters
llthe log-line to examine
Return values
falseif not full / if able to accept another log_item
trueif full

◆ log_line_index_by_item()

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.

This variant accepts a reference item and looks for an item that is of the same type (for wellknown types), or one that is of a generic type, and with the same key name (for generic types). For example, a reference item containing a generic string with key "foo" will a generic string, integer, or float with the key "foo".

Parameters
lllog line
refa reference item of the log item type to look for
Return values
<0none found
>=0index of the key/value pair in the log line

Find the (index of the) first key/value pair of the given type in the log line.

This variant accepts a reference item and looks for an item that is of the same type (for wellknown types), or one that is of a generic type, and with the same key name (for generic types). For example, a reference item containing a generic string with key "foo" will a generic string, integer, or float with the key "foo".

Parameters
lllog line
refa reference item of the log item type to look for
Return values
<0none found
>=0index of the key/value pair in the log line

◆ log_line_index_by_name()

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.

Parameters
lllog line
keythe key to look for
Return values
-1none found
-2invalid search-key given
-3no log_line given
>=0index of the key/value pair in the log line

◆ log_line_index_by_type()

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.

Parameters
lllog line
tthe log item type to look for
Return values
<0none found
>=0index of the key/value pair in the log line

Find the (index of the) first key/value pair of the given type in the log line.

Parameters
lllog line
tthe log item type to look for
Return values
<0none found
>=0index of the key/value pair in the log line

◆ log_line_init()

log_line* log_line_init ( )

Initialize a log_line.

Return values
nullptrcould not set up buffer (too small?)
otheraddress of the newly initialized log_line

Initialize a log_line.

Return values
nullptrcould not set up buffer (too small?)
otheraddress of the newly initialized log_line

◆ log_line_item_by_name()

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.

Parameters
lllog line
keythe key to look for
Return values
nullptritem not found
otherwisepointer to the item (not a copy thereof!)

◆ log_line_item_count()

int log_line_item_count ( log_line ll)

How many items are currently set on the given log_line?

Parameters
llthe log-line to examine
Return values
thenumber of items set

◆ log_line_item_free()

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.

This frees whichever of key and value were dynamically allocated. This leaves a "gap" in the bag that may immediately be overwritten with an updated element. If the intention is to remove the item without replacing it, use log_line_item_remove() instead!

Parameters
lllog_line
elemindex of the key/value pair to release

◆ log_line_item_free_all()

void log_line_item_free_all ( log_line ll)

Release all log line items (key/value pairs) in log line ll.

This frees whichever keys and values were dynamically allocated.

Parameters
lllog_line

◆ log_line_item_init()

log_item* log_line_item_init ( log_line ll)

Initializes an entry in a log line for use.

This simply puts it in a defined state; if you wish to reset an existing item, see log_item_free(). This resets the element beyond the last. The element count is not adjusted; this is for the caller to do once it sets up a valid element to suit its needs in the cleared slot. Finally, it is up to the caller to make sure that an element can be allocated.

Parameters
llthe log-line to initialize a log_item in
Return values
theaddress of the cleared log_item

◆ log_line_item_remove()

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.

This frees whichever of key and value were dynamically allocated. This moves any trailing items to fill the "gap" and decreases the counter of elements in the log line. If the intention is to leave a "gap" in the bag that may immediately be overwritten with an updated element, use log_line_item_free() instead!

Parameters
lllog_line
elemindex of the key/value pair to release

◆ log_line_item_set()

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

On success, the number of registered items on the log line is increased, the item's type is added to the log_line's "seen" property, and a pointer to the item's log_item_data struct is returned for convenience.

It is up to the caller to ensure the log_line can accept more items (e.g. by using log_line_full(ll)).

The allocation of this item will be LOG_ITEM_FREE_NONE; specifically, any pre-existing value will be clobbered. It is therefore WRONG a) to use this on a log_item that already has a key; it should only be used on freshly init'd log_items; b) to use this on a log_item that already has a value (specifically, an allocated one); the correct order is to init a log_item, then set up type and key, and finally to set the value. If said value is an allocated string, the log_item's alloc should be bitwise or'd with LOG_ITEM_FREE_VALUE.

Parameters
llthe log_line to work on
tthe item-type
Return values
apointer to the log_item's log_data, for easy chaining: log_line_item_set_with_key(...)->data_integer= 1;

On success, the number of registered items on the log line is increased, the item's type is added to the log_line's "seen" property, and a pointer to the item's log_item_data struct is returned for convenience.

The allocation of this item will be LOG_ITEM_FREE_NONE; specifically, any pre-existing value will be clobbered. It is therefore WRONG a) to use this on a log_item that already has a key; it should only be used on freshly init'd log_items; b) to use this on a log_item that already has a value (specifically, an allocated one); the correct order is to init a log_item, then set up type and key, and finally to set the value. If said value is an allocated string, the log_item's alloc should be bitwise or'd with LOG_ITEM_FREE_VALUE.

Parameters
llthe log_line to work on
tthe item-type
Return values
!nullptra pointer to the log_item's log_data, for easy chaining: log_line_item_set(...)->data_integer= 1;
nullptrcould not create a log_item in given log_line

◆ log_line_item_set_with_key()

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

It is up to the caller to ensure the log_line can accept more items (e.g. by using log_line_full(ll)). On success, the number of registered items on the log line is increased, the item's type is added to the log_line's "seen" property, and a pointer to the item's log_item_data struct is returned for convenience.

Parameters
llthe log_line to work on
tthe item-type
keythe key to set on the item. ignored for non-generic types (may pass nullptr for those) see alloc
allocLOG_ITEM_FREE_KEY if key was allocated by caller LOG_ITEM_FREE_NONE if key was not allocated Allocated keys will automatically free()d when the log_item is. The log_item's alloc flags will be set to the submitted value; specifically, any pre-existing value will be clobbered. It is therefore WRONG a) to use this on a log_item that already has a key; it should only be used on freshly init'd log_items; b) to use this on a log_item that already has a value (specifically, an allocated one); the correct order is to init a log_item, then set up type and key, and finally to set the value. If said value is an allocated string, the log_item's alloc should be bitwise or'd with LOG_ITEM_FREE_VALUE.
Return values
apointer to the log_item's log_data, for easy chaining: log_line_item_set_with_key(...)->data_integer= 1;

On success, the number of registered items on the log line is increased, the item's type is added to the log_line's "seen" property, and a pointer to the item's log_item_data struct is returned for convenience.

Parameters
llthe log_line to work on
tthe item-type
keythe key to set on the item. ignored for non-generic types (may pass nullptr for those) see alloc
allocLOG_ITEM_FREE_KEY if key was allocated by caller LOG_ITEM_FREE_NONE if key was not allocated Allocated keys will automatically free()d when the log_item is. The log_item's alloc flags will be set to the submitted value; specifically, any pre-existing value will be clobbered. It is therefore WRONG a) to use this on a log_item that already has a key; it should only be used on freshly init'd log_items; b) to use this on a log_item that already has a value (specifically, an allocated one); the correct order is to init a log_item, then set up type and key, and finally to set the value. If said value is an allocated string, the log_item's alloc should be bitwise or'd with LOG_ITEM_FREE_VALUE.
Return values
!nullptra pointer to the log_item's log_data, for easy chaining: log_line_item_set_with_key(...)->data_integer= 1;
nullptrcould not create a log_item in given log_line

◆ log_line_item_types_seen()

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.

Parameters
llthe log_line to examine
mthe log_type to test for
Return values
0not present
!=0present

◆ log_line_submit()

int log_line_submit ( log_line ll)

Complete, filter, and write submitted log items.

This expects a log_line collection of log-related key/value pairs, e.g. from log_message().

Where missing, timestamp, priority, thread-ID (if any) and so forth are added.

Log item source services, log item filters, and log item sinks are then called.

Parameters
llkey/value pairs describing info to log
Return values
intnumber of fields in created log line

◆ 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,
struct System_status_var query_start_status 
)

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

Parameters
thdthread handle
query_start_statusPointer to a snapshot of thd->status_var taken at the start of execution

◆ log_slow_statement()

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.

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
query_start_statusPointer to a snapshot of thd->status_var taken at the start of execution

◆ log_string_compare()

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.

Server-internal. External services should access these via the log_builtins service API (cf. preamble for this file). Compare two NUL-terminated byte strings

Note that when comparing without length limit, the long string is greater if they're equal up to the length of the shorter string, but the shorter string will be considered greater if its "value" up to that point is greater:

compare 'abc','abcd': -100 (longer wins if otherwise same) compare 'abca','abcd': -3 (higher value wins) compare 'abcaaaaa','abcd': -3 (higher value wins)

Parameters
athe first string
bthe second string
lencompare at most this many characters – 0 for no limit
case_insensitiveignore upper/lower case in comparison
Return values
-1a < b
0a == b
1a > b

Modular logger: log line and key/value manipulation helpers.

Note that when comparing without length limit, the long string is greater if they're equal up to the length of the shorter string, but the shorter string will be considered greater if its "value" up to that point is greater:

compare 'abc','abcd': -100 (longer wins if otherwise same) compare 'abca','abcd': -3 (higher value wins) compare 'abcaaaaa','abcd': -3 (higher value wins)

Parameters
athe first string
bthe second string
lencompare at most this many characters – 0 for no limit
case_insensitiveignore upper/lower case in comparison
Return values
-1a < b
0a == b
1a > b

◆ log_vmessage()

int log_vmessage ( int  log_type,
va_list  fili 
)

Write a message to a log (for now just used for error log).

This is a variadic convenience interface to the logging components (which use the log_line structure internally), e.g.

log_message(LOG_TYPE_ERROR, LOG_ITEM_LOG_PRIO, INFORMATION_LEVEL, LOG_ITEM_LOG_MESSAGE, "file %s is %f %% yellow", filename, yellowfication);

For use by legacy sql_print_*(), legacy my_plugin_log_message(); also available via the log_builtins service as message().

Wherever possible, use the fluent C++ wrapper LogErr() (see log_builtins.h) instead.

See log_shared.h for LOG_TYPEs as well as for allowed LOG_ITEM_ types.

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

Interim helper: write to the default error stream.

Automatically appends a "\n", so the caller needn't. Does its own locking.

Parameters
bufferdata to write
lengthlength of the data
Return values
intnumber of added fields, if any

◆ make_iso8601_timestamp()

int make_iso8601_timestamp ( char *  buf,
ulonglong  utime,
int  mode 
)

Make and return an ISO 8601 / RFC 3339 compliant timestamp.

Heeds log_timestamps.

Parameters
bufA buffer of at least 26 bytes to store the timestamp in (19 + tzinfo tail + \0)
utimeMicroseconds since the epoch
modeif 0, use UTC; if 1, use local time
Return values
lengthof timestamp (excluding \0)

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

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

◆ reopen_error_log()

bool reopen_error_log ( )

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

Variable Documentation

◆ LOG_FILE

const uint LOG_FILE = 2
static

◆ LOG_NONE

const uint LOG_NONE = 1
static

◆ LOG_TABLE

const uint LOG_TABLE = 4
static

◆ log_throttle_qni

Slow_log_throttle log_throttle_qni

◆ query_logger

Query_logger query_logger