MySQL  8.0.17
Source Code Documentation
log_builtins_imp.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
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 #ifdef IN_DOXYGEN
30 #endif
31 
32 /**
33  This defines built-in functions for use by logging services.
34  These helpers are organized into a number of APIs grouping
35  related functionality.
36 
37  This file defines internals; to use the logger from a service,
38  include log_builtins.h instead.
39 
40  For documentation of the individual functions, see log_builtins.cc
41 */
42 
44 
49 };
50 
51 /**
52  Release all buffered log-events (discard_error_log_messages()),
53  optionally after running them through the error log stack first
54  (flush_error_log_messages()). Safe to call repeatedly (though
55  subsequent calls will only output anything if further events
56  occurred after the previous flush).
57 
58  @param mode LOG_BUFFER_DISCARD_ONLY (to just
59  throw away the buffered events), or
60  LOG_BUFFER_PROCESS_AND_DISCARD to
61  filter/print them first, or
62  LOG_BUFFER_REPORT_AND_KEEP to print
63  an intermediate report on time-out
64 */
66 
67 /**
68  Maximum number of key/value pairs in a log event.
69  May be changed or abolished later.
70 */
71 #define LOG_ITEM_MAX 64
72 
73 /**
74  Iterator over the key/value pairs of a log_line.
75  At present, only one iter may exist per log_line.
76 */
77 typedef struct _log_item_iter {
78  struct _log_line *ll; ///< log_line this is the iter for
79  int index; ///< index of current key/value pair
81 
82 /**
83  log_line ("log event")
84 */
85 typedef struct _log_line {
86  log_item_type_mask seen; ///< bit field flagging item-types contained
87  log_item_iter iter; ///< iterator over key/value pairs
88  int count; ///< number of key/value pairs ("log items")
89  log_item item[LOG_ITEM_MAX]; ///< log items
90 } log_line;
91 
92 // see include/mysql/components/services/log_builtins.h
93 
94 /**
95  Primitives for services to interact with the structured logger:
96  functions pertaining to log_line and log_item data
97 */
99  public: /* Service Implementations */
101  static DEFINE_METHOD(int, wellknown_by_name,
102  (const char *key, size_t length));
104  static DEFINE_METHOD(const char *, wellknown_get_name, (uint i));
105 
106  static DEFINE_METHOD(int, item_inconsistent, (log_item * li));
110 
111  static DEFINE_METHOD(bool, item_set_int, (log_item_data * lid, longlong i));
112  static DEFINE_METHOD(bool, item_set_float, (log_item_data * lid, double f));
113  static DEFINE_METHOD(bool, item_set_lexstring,
114  (log_item_data * lid, const char *s, size_t s_len));
115  static DEFINE_METHOD(bool, item_set_cstring,
116  (log_item_data * lid, const char *s));
117 
119  (log_item * li, log_item_type t, const char *key,
120  uint32 alloc));
122  (log_item * li, log_item_type t));
123 
125  (log_line * ll, log_item_type t, const char *key,
126  uint32 alloc));
128  (log_line * ll, log_item_type t));
129 
130  static DEFINE_METHOD(log_line *, line_init, ());
131  static DEFINE_METHOD(void, line_exit, (log_line * ll));
132  static DEFINE_METHOD(int, line_item_count, (log_line * ll));
133 
135  (log_line * ll, log_item_type_mask m));
136 
138  (log_line * ll));
139  static DEFINE_METHOD(void, line_item_iter_release, (log_item_iter * it));
143  (log_item_iter * it));
144 
145  static DEFINE_METHOD(int, line_submit, (log_line * ll));
146 
147  static DEFINE_METHOD(int, message, (int log_type, ...));
148 
149  static DEFINE_METHOD(int, sanitize, (log_item * li));
150 
151  static DEFINE_METHOD(const char *, errmsg_by_errcode, (int mysql_errcode));
152 
153  static DEFINE_METHOD(longlong, errcode_by_errsymbol, (const char *sym));
154 
155  static DEFINE_METHOD(const char *, label_from_prio, (int prio));
156 
157  static DEFINE_METHOD(int, open_errstream,
158  (const char *file, void **my_errstream));
159 
160  static DEFINE_METHOD(int, write_errstream,
161  (void *my_errstream, const char *buffer, size_t length));
162 
163  static DEFINE_METHOD(int, dedicated_errstream, (void *my_errstream));
164 
165  static DEFINE_METHOD(int, close_errstream, (void **my_errstream));
166 };
167 
168 /**
169  String primitives for logging services.
170 */
172  public: /* Service Implementations */
173  static DEFINE_METHOD(void *, malloc, (size_t len));
174  static DEFINE_METHOD(char *, strndup, (const char *fm, size_t len));
175  static DEFINE_METHOD(void, free, (void *ptr));
176  static DEFINE_METHOD(size_t, length, (const char *s));
177  static DEFINE_METHOD(char *, find_first, (const char *s, int c));
178  static DEFINE_METHOD(char *, find_last, (const char *s, int c));
179 
180  static DEFINE_METHOD(int, compare,
181  (const char *a, const char *b, size_t len,
182  bool case_insensitive));
183 
184  static DEFINE_METHOD(size_t, substitutev,
185  (char *to, size_t n, const char *fmt, va_list ap))
186  MY_ATTRIBUTE((format(printf, 3, 0)));
187 
188  static DEFINE_METHOD(size_t, substitute,
189  (char *to, size_t n, const char *fmt, ...))
190  MY_ATTRIBUTE((format(printf, 3, 4)));
191 };
192 
193 /**
194  Temporary primitives for logging services.
195 */
197  public: /* Service Implementations */
198  static DEFINE_METHOD(size_t, notify_client,
199  (void *thd, uint severity, uint code, char *to, size_t n,
200  const char *format, ...))
201  MY_ATTRIBUTE((format(printf, 6, 7)));
202 };
203 
204 /**
205  Syslog/Eventlog functions for logging services.
206 */
208  public: /* Service Implementations */
209  static DEFINE_METHOD(int, open, (const char *name, int option, int facility));
210  static DEFINE_METHOD(int, write, (enum loglevel level, const char *msg));
211  static DEFINE_METHOD(int, close, (void));
212 };
213 
214 #endif /* LOG_BUILTINS_IMP_H */
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:2701
void log_sink_buffer_flush(enum log_sink_buffer_flush_mode mode)
Release all buffered log-events (discard_error_log_messages()), optionally after running them through...
Definition: log_builtins.cc:1535
static int wellknown_by_type(log_item_type t) noexcept
See whether a type is wellknown.
Definition: log_builtins.cc:2613
struct _log_line log_line
log_line ("log event")
static int write(enum loglevel level, const char *msg) noexcept
Wrapper for mysys&#39; my_syslog.
Definition: log_builtins.cc:3600
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:2639
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:3015
const string name("\ame\)
char buffer[STRING_BUFFER]
Definition: test_sql_9_sessions.cc:57
log_sink_buffer_flush_mode
This defines built-in functions for use by logging services.
Definition: log_builtins_imp.h:45
log_item_type_mask seen
bit field flagging item-types contained
Definition: log_builtins_imp.h:86
Definition: log_shared.h:183
static void free(void *ptr) noexcept
Wrapper for my_free() - free allocated memory.
Definition: log_builtins.cc:3445
Definition: log_builtins_imp.h:47
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:3512
enum enum_log_item_type log_item_type
item_type – what to log
struct module_t * m
Definition: dbug_analyze.cc:469
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:3000
static char * find_first(const char *s, int c) noexcept
Wrapper for strchr() - find character in string, from the left.
Definition: log_builtins.cc:3460
printf("== done ==\)
uint64 log_item_type_mask
a bit mask of log_types.
Definition: log_shared.h:213
log_line ("log event")
Definition: log_builtins_imp.h:85
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:2714
static int line_item_count(log_line *ll) noexcept
How many items are currently set on the given log_line?
Definition: log_builtins.cc:2955
static int open(const char *name, int option, int facility) noexcept
Wrapper for mysys&#39; my_openlog.
Definition: log_builtins.cc:3577
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:2819
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:3521
static int sanitize(log_item *li) noexcept
Definition: log_builtins.cc:3143
#define DEFINE_METHOD(retval, name, args)
int index
index of current key/value pair
Definition: log_builtins_imp.h:79
static char * find_last(const char *s, int c) noexcept
Wrapper for strrchr() - find character in string, from the right.
Definition: log_builtins.cc:3468
Definition: log_builtins_imp.h:48
static int message(int log_type,...) noexcept
Submit a log-message for log "log_type".
Definition: log_builtins.cc:3123
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:3496
static bool item_set_int(log_item_data *lid, longlong i) noexcept
Set an integer value on a log_item.
Definition: log_builtins.cc:2730
Syslog/Eventlog functions for logging services.
Definition: log_builtins_imp.h:207
structure to define some default "config_option" option settings
Definition: innodb_config.h:180
static int open_errstream(const char *file, void **my_errstream) noexcept
open an error log file
Definition: log_builtins.cc:3268
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:3243
static char * strndup(const char *fm, size_t len) noexcept
Wrapper for my_strndup()
Definition: log_builtins.cc:3438
static void * malloc(size_t len) noexcept
Wrapper for my_malloc()
Definition: log_builtins.cc:3424
static bool item_generic_type(log_item_type t) noexcept
Predicate used to determine whether a type is generic (generic string, generic float, generic integer) rather than a well-known type.
Definition: log_builtins.cc:2688
log_item item[LOG_ITEM_MAX]
log items
Definition: log_builtins_imp.h:89
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:2925
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:3033
unsigned int len
Definition: dbug_analyze.cc:216
unsigned int uint
Definition: uca-dump.cc:29
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:2855
long long int longlong
Definition: my_inttypes.h:73
int count
number of key/value pairs ("log items")
Definition: log_builtins_imp.h:88
t
Definition: dbug_analyze.cc:147
uint32_t uint32
Definition: my_inttypes.h:62
Temporary primitives for logging services.
Definition: log_builtins_imp.h:196
char msg[1024]
Definition: test_sql_9_sessions.cc:282
static const char * key
Definition: suite_stubs.c:14
static size_t length(const char *s) noexcept
Wrapper for strlen() - length of a nul-terminated byte string.
Definition: log_builtins.cc:3452
static void line_exit(log_line *ll) noexcept
Release a log_line allocated with line_init()
Definition: log_builtins.cc:2944
static int item_inconsistent(log_item *li) noexcept
Sanity check an item.
Definition: log_builtins.cc:2674
String primitives for logging services.
Definition: log_builtins_imp.h:171
static longlong errcode_by_errsymbol(const char *sym) noexcept
Return MySQL error code for a given error symbol.
Definition: log_builtins.cc:3228
static const char * wellknown_get_name(uint i) noexcept
Accessor: from a record describing a wellknown key, get its name.
Definition: log_builtins.cc:2650
static int line_submit(log_line *ll) noexcept
Complete, filter, and write submitted log items.
Definition: log_builtins.cc:3084
#define LOG_ITEM_MAX
Maximum number of key/value pairs in a log event.
Definition: log_builtins_imp.h:71
static int close_errstream(void **my_errstream) noexcept
close an error log file previously opened with open_errstream()
Definition: log_builtins.cc:3387
Definition: log_shared.h:193
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:2969
static int close(void) noexcept
Wrapper for mysys&#39; my_closelog.
Definition: log_builtins.cc:3617
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:2628
Primitives for services to interact with the structured logger: functions pertaining to log_line and ...
Definition: log_builtins_imp.h:98
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:2982
loglevel
Definition: my_loglevel.h:32
Iterator over the key/value pairs of a log_line.
Definition: log_builtins_imp.h:77
struct _log_line * ll
log_line this is the iter for
Definition: log_builtins_imp.h:78
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:2779
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:2746
static int 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:3346
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:2894
static int dedicated_errstream(void *my_errstream) noexcept
are we writing to a dedicated errstream, or are we sharing it?
Definition: log_builtins.cc:3371
Definition: log_builtins_imp.h:46
static log_line * line_init() noexcept
Dynamically allocate and initialize a log_line.
Definition: log_builtins.cc:2935
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:3053
static bool item_set_lexstring(log_item_data *lid, const char *s, size_t s_len) noexcept
Set a string value on a log_item.
Definition: log_builtins.cc:2763
struct _log_item_iter log_item_iter
Iterator over the key/value pairs of a log_line.
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
enum enum_log_item_class log_item_class
static size_t notify_client(void *thd, uint severity, uint code, char *to, size_t n, const char *format,...) noexcept
Definition: log_builtins.cc:3538
static int log_type
Definition: mi_log.cc:46
log_item_iter iter
iterator over key/value pairs
Definition: log_builtins_imp.h:87
static const char * errmsg_by_errcode(int mysql_errcode) noexcept
Return MySQL error message for a given error code.
Definition: log_builtins.cc:3215