MySQL 8.4.0
Source Code Documentation
log_builtins.cc File Reference
#include "log_builtins_filter_imp.h"
#include "log_builtins_imp.h"
#include "log_sink_buffer.h"
#include "log_sink_perfschema.h"
#include "log_sink_trad.h"
#include "m_string.h"
#include "mysys_err.h"
#include <mysql/components/services/log_shared.h>
#include "my_time.h"
#include "sql/current_thd.h"
#include "sql/log.h"
#include "sql/mysqld.h"
#include "sql/sql_class.h"
#include "sql/tztime.h"
#include "string_with_len.h"
#include "mysql/components/services/log_builtins.h"
#include <syslog.h>

Classes

struct  log_service_cache_entry_free
 
struct  log_errstream
 An error-stream. More...
 
struct  _log_item_wellknown_key
 Pre-defined "well-known" keys, as opposed to ad hoc ones, for key/value pairs in logging. More...
 

Macros

#define LOG_SUBSYSTEM_TAG   "Server"
 
#define LOG_SERVICES_PREFIX   "log_service"
 Name of the interface that log-services implements. More...
 
#define LOG_SERVICES_URN   "file://component_"
 URN-prefix used to load a log-component. More...
 

Typedefs

using cache_entry_with_deleter = unique_ptr< log_service_cache_entry, log_service_cache_entry_free >
 We're caching handles to the services used in error logging as looking them up is costly. More...
 
typedef struct _log_item_wellknown_key log_item_wellknown_key
 Pre-defined "well-known" keys, as opposed to ad hoc ones, for key/value pairs in logging. More...
 

Functions

bool log_line_buffer_event (log_line *ll)
 Initial log-processor: Just buffer events until we have external log-components. More...
 
void log_line_process_hook_set (log_line_processor llp)
 Set the log-event processor. More...
 
log_line_processor log_line_process_hook_get (void)
 Get current log-event processor. More...
 
ulonglong log_builtins_started ()
 When the logger-core was initialized. More...
 
void log_error_stage_set (enum log_error_stage les)
 Set error-logging stage hint (e.g. are loadable services available yet?). More...
 
enum log_error_stage log_error_stage_get ()
 What mode is error-logging in (e.g. are loadable services available yet)? More...
 
static int log_service_check_if_builtin (const char *name, size_t len)
 Test whether a given log-service name refers to a built-in service (built-in filter or built-in sink at this point). More...
 
static bool log_service_has_characteristics (log_service_cache_entry *sce, int required_flags)
 Test whether given service has all of the given characteristics. 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...
 
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...
 
int log_item_inconsistent (log_item *li)
 Sanity check an item. More...
 
void log_item_free (log_item *li)
 Release any of key and value on a log-item that were dynamically allocated. More...
 
log_linelog_line_init ()
 Initialize a log_line. More...
 
void log_line_exit (log_line *ll)
 Release a log_line allocated with line_init() More...
 
log_itemlog_line_get_output_buffer (log_line *ll)
 Get log-line's output buffer. 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...
 
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...
 
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_buffer (log_item_data *lid, char *s, size_t s_len)
 Set a string buffer 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...
 
const char * log_label_from_prio (int prio)
 Convenience function: Derive a log label ("error", "warning", "information") from a severity. More...
 
enum loglevel log_prio_from_label (const char *label)
 Derive the event's priority (SYSTEM_LEVEL, ERROR_LEVEL, ...) from a textual label. More...
 
bool log_line_error_stack_run (log_line *ll)
 MySQL server's default log-processor. More...
 
int log_line_submit (log_line *ll)
 Complete, filter, and write submitted log items. More...
 
int make_iso8601_timestamp (char *buf, ulonglong utime, enum enum_iso8601_tzmode mode)
 Make and return an ISO 8601 / RFC 3339 compliant timestamp. More...
 
ulonglong iso8601_timestamp_to_microseconds (const char *timestamp, size_t len)
 Parse a ISO8601 timestamp and return the number of microseconds since the epoch. More...
 
static ssize_t log_builtins_stack_get_service_from_var (const char **s, const char **e, char *d)
 Helper: get token from error stack configuration string. More...
 
static my_h_service log_service_get_by_name (const char *name, size_t len, char **urn)
 Look up a log service by name (in the service registry). More...
 
static log_service_cache_entrylog_service_cache_entry_new (const char *name, size_t name_len, my_h_service srv, char *urn)
 Create a new entry in the cache of log services. More...
 
static int log_service_get_characteristics (my_h_service service)
 Find out characteristics of a service (e.g. More...
 
log_service_instancelog_service_instance_new (log_service_cache_entry *sce, log_line *ll)
 Allocate and open a new instance of a given service. More...
 
static void log_service_instance_release_all ()
 Close and release all instances of all log services. More...
 
int log_builtins_error_stack_flush ()
 Call flush() on all log_services. More...
 
log_error_stack_error log_builtins_error_stack (const char *conf, bool check_only, size_t *pos)
 Set up custom error logging stack. More...
 
void log_builtins_error_stack_wrlock ()
 Acquire an exclusive lock on the error logger core. More...
 
void log_builtins_error_stack_unlock ()
 Release a lock on the error logger core. More...
 
int log_builtins_exit ()
 De-initialize the structured logging subsystem. More...
 
int log_builtins_init ()
 Initialize the structured logging subsystem. More...
 
log_service_error make_log_path (char *result, const char *name_or_ext)
 Create a log-file name (path + name + extension). More...
 
log_service_error log_close_errstream (void **my_errstream)
 Close an error log file previously opened with open_errstream(). More...
 

Variables

PSI_memory_key key_memory_log_error_loaded_services
 
PSI_memory_key key_memory_log_error_stack
 
log_line_processor log_line_process_hook = log_line_buffer_event
 The function pointed to by this hook is run when a log-event is submitted. More...
 
static collation_unordered_map< string, cache_entry_with_deleter > * log_service_cache
 
static mysql_rwlock_t THR_LOCK_log_stack
 Lock for the log "stack" (i.e. More...
 
static mysql_mutex_t THR_LOCK_log_syseventlog
 Make sure only one instance of syslog/Eventlog code runs at a time. More...
 
static ulonglong log_builtins_inited = 0
 When the logger-core was initialized. More...
 
log_service_instancelog_service_instances = nullptr
 Chain of log-service instances. More...
 
log_service_instancelog_sink_pfs_source = nullptr
 The first configured writer that also has a log-reader is the source for the "data" field in performance_schema.error_log. More...
 
static enum log_error_stage log_error_stage_current = LOG_ERROR_STAGE_BUFFERING
 What mode is error-logging in (e.g. are loadable services available yet)? More...
 
static const log_item_wellknown_key log_item_wellknown_keys []
 We support a number of predefined keys, such as "error-code" or "message". More...
 
static uint log_item_wellknown_keys_count
 

Macro Definition Documentation

◆ LOG_SERVICES_PREFIX

#define LOG_SERVICES_PREFIX   "log_service"

Name of the interface that log-services implements.

◆ LOG_SERVICES_URN

#define LOG_SERVICES_URN   "file://component_"

URN-prefix used to load a log-component.

When log-components passed to log_builtins_error_stack() are neither built-in nor have they been loaded already, this prefix will be prepended to their name to look them up using the component framework. Thus, "log_sink_json" will be looked up as "file://component_log_sink_json" and so on.

◆ LOG_SUBSYSTEM_TAG

#define LOG_SUBSYSTEM_TAG   "Server"

Typedef Documentation

◆ cache_entry_with_deleter

We're caching handles to the services used in error logging as looking them up is costly.

◆ log_item_wellknown_key

Pre-defined "well-known" keys, as opposed to ad hoc ones, for key/value pairs in logging.

Function Documentation

◆ iso8601_timestamp_to_microseconds()

ulonglong iso8601_timestamp_to_microseconds ( const char *  timestamp,
size_t  len 
)

Parse a ISO8601 timestamp and return the number of microseconds since the epoch.

Heeds +/- timezone info if present.

See also
make_iso8601_timestamp()
Parameters
timestampan ASCII string containing an ISO8601 timestamp
lenLength in bytes of the aforementioned string
Returns
microseconds since the epoch

◆ log_builtins_error_stack()

log_error_stack_error 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
LOG_ERROR_STACK_SUCCESSsuccess
LOG_ERROR_STACK_DELIMITER_MISSINGexpected delimiter not found
LOG_ERROR_STACK_SERVICE_MISSINGone or more services not found
LOG_ERROR_STACK_CACHE_ENTRY_OOMcouldn't create service cache entry
LOG_ERROR_STACK_MULTITON_DENIEDtried to multi-open singleton
LOG_ERROR_STACK_SERVICE_INSTANCE_OOMcouldn't create service instance entry
LOG_ERROR_STACK_ENDS_IN_NON_SINKlast element should be a sink
LOG_ERROR_STACK_SERVICE_UNAVAILABLEservice only available during start-up (may not be set by the user)
LOG_ERROR_STACK_NO_PFS_SUPPORT(check_only warning) no sink with performance_schema support selected
LOG_ERROR_STACK_NO_LOG_PARSER(check_only warning) no sink providing a log-parser selected
LOG_ERROR_MULTIPLE_FILTERS(check_only warning) more than one filter service selected
LOG_ERROR_UNEXPECTED_DELIMITER_FOUNDservice starts with a delimiter
LOG_ERROR_MIXED_DELIMITERSuse ',' or ';', not both!

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

Returns
0 if no problems occurred, otherwise the negative count of the components that failed to flush

◆ log_builtins_error_stack_unlock()

void log_builtins_error_stack_unlock ( )

Release a lock on the error logger core.

◆ log_builtins_error_stack_wrlock()

void log_builtins_error_stack_wrlock ( )

Acquire an exclusive lock on the error logger core.

Used e.g. to pause all logging while the previous run's log is read to performance_schema.error_log.

◆ 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 initialize buffered logging lock

◆ log_builtins_stack_get_service_from_var()

static ssize_t log_builtins_stack_get_service_from_var ( const char **  s,
const char **  e,
char *  d 
)
static

Helper: get token from error stack configuration string.

Parameters
[in,out]sstart of the token (may be positioned on whitespace on call; this will be adjusted to the first non-white character)
[out]eend of the token
[in,out]ddelimiter (in: last used, \0 if none; out: detected here)
Return values
<0an error occurred
>=0the length in bytes of the token

◆ log_builtins_started()

ulonglong log_builtins_started ( )

When the logger-core was initialized.

Return values
0logger-core is not currently available
>0time (micro-seconds since the epoch) the logger became available

◆ log_close_errstream()

log_service_error log_close_errstream ( void **  my_errstream)

Close an error log file previously opened with open_errstream().

Parameters
my_errstreama handle describing the log file
Returns
LOG_SERVICE_SUCCESS on success

◆ log_error_stage_get()

enum log_error_stage log_error_stage_get ( void  )

What mode is error-logging in (e.g. are loadable services available yet)?

◆ log_error_stage_set()

void log_error_stage_set ( enum log_error_stage  les)

Set error-logging stage hint (e.g. are loadable services available yet?).

◆ 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

◆ 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

◆ 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

◆ log_item_inconsistent()

int log_item_inconsistent ( log_item li)

Sanity check an item.

Certain log sinks have very low requirements with regard to the data they receive; they write keys as strings, and then data according to the item's class (string, integer, or float), formatted to the sink's standards (e.g. JSON, XML, ...). Code that has higher requirements can use this check to see whether the given item is of a known type (whether generic or wellknown), whether the given type and class agree, and whether in case of a well-known type, the given key is correct for that type. If your code generates items that don't pass this check, you should probably go meditate on it.

Parameters
lithe log_item to check
Return values
LOG_ITEM_OKno problems
LOG_ITEM_TYPE_NOT_FOUNDunknown item type
LOG_ITEM_CLASS_MISMATCHitem_class derived from type isn't what's set on the item
LOG_ITEM_KEY_MISMATCHclass not generic, so key should match wellknown
LOG_ITEM_STRING_NULLclass is string, pointer is nullptr
LOG_ITEM_KEY_NULLno key set (this is legal e.g. on aux items of filter rules, but should not occur in a log_line, i.e., log_sinks are within their rights to discard such items)

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

bool log_item_set_buffer ( log_item_data lid,
char *  s,
size_t  s_len 
)

Set a string buffer on a log_item.

On success, the caller should change the item_class to LOG_BUFFER.

Parameters
lidlog_item_data struct to set the value on
spointer to string-buffer (non-const)
s_lenbuffer-size
Return values
truecould not set a valid buffer
falseitem was assigned a buffer

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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

◆ log_line_buffer_event()

bool log_line_buffer_event ( log_line ll)

Initial log-processor: Just buffer events until we have external log-components.

Parameters
llthe log-event to buffer
Return values
truelog_sink_buffer() failed
falselog_sink_buffer() succeeded

◆ log_line_error_stack_run()

bool log_line_error_stack_run ( log_line ll)

MySQL server's default log-processor.

Apply all components (filters, sinks, ...) in the log stack to a given event.

Parameters
llthe log-event to process
Return values
truefailure
falsesuccess

◆ log_line_exit()

void log_line_exit ( log_line ll)

Release a log_line allocated with line_init()

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

log_item * log_line_get_output_buffer ( log_line ll)

Get log-line's output buffer.

If the logger core provides this buffer, the log-service may use it to assemble its output therein and implicitly return it to the core. Participation is required for services that support populating performance_schema.error_log, and optional for all others.

Parameters
llthe log_line to examine
Return values
nullptrsuccess, an output buffer is available
otherwisefailure, no output buffer is available

◆ 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

◆ 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

◆ 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

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

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

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

log_line_processor log_line_process_hook_get ( void  )

Get current log-event processor.

When a log-event is submitted, a function is applied to that event. That function usually either buffers the event for later processing, or filters and logs the event. log_line_process_hook_get() returns a pointer to that function.

Return values
apointer to a log-event processing function

◆ log_line_process_hook_set()

void log_line_process_hook_set ( log_line_processor  llp)

Set the log-event processor.

When a log-event is submitted, a function is applied to that event. That function usually either buffers the event for later processing, or filters and logs the event. That function can be set here.

Parameters
llpA log-processor

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

enum loglevel log_prio_from_label ( const char *  label)

Derive the event's priority (SYSTEM_LEVEL, ERROR_LEVEL, ...) from a textual label.

If the label can not be identified, default to ERROR_LEVEL as it is better to keep something that needn't be kept than to discard something that shouldn't be.

Parameters
labelThe prio label as a \0 terminated C-string.
Return values
thepriority (as an enum loglevel)

◆ log_service_cache_entry_new()

static log_service_cache_entry * log_service_cache_entry_new ( const char *  name,
size_t  name_len,
my_h_service  srv,
char *  urn 
)
static

Create a new entry in the cache of log services.

Parameters
nameName of component that provides the service
name_lenLength of that name
srvThe handle of the log_service
urnPointer to allocated buffer containing an URN, or NULL
Return values
Anew log_service_cache_entry on success
nullptron failure

◆ log_service_check_if_builtin()

static int log_service_check_if_builtin ( const char *  name,
size_t  len 
)
static

Test whether a given log-service name refers to a built-in service (built-in filter or built-in sink at this point).

Parameters
namethe name – either just the component's, or a fully qualified service.component
lenthe length of the aforementioned name
Returns
flags for built-in|singleton|filter (if built-in filter) or flags for built-in|singleton|sink (if built-in sink) otherwise LOG_SERVICE_UNSPECIFIED

◆ log_service_get_by_name()

static my_h_service log_service_get_by_name ( const char *  name,
size_t  len,
char **  urn 
)
static

Look up a log service by name (in the service registry).

Parameters
namename of the component
lenlength of that name
[out]urnif the component was loaded implicitly, returns a pointer to a newly-allocated buffer containing the URN used
Return values
ahandle to that service (or nullptr on failure)

◆ log_service_get_characteristics()

static int log_service_get_characteristics ( my_h_service  service)
static

Find out characteristics of a service (e.g.

whether it is a singleton) by asking it.

(See log_service_chistics for a list of possible characteristics!)

Parameters
servicewhat service to examine
Return values
aset of log_service_chistics flags

◆ log_service_has_characteristics()

static bool log_service_has_characteristics ( log_service_cache_entry sce,
int  required_flags 
)
inlinestatic

Test whether given service has all of the given characteristics.

(See log_service_chistics for a list!)

Parameters
sceservice cache entry for the service in question
required_flagsflags we're interested in (bitwise or'd)
Return values
trueif all given flags are present, false otherwise

◆ log_service_instance_new()

log_service_instance * log_service_instance_new ( log_service_cache_entry sce,
log_line ll 
)

Allocate and open a new instance of a given service.

Parameters
scethe cache-entry for the service
lla log_line containing optional parameters, or nullptr
Returns
a pointer to an instance record or success, nullptr otherwise

◆ log_service_instance_release_all()

static void log_service_instance_release_all ( )
static

Close and release all instances of all log services.

◆ 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

◆ make_iso8601_timestamp()

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

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

Accepts the log_timestamps global variable in its third parameter.

Parameters
bufA buffer of at least iso8601_size bytes to store the timestamp in. The timestamp will be \0 terminated.
utimeMicroseconds since the epoch
modeif 0, use UTC; if 1, use local time
Return values
lengthof timestamp (excluding \0)

◆ make_log_path()

log_service_error make_log_path ( char *  result,
const char *  name_or_ext 
)

Create a log-file name (path + name + extension).

The path will be taken from @log_error. If name + extension are given, they are used. If only an extension is given (argument starts with '.'), the name is taken from @log_error, and the extension is used. If only a name is given (but no extension), the name and a default extension are used.

Parameters
resultBuffer to return to created path+name+extension in. Size must be FN_REFLEN.
name_or_extif beginning with '.': @global.log_error, except with this extension otherwise: use this as file name in the same location as @global.log_error

Value may not contain folder separators!

Return values
LOG_SERVICE_SUCCESSbuffer contains a valid result
LOG_SERVICE_BUFFER_SIZE_INSUFFICIENTan error occurred

Variable Documentation

◆ key_memory_log_error_loaded_services

PSI_memory_key key_memory_log_error_loaded_services

◆ key_memory_log_error_stack

PSI_memory_key key_memory_log_error_stack

◆ log_builtins_inited

ulonglong log_builtins_inited = 0
static

When the logger-core was initialized.

0: logger-core is not currently available >0: time (micro-seconds since the epoch) the logger-core became available

◆ log_error_stage_current

enum log_error_stage log_error_stage_current = LOG_ERROR_STAGE_BUFFERING
static

What mode is error-logging in (e.g. are loadable services available yet)?

◆ log_item_wellknown_keys

const log_item_wellknown_key log_item_wellknown_keys[]
static

We support a number of predefined keys, such as "error-code" or "message".

These are defined here. We also support user-defined "ad hoc" (or "generic") keys that let users of the error stack add values with arbitrary keys (as long as those keys don't coincide with the wellknown ones, anyway).

The idea here is that we want the flexibility of arbitrary keys, while being able to do certain optimizations for the common case. This also allows us to relatively cheaply add some convenience features, e.g. we know that error symbol ("ER_STARTUP") and error code (1451) are related, and can supply one as the other is submitted. Likewise of course, we can use the error code to fetch the associated registered error message string for that error code. Et cetera!

◆ log_item_wellknown_keys_count

uint log_item_wellknown_keys_count
static
Initial value:
=
static const log_item_wellknown_key log_item_wellknown_keys[]
We support a number of predefined keys, such as "error-code" or "message".
Definition: log_builtins.cc:300
Pre-defined "well-known" keys, as opposed to ad hoc ones, for key/value pairs in logging.
Definition: keyring_log_builtins_definition.cc:84

◆ log_line_process_hook

log_line_processor log_line_process_hook = log_line_buffer_event

The function pointed to by this hook is run when a log-event is submitted.

By default (until any sinks are set), we just buffer incoming events.

◆ log_service_cache

collation_unordered_map<string, cache_entry_with_deleter>* log_service_cache
static

◆ log_service_instances

log_service_instance* log_service_instances = nullptr

Chain of log-service instances.

anchor

(Each service can have no/one/several instances.)

◆ log_sink_pfs_source

log_service_instance* log_sink_pfs_source = nullptr

The first configured writer that also has a log-reader is the source for the "data" field in performance_schema.error_log.

log-reader

◆ THR_LOCK_log_stack

mysql_rwlock_t THR_LOCK_log_stack
static

Lock for the log "stack" (i.e.

the list of active log-services). X-locked while stack is changed/configured. S-locked while stack is used.

◆ THR_LOCK_log_syseventlog

mysql_mutex_t THR_LOCK_log_syseventlog
static

Make sure only one instance of syslog/Eventlog code runs at a time.

(The loadable log-service is a singleton, which enforces that at most one instance of it exists. The logger-core has its own lock that serializes access to it. That however does not prevent the logger core and system variable updates from using Eventlog functions concurrently. This lock guards against that. It also serializes any other (non-error logging) users of this service.