MySQL  8.0.27
Source Code Documentation
log_builtins_imp.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, 2021, Oracle and/or its affiliates.
2 
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6 
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef LOG_BUILTINS_IMP_H
24 #define LOG_BUILTINS_IMP_H
25 
26 #include <stdarg.h>
27 
28 #include "log_builtins_internal.h"
29 
30 #ifdef IN_DOXYGEN
32 #else
34 #endif
35 
36 /**
37  This defines built-in functions for use by logging services.
38  These helpers are organized into a number of APIs grouping
39  related functionality.
40 
41  This file defines internals; to use the logger from a service,
42  include log_builtins.h instead.
43 
44  For documentation of the individual functions, see log_builtins.cc
45 */
46 
48 
49 /**
50  Name of internal filtering engine (so we may recognize it when the
51  user refers to it by name in log_error_services).
52 */
53 #define LOG_BUILTINS_FILTER "log_filter_internal"
54 
55 /**
56  Name of internal log writer (so we may recognize it when the user
57  refers to it by name in log_error_services).
58 */
59 #define LOG_BUILTINS_SINK "log_sink_internal"
60 
61 /**
62  Name of buffered log writer. This sink is used internally during
63  start-up until we know where to write and how to filter, and have
64  all the components to do so. While we don't let the DBA add this
65  sink to the logging pipeline once we're out of start-up, we have
66  a name for this to be able to correctly tag its record in the
67  service-cache.
68 */
69 #define LOG_BUILTINS_BUFFER "log_sink_buffer"
70 
71 /**
72  Default services pipeline for log_builtins_error_stack().
73 */
74 #define LOG_ERROR_SERVICES_DEFAULT LOG_BUILTINS_FILTER "; " LOG_BUILTINS_SINK
75 
76 // see include/mysql/components/services/log_builtins.h
77 
78 /**
79  Primitives for services to interact with the structured logger:
80  functions pertaining to log_line and log_item data
81 */
83  public: /* Service Implementations */
85  static DEFINE_METHOD(int, wellknown_by_name,
86  (const char *key, size_t length));
88  static DEFINE_METHOD(const char *, wellknown_get_name, (uint i));
89 
90  static DEFINE_METHOD(int, item_inconsistent, (log_item * li));
94 
95  static DEFINE_METHOD(bool, item_set_int, (log_item_data * lid, longlong i));
96  static DEFINE_METHOD(bool, item_set_float, (log_item_data * lid, double f));
97  static DEFINE_METHOD(bool, item_set_lexstring,
98  (log_item_data * lid, const char *s, size_t s_len));
99  static DEFINE_METHOD(bool, item_set_cstring,
100  (log_item_data * lid, const char *s));
101 
103  (log_item * li, log_item_type t, const char *key,
104  uint32 alloc));
106  (log_item * li, log_item_type t));
107 
109  (log_line * ll, log_item_type t, const char *key,
110  uint32 alloc));
112  (log_line * ll, log_item_type t));
113 
114  static DEFINE_METHOD(log_line *, line_init, ());
115  static DEFINE_METHOD(void, line_exit, (log_line * ll));
116  static DEFINE_METHOD(int, line_item_count, (log_line * ll));
117 
119  (log_line * ll, log_item_type_mask m));
120 
122 
124  (log_line * ll));
125  static DEFINE_METHOD(void, line_item_iter_release, (log_item_iter * it));
129  (log_item_iter * it));
130 
131  static DEFINE_METHOD(int, line_submit, (log_line * ll));
132 
133  static DEFINE_METHOD(int, message, (int log_type, ...));
134 
135  static DEFINE_METHOD(int, sanitize, (log_item * li));
136 
137  static DEFINE_METHOD(const char *, errmsg_by_errcode, (int mysql_errcode));
138 
139  static DEFINE_METHOD(longlong, errcode_by_errsymbol, (const char *sym));
140 
141  static DEFINE_METHOD(const char *, label_from_prio, (int prio));
142 
144  (const char *timestamp, size_t len));
145 
147  (const char *file, void **my_errstream));
148 
150  (void *my_errstream, const char *buffer, size_t length));
151 
152  static DEFINE_METHOD(int, dedicated_errstream, (void *my_errstream));
153 
155  (void **my_errstream));
156 
158  (const char *file, void **my_errstream));
159 };
160 
161 /**
162  String primitives for logging services.
163 */
165  public: /* Service Implementations */
166  static DEFINE_METHOD(void *, malloc, (size_t len));
167  static DEFINE_METHOD(char *, strndup, (const char *fm, size_t len));
168  static DEFINE_METHOD(void, free, (void *ptr));
169  static DEFINE_METHOD(size_t, length, (const char *s));
170  static DEFINE_METHOD(char *, find_first, (const char *s, int c));
171  static DEFINE_METHOD(char *, find_last, (const char *s, int c));
172 
173  static DEFINE_METHOD(int, compare,
174  (const char *a, const char *b, size_t len,
175  bool case_insensitive));
176 
177  static DEFINE_METHOD(size_t, substitutev,
178  (char *to, size_t n, const char *fmt, va_list ap))
179  MY_ATTRIBUTE((format(printf, 3, 0)));
180 
181  static DEFINE_METHOD(size_t, substitute,
182  (char *to, size_t n, const char *fmt, ...))
183  MY_ATTRIBUTE((format(printf, 3, 4)));
184 };
185 
186 /**
187  Temporary primitives for logging services.
188 */
190  public: /* Service Implementations */
191  static DEFINE_METHOD(size_t, notify_client,
192  (void *thd, uint severity, uint code, char *to, size_t n,
193  const char *format, ...))
194  MY_ATTRIBUTE((format(printf, 6, 7)));
195 };
196 
197 /**
198  Syslog/Eventlog functions for logging services.
199 */
201  public: /* Service Implementations */
203  (const char *name, int option, int facility));
205  (enum loglevel level, const char *msg));
206  static DEFINE_METHOD(log_service_error, close, (void));
207 };
208 
209 #endif /* LOG_BUILTINS_IMP_H */
Primitives for services to interact with the structured logger: functions pertaining to log_line and ...
Definition: log_builtins_imp.h:82
static log_item * line_item_iter_first(log_item_iter *it) noexcept
Use the log_line iterator to get the first item from the set.
Definition: log_builtins.cc:2465
static log_item_iter * line_item_iter_acquire(log_line *ll) noexcept
Get an iterator for the items in a log_line.
Definition: log_builtins.cc:2432
static int item_inconsistent(log_item *li) noexcept
Sanity check an item.
Definition: log_builtins.cc:2119
static longlong errcode_by_errsymbol(const char *sym) noexcept
Return MySQL error code for a given error symbol.
Definition: log_builtins.cc:2680
static bool item_string_class(log_item_class c) noexcept
Predicate used to determine whether a class is a string class (C-string or Lex-string).
Definition: log_builtins.cc:2146
static log_item_data * item_set(log_item *li, log_item_type t) noexcept
As log_item_set_with_key(), except that the key is automatically derived from the wellknown log_item_...
Definition: log_builtins.cc:2288
static int line_submit(log_line *ll) noexcept
Complete, filter, and write submitted log items.
Definition: log_builtins.cc:2535
static log_item * line_item_iter_next(log_item_iter *it) noexcept
Use the log_line iterator to get the next item from the set.
Definition: log_builtins.cc:2484
static int dedicated_errstream(void *my_errstream) noexcept
Are we writing to a dedicated errstream, or are we sharing it?
Definition: log_builtins.cc:2926
static log_service_error open_errstream(const char *file, void **my_errstream) noexcept
Open an error log file.
Definition: log_builtins.cc:2808
static bool item_set_lexstring(log_item_data *lid, const char *s, size_t s_len) noexcept
Definition: log_builtins.cc:2196
static log_item * line_get_output_buffer(log_line *ll) noexcept
Get log-line's output buffer.
Definition: log_builtins.cc:2419
static int wellknown_by_name(const char *key, size_t length) noexcept
See whether a string is a wellknown field name.
Definition: log_builtins.cc:2073
static log_service_error write_errstream(void *my_errstream, const char *buffer, size_t length) noexcept
Write to an error log file previously opened with open_errstream().
Definition: log_builtins.cc:2901
static const char * wellknown_get_name(uint i) noexcept
Accessor: from a record describing a wellknown key, get its name.
Definition: log_builtins.cc:2095
static int sanitize(log_item *li) noexcept
Definition: log_builtins.cc:2594
static bool item_generic_type(log_item_type t) noexcept
Predicate used to determine whether a type is generic (generic string, generic float,...
Definition: log_builtins.cc:2133
static bool item_numeric_class(log_item_class c) noexcept
Predicate used to determine whether a class is a numeric class (integer or float).
Definition: log_builtins.cc:2159
static log_service_error close_errstream(void **my_errstream) noexcept
Close an error log file previously opened with open_errstream() (wrapper for the component system).
Definition: log_builtins.cc:2989
static log_item_data * item_set_with_key(log_item *li, log_item_type t, const char *key, uint32 alloc) noexcept
Create new log item with key name "key", and allocation flags of "alloc" (see enum_log_item_free).
Definition: log_builtins.cc:2252
static int message(int log_type,...) noexcept
Submit a log-message for log "log_type".
Definition: log_builtins.cc:2574
static const char * label_from_prio(int prio) noexcept
Convenience function: Derive a log label ("error", "warning", "information") from a severity.
Definition: log_builtins.cc:2695
static bool item_set_cstring(log_item_data *lid, const char *s) noexcept
Set a string value on a log_item.
Definition: log_builtins.cc:2212
static log_line * line_init() noexcept
Dynamically allocate and initialize a log_line.
Definition: log_builtins.cc:2368
static void line_item_iter_release(log_item_iter *it) noexcept
Release an iterator for the items in a log_line.
Definition: log_builtins.cc:2450
static bool item_set_int(log_item_data *lid, longlong i) noexcept
Set an integer value on a log_item.
Definition: log_builtins.cc:2175
static void line_exit(log_line *ll) noexcept
Release a log_line allocated with line_init()
Definition: log_builtins.cc:2377
static ulonglong parse_iso8601_timestamp(const char *timestamp, size_t len) noexcept
Parse a ISO8601 timestamp and return the number of microseconds since the epoch.
Definition: log_builtins.cc:2711
static bool item_set_float(log_item_data *lid, double f) noexcept
Set a floating point value on a log_item.
Definition: log_builtins.cc:2191
static log_item_data * line_item_set_with_key(log_line *ll, log_item_type t, const char *key, uint32 alloc) noexcept
Create new log item in log line "ll", with key name "key", and allocation flags of "alloc" (see enum_...
Definition: log_builtins.cc:2327
static const char * errmsg_by_errcode(int mysql_errcode) noexcept
Return MySQL error message for a given error code.
Definition: log_builtins.cc:2667
static log_item_type_mask line_item_types_seen(log_line *ll, log_item_type_mask m) noexcept
Test whether a given type is presumed present on the log line.
Definition: log_builtins.cc:2402
static int line_item_count(log_line *ll) noexcept
How many items are currently set on the given log_line?
Definition: log_builtins.cc:2388
static log_service_error reopen_errstream(const char *file, void **my_errstream) noexcept
Re-open an error log file (primarily to facilitate flush/log-rotation)
Definition: log_builtins.cc:3023
static log_item_data * line_item_set(log_line *ll, log_item_type t) noexcept
Create a new log item of well-known type "t" in log line "ll".
Definition: log_builtins.cc:2358
static int wellknown_by_type(log_item_type t) noexcept
See whether a type is wellknown.
Definition: log_builtins.cc:2058
static log_item * line_item_iter_current(log_item_iter *it) noexcept
Use the log_line iterator to get the current item from the set.
Definition: log_builtins.cc:2504
static log_item_type wellknown_get_type(uint i) noexcept
Accessor: from a record describing a wellknown key, get its type.
Definition: log_builtins.cc:2084
String primitives for logging services.
Definition: log_builtins_imp.h:164
static size_t length(const char *s) noexcept
Wrapper for strlen(): length of a nul-terminated byte string.
Definition: log_builtins.cc:3125
static size_t substitutev(char *to, size_t n, const char *fmt, va_list ap) noexcept
Wrapper for vsnprintf(): Replace all % in format string with variables from list.
Definition: log_builtins.cc:3185
static char * find_last(const char *s, int c) noexcept
Wrapper for strrchr(): find character in string, from the right.
Definition: log_builtins.cc:3141
static char * strndup(const char *fm, size_t len) noexcept
Wrapper for my_strndup(): Alloc (len+1) bytes, then copy len bytes from fm, and \0 terminate.
Definition: log_builtins.cc:3111
static int compare(const char *a, const char *b, size_t len, bool case_insensitive) noexcept
Compare two NUL-terminated byte strings.
Definition: log_builtins.cc:3169
static size_t substitute(char *to, size_t n, const char *fmt,...) noexcept
Wrapper for vsnprintf(): Replace all % in format string with variables from list.
Definition: log_builtins.cc:3194
static void * malloc(size_t len) noexcept
Wrapper for my_malloc(): Alloc (len+1) bytes.
Definition: log_builtins.cc:3098
static void free(void *ptr) noexcept
Wrapper for my_free(): free allocated memory.
Definition: log_builtins.cc:3118
static char * find_first(const char *s, int c) noexcept
Wrapper for strchr(): find character in string, from the left.
Definition: log_builtins.cc:3133
Syslog/Eventlog functions for logging services.
Definition: log_builtins_imp.h:200
static log_service_error open(const char *name, int option, int facility) noexcept
Wrapper for mysys' my_openlog.
Definition: log_builtins.cc:3251
static log_service_error close(void) noexcept
Wrapper for mysys' my_closelog.
Definition: log_builtins.cc:3302
static log_service_error write(enum loglevel level, const char *msg) noexcept
Wrapper for mysys' my_syslog.
Definition: log_builtins.cc:3285
Temporary primitives for logging services.
Definition: log_builtins_imp.h:189
static size_t notify_client(void *thd, uint severity, uint code, char *to, size_t n, const char *format,...) noexcept
Definition: log_builtins.cc:3211
enum enum_log_service_error log_service_error
Error codes.
enum enum_log_item_class log_item_class
enum enum_log_item_type log_item_type
item_type – what to log
uint64 log_item_type_mask
a bit mask of log_types.
Definition: log_shared.h:215
static int log_type
Definition: mi_log.cc:46
unsigned long long int ulonglong
Definition: my_inttypes.h:55
long long int longlong
Definition: my_inttypes.h:54
uint32_t uint32
Definition: my_inttypes.h:66
loglevel
Definition: my_loglevel.h:40
Definition: os0file.h:85
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:75
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:391
const string timestamp("\"Timestamp\"")
required string key
Definition: replication_asynchronous_connection_failover.proto:59
#define DEFINE_METHOD(retval, name, args)
A macro to ensure method implementation has required properties, that is it does not throw exceptions...
Definition: service_implementation.h:78
case opt name
Definition: sslopt-case.h:32
Iterator over the key/value pairs of a log_line.
Definition: keyring_log_builtins_definition.cc:62
Definition: log_shared.h:195
log_line ("log event")
Definition: keyring_log_builtins_definition.cc:70
unsigned int uint
Definition: uca-dump.cc:29
Definition: log_shared.h:184
int n
Definition: xcom_base.cc:505