1 /* Copyright (c) 2017, 2020, 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 29 #include <mysql/components/services/log_builtins.h> 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 43 #include <mysql/components/services/log_shared.h> 44 45 enum log_sink_buffer_flush_mode { 46 LOG_BUFFER_DISCARD_ONLY, 47 LOG_BUFFER_PROCESS_AND_DISCARD, 48 LOG_BUFFER_REPORT_AND_KEEP 49 }; 50 51 enum log_error_stage { 52 LOG_ERROR_STAGE_BUFFERING_EARLY, ///< no log-destination yet 53 LOG_ERROR_STAGE_BUFFERING_UNIPLEX, ///< 1 sink, or individual files 54 LOG_ERROR_STAGE_BUFFERING_MULTIPLEX, ///< 2+ sinks writing to stderr 55 LOG_ERROR_STAGE_EXTERNAL_SERVICES_AVAILABLE, ///< external services available 56 LOG_ERROR_STAGE_SHUTTING_DOWN ///< no external components 57 }; 58 59 /// Set error-logging stage hint (e.g. are loadable services available yet?). 60 void log_error_stage_set(enum log_error_stage les); 61 62 /// What mode is error-logging in (e.g. are loadable services available yet)? 63 enum log_error_stage log_error_stage_get(void); 64 65 /** 66 Name of internal filtering engine (so we may recognize it when the 67 user refers to it by name in log_error_services). 68 */ 69 #define LOG_BUILTINS_FILTER "log_filter_internal" 70 71 /** 72 Name of internal log writer (so we may recognize it when the user 73 refers to it by name in log_error_services). 74 */ 75 #define LOG_BUILTINS_SINK "log_sink_internal" 76 77 /** 78 Name of buffered log writer. This sink is used internally during 79 start-up until we know where to write and how to filter, and have 80 all the components to do so. While we don't let the DBA add this 81 sink to the logging pipeline once we're out of start-up, we have 82 a name for this to be able to correctly tag its record in the 83 service-cache. 84 */ 85 #define LOG_BUILTINS_BUFFER "log_sink_buffer" 86 87 /** 88 Default services pipeline for log_builtins_error_stack(). 89 */ 90 #define LOG_ERROR_SERVICES_DEFAULT LOG_BUILTINS_FILTER "; " LOG_BUILTINS_SINK 91 92 /** 93 Release all buffered log-events (discard_error_log_messages()), 94 optionally after running them through the error log stack first 95 (flush_error_log_messages()). Safe to call repeatedly (though 96 subsequent calls will only output anything if further events 97 occurred after the previous flush). 98 99 @param mode LOG_BUFFER_DISCARD_ONLY (to just 100 throw away the buffered events), or 101 LOG_BUFFER_PROCESS_AND_DISCARD to 102 filter/print them first, or 103 LOG_BUFFER_REPORT_AND_KEEP to print 104 an intermediate report on time-out 105 */ 106 void log_sink_buffer_flush(enum log_sink_buffer_flush_mode mode); 107 108 /** 109 Maximum number of key/value pairs in a log event. 110 May be changed or abolished later. 111 */ 112 #define LOG_ITEM_MAX 64 113 114 /** 115 Iterator over the key/value pairs of a log_line. 116 At present, only one iter may exist per log_line. 117 */ 118 typedef struct _log_item_iter { 119 struct _log_line *ll; ///< log_line this is the iter for 120 int index; ///< index of current key/value pair 121 } log_item_iter; 122 123 /** 124 log_line ("log event") 125 */ 126 typedef struct _log_line { 127 log_item_type_mask seen; ///< bit field flagging item-types contained 128 log_item_iter iter; ///< iterator over key/value pairs 129 int count; ///< number of key/value pairs ("log items") 130 log_item item[LOG_ITEM_MAX]; ///< log items 131 } log_line; 132 133 // see include/mysql/components/services/log_builtins.h 134 135 /** 136 Primitives for services to interact with the structured logger: 137 functions pertaining to log_line and log_item data 138 */ 139 class log_builtins_imp { 140 public: /* Service Implementations */ 141 static DEFINE_METHOD(int, wellknown_by_type, (log_item_type t)); 142 static DEFINE_METHOD(int, wellknown_by_name, 143 (const char *key, size_t length)); 144 static DEFINE_METHOD(log_item_type, wellknown_get_type, (uint i)); 145 static DEFINE_METHOD(const char *, wellknown_get_name, (uint i)); 146 147 static DEFINE_METHOD(int, item_inconsistent, (log_item * li)); 148 static DEFINE_METHOD(bool, item_generic_type, (log_item_type t)); 149 static DEFINE_METHOD(bool, item_string_class, (log_item_class c)); 150 static DEFINE_METHOD(bool, item_numeric_class, (log_item_class c)); 151 152 static DEFINE_METHOD(bool, item_set_int, (log_item_data * lid, longlong i)); 153 static DEFINE_METHOD(bool, item_set_float, (log_item_data * lid, double f)); 154 static DEFINE_METHOD(bool, item_set_lexstring, 155 (log_item_data * lid, const char *s, size_t s_len)); 156 static DEFINE_METHOD(bool, item_set_cstring, 157 (log_item_data * lid, const char *s)); 158 159 static DEFINE_METHOD(log_item_data *, item_set_with_key, 160 (log_item * li, log_item_type t, const char *key, 161 uint32 alloc)); 162 static DEFINE_METHOD(log_item_data *, item_set, 163 (log_item * li, log_item_type t)); 164 165 static DEFINE_METHOD(log_item_data *, line_item_set_with_key, 166 (log_line * ll, log_item_type t, const char *key, 167 uint32 alloc)); 168 static DEFINE_METHOD(log_item_data *, line_item_set, 169 (log_line * ll, log_item_type t)); 170 171 static DEFINE_METHOD(log_line *, line_init, ()); 172 static DEFINE_METHOD(void, line_exit, (log_line * ll)); 173 static DEFINE_METHOD(int, line_item_count, (log_line * ll)); 174 175 static DEFINE_METHOD(log_item_type_mask, line_item_types_seen, 176 (log_line * ll, log_item_type_mask m)); 177 178 static DEFINE_METHOD(log_item_iter *, line_item_iter_acquire, 179 (log_line * ll)); 180 static DEFINE_METHOD(void, line_item_iter_release, (log_item_iter * it)); 181 static DEFINE_METHOD(log_item *, line_item_iter_first, (log_item_iter * it)); 182 static DEFINE_METHOD(log_item *, line_item_iter_next, (log_item_iter * it)); 183 static DEFINE_METHOD(log_item *, line_item_iter_current, 184 (log_item_iter * it)); 185 186 static DEFINE_METHOD(int, line_submit, (log_line * ll)); 187 188 static DEFINE_METHOD(int, message, (int log_type, ...)); 189 190 static DEFINE_METHOD(int, sanitize, (log_item * li)); 191 192 static DEFINE_METHOD(const char *, errmsg_by_errcode, (int mysql_errcode)); 193 194 static DEFINE_METHOD(longlong, errcode_by_errsymbol, (const char *sym)); 195 196 static DEFINE_METHOD(const char *, label_from_prio, (int prio)); 197 198 static DEFINE_METHOD(int, open_errstream, 199 (const char *file, void **my_errstream)); 200 201 static DEFINE_METHOD(int, write_errstream, 202 (void *my_errstream, const char *buffer, size_t length)); 203 204 static DEFINE_METHOD(int, dedicated_errstream, (void *my_errstream)); 205 206 static DEFINE_METHOD(int, close_errstream, (void **my_errstream)); 207 }; 208 209 /** 210 String primitives for logging services. 211 */ 212 class log_builtins_string_imp { 213 public: /* Service Implementations */ 214 static DEFINE_METHOD(void *, malloc, (size_t len)); 215 static DEFINE_METHOD(char *, strndup, (const char *fm, size_t len)); 216 static DEFINE_METHOD(void, free, (void *ptr)); 217 static DEFINE_METHOD(size_t, length, (const char *s)); 218 static DEFINE_METHOD(char *, find_first, (const char *s, int c)); 219 static DEFINE_METHOD(char *, find_last, (const char *s, int c)); 220 221 static DEFINE_METHOD(int, compare, 222 (const char *a, const char *b, size_t len, 223 bool case_insensitive)); 224 225 static DEFINE_METHOD(size_t, substitutev, 226 (char *to, size_t n, const char *fmt, va_list ap)) 227 MY_ATTRIBUTE((format(printf, 3, 0))); 228 229 static DEFINE_METHOD(size_t, substitute, 230 (char *to, size_t n, const char *fmt, ...)) 231 MY_ATTRIBUTE((format(printf, 3, 4))); 232 }; 233 234 /** 235 Temporary primitives for logging services. 236 */ 237 class log_builtins_tmp_imp { 238 public: /* Service Implementations */ 239 static DEFINE_METHOD(size_t, notify_client, 240 (void *thd, uint severity, uint code, char *to, size_t n, 241 const char *format, ...)) 242 MY_ATTRIBUTE((format(printf, 6, 7))); 243 }; 244 245 /** 246 Syslog/Eventlog functions for logging services. 247 */ 248 class log_builtins_syseventlog_imp { 249 public: /* Service Implementations */ 250 static DEFINE_METHOD(int, open, (const char *name, int option, int facility)); 251 static DEFINE_METHOD(int, write, (enum loglevel level, const char *msg)); 252 static DEFINE_METHOD(int, close, (void)); 253 }; 254 255 #endif /* LOG_BUILTINS_IMP_H */ 256