MySQL 8.4.0
Source Code Documentation
logging.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26/**
27 * @file
28 * @brief Logging interface for using and extending the logging subsystem.
29 */
30
31#ifndef MYSQL_HARNESS_LOGGING_INCLUDED
32#define MYSQL_HARNESS_LOGGING_INCLUDED
33
34#include "harness_export.h"
38
39#include <chrono>
40#include <cstdarg>
41#include <fstream>
42#include <list>
43#include <mutex>
44#include <string>
45
46namespace mysql_harness {
47
48namespace logging {
49
50/**
51 * Max message length that can be logged; if message is longer,
52 * it will be truncated to this length.
53 */
54const size_t kLogMessageMaxSize = 4096;
55
56/**
57 * Section name and option name used in config file (and later in configuration
58 * object) to specify log level, best explained by example:
59 *
60 * v----------------------- kConfigSectionLogger
61 * [logger]
62 * v------------------------ kConfigOptionLogLevel
63 * level = DEBUG
64 * v------------------------ kConfigOptionLogFilename
65 * filename = foo.log
66 * v------------------------ kConfigOptionLogTimestampPrecision
67 * timestamp_precision = second|sec|s|millisecond|msec|ms|
68 * microsecond|usec|us|nanosecond|nsec|ns
69 */
70constexpr char kConfigOptionLogFilename[] = "filename";
71constexpr char kConfigOptionLogDestination[] = "destination";
72constexpr char kConfigOptionLogLevel[] = "level";
73constexpr char kConfigOptionLogTimestampPrecision[] = "timestamp_precision";
74constexpr char kConfigSectionLogger[] = "logger";
75
76constexpr char kNone[] = "";
77/**
78 * Special names reserved for "main" program logger. It will use one of the
79 * two handlers, depending on whether logging_folder is empty or not.
80 */
81constexpr char kMainLogger[] = "main";
82constexpr char kMainLogHandler[] = "main_log_handler";
83constexpr char kMainConsoleHandler[] = "main_console_handler";
84
85constexpr char kSqlLogger[] = "sql";
86/**
87 * Default log filename
88 */
89constexpr char kDefaultLogFilename[] = "mysqlrouter.log";
90/**
91 * Log level values.
92 *
93 * Log levels are ordered numerically from most important (lowest
94 * value) to least important (highest value).
95 */
96enum class LogLevel {
97 /** Fatal failure. Router usually exits after logging this. */
98 kFatal,
99
100 /**
101 * System message. These messages are always logged, such as state changes
102 * during startup and shutdown.
103 */
104 kSystem,
105
106 /**
107 * Error message. indicate that something is not working properly and
108 * actions need to be taken. However, the router continue
109 * operating but the particular thread issuing the error message
110 * might terminate.
111 */
112 kError,
113
114 /**
115 * Warning message. Indicate a potential problem that could require
116 * actions, but does not cause a problem for the continuous operation
117 * of the router.
118 */
119 kWarning,
120
121 /**
122 * Informational message. Information that can be useful to check
123 * the behaviour of the router during normal operation.
124 */
125 kInfo,
126
127 /**
128 * Note level contains additional information over the normal informational
129 * messages.
130 */
131 kNote,
132
133 /**
134 * Debug message. Message contain internal details that can be
135 * useful for debugging problematic situations, especially regarding
136 * the router itself.
137 */
138 kDebug,
139
140 kNotSet // Always higher than all other log messages
141};
142
143/**
144 * Default log level used by the router.
145 */
147
148/**
149 * Default log level written by the router to the config file on bootstrap.
150 */
152
153/**
154 * Log level name for the default log level used by the router
155 */
156const char *const kDefaultLogLevelName = "warning";
157
158/**
159 * Log level name used in raw logging mode
160 */
161const char *const kRawLogLevelName = "info";
162
163/**
164 * Log timestamp precision values.
165 */
167 // Second
168 kSec = 0,
169
170 // Millisecond
171 kMilliSec = 3,
172
173 // Microsecond
174 kMicroSec = 6,
175
176 // Nanosecond
177 kNanoSec = 9,
178
179 kNotSet // Always higher than all other log precisions
180};
181
182/**
183 * Log record containing information collected by the logging
184 * system.
185 *
186 * The log record is passed to the handlers together with message.
187 */
188struct Record {
191 std::chrono::time_point<std::chrono::system_clock> created;
192 std::string domain;
193 std::string message;
194};
195
196/**
197 * Log message for the domain.
198 *
199 * This will log an error, warning, informational, or debug message
200 * for the given domain. The domain have to be be registered before
201 * anything is being logged. The `Loader` uses the plugin name as the
202 * domain name, so normally you should provide the plugin name as the
203 * first argument to this function.
204 *
205 * @param name Domain name to use when logging message.
206 *
207 * @param fmt `printf`-style format string, with arguments following.
208 */
209/** @{ */
210#ifdef __cplusplus
211extern "C" {
212#endif
213
214/**
215 * Pre-processor symbol containing the name of the log domain. If not
216 * defined explicitly when compiling, it will be an empty string, which
217 * means that it logs to the top log domain.
218 */
219
220#ifndef MYSQL_ROUTER_LOG_DOMAIN
221#define MYSQL_ROUTER_LOG_DOMAIN ""
222#endif
223
224/*
225 * We need to declare these first, because __attribute__ can only be used in
226 * declarations.
227 */
228static inline void log_system(const char *fmt, ...)
229 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
230static inline void log_error(const char *fmt, ...)
231 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
232static inline void log_warning(const char *fmt, ...)
233 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
234static inline void log_info(const char *fmt, ...)
235 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
236static inline void log_note(const char *fmt, ...)
237 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
238static inline void log_debug(const char *fmt, ...)
239 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
240static inline void log_custom(const LogLevel log_level, const char *fmt, ...)
241 ATTRIBUTE_GCC_FORMAT(printf, 2, 3);
242
243/*
244 * Define inline functions that pick up the log domain defined for the module.
245 */
246
247static inline void log_system(const char *fmt, ...) {
248 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
249 const char *fmt, va_list ap);
250 va_list ap;
251 va_start(ap, fmt);
253 va_end(ap);
254}
255
256static inline void log_error(const char *fmt, ...) {
257 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
258 const char *fmt, va_list ap);
259 va_list ap;
260 va_start(ap, fmt);
262 va_end(ap);
263}
264
265static inline void log_warning(const char *fmt, ...) {
266 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
267 const char *fmt, va_list ap);
268 va_list ap;
269 va_start(ap, fmt);
271 va_end(ap);
272}
273
274static inline void log_info(const char *fmt, ...) {
275 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
276 const char *fmt, va_list ap);
277 va_list ap;
278 va_start(ap, fmt);
280 va_end(ap);
281}
282
283static inline void log_note(const char *fmt, ...) {
284 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
285 const char *fmt, va_list ap);
286 va_list ap;
287 va_start(ap, fmt);
289 va_end(ap);
290}
291
292static inline void log_debug(const char *fmt, ...) {
293 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
294 const char *fmt, va_list ap);
295 va_list ap;
296 va_start(ap, fmt);
298 va_end(ap);
299}
300
301static inline void log_custom(const LogLevel log_level, const char *fmt, ...) {
302 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
303 const char *fmt, va_list ap);
304 va_list ap;
305 va_start(ap, fmt);
307 va_end(ap);
308}
309
310/** @} */
311
312#ifdef __cplusplus
313}
314#endif
315
316HARNESS_EXPORT bool log_level_is_handled(LogLevel level, const char *domain);
317
318static inline bool log_level_is_handled(LogLevel level) {
320}
321
322} // namespace logging
323
324} // namespace mysql_harness
325
326/**
327 * convenience macro to avoid common boilerplate
328 */
329#define IMPORT_LOG_FUNCTIONS() \
330 using mysql_harness::logging::log_system; \
331 using mysql_harness::logging::log_error; \
332 using mysql_harness::logging::log_warning; \
333 using mysql_harness::logging::log_info; \
334 using mysql_harness::logging::log_note; \
335 using mysql_harness::logging::log_debug; \
336 using mysql_harness::logging::log_custom;
337
338#endif // MYSQL_HARNESS_LOGGING_INCLUDED
#define ATTRIBUTE_GCC_FORMAT(style, fmt_pos, arg_pos)
Definition: compiler_attributes.h:33
#define MYSQL_ROUTER_LOG_DOMAIN
Log message for the domain.
Definition: logging.h:221
static loglevel log_level(const Sql_condition *condition)
Definition: histogram.cc:1643
const size_t kLogMessageMaxSize
Max message length that can be logged; if message is longer, it will be truncated to this length.
Definition: logging.h:54
constexpr char kConfigOptionLogLevel[]
Definition: logging.h:72
const char *const kDefaultLogLevelName
Log level name for the default log level used by the router.
Definition: logging.h:156
constexpr char kDefaultLogFilename[]
Default log filename.
Definition: logging.h:89
constexpr char kConfigOptionLogTimestampPrecision[]
Definition: logging.h:73
constexpr char kSqlLogger[]
Definition: logging.h:85
const LogLevel kDefaultLogLevel
Default log level used by the router.
Definition: logging.h:146
LogLevel
Log level values.
Definition: logging.h:96
@ kInfo
Informational message.
@ kNote
Note level contains additional information over the normal informational messages.
constexpr char kMainLogger[]
Special names reserved for "main" program logger.
Definition: logging.h:81
constexpr char kNone[]
Definition: logging.h:76
static void static void static void static void static void static void static void log_custom(const LogLevel log_level, const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:301
static void static void log_error(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:256
constexpr char kMainConsoleHandler[]
Definition: logging.h:83
constexpr char kConfigOptionLogFilename[]
Section name and option name used in config file (and later in configuration object) to specify log l...
Definition: logging.h:70
static void static void static void static void static void log_note(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:283
const char *const kRawLogLevelName
Log level name used in raw logging mode.
Definition: logging.h:161
static void log_system(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:247
static void static void static void static void log_info(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:274
static void static void static void static void static void static void log_debug(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:292
const LogLevel kDefaultLogLevelBootstrap
Default log level written by the router to the config file on bootstrap.
Definition: logging.h:151
static void static void static void log_warning(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:265
LogTimestampPrecision
Log timestamp precision values.
Definition: logging.h:166
constexpr char kMainLogHandler[]
Definition: logging.h:82
constexpr char kConfigSectionLogger[]
Definition: logging.h:74
constexpr char kConfigOptionLogDestination[]
Definition: logging.h:71
HARNESS_EXPORT bool log_level_is_handled(LogLevel level, const char *domain)
Definition: registry.cc:509
Definition: common.h:42
pid_t pid_type
Definition: process.h:45
void HARNESS_EXPORT log_message(LogLevel level, const char *module, const char *fmt, va_list ap)
Definition: registry.cc:550
Log record containing information collected by the logging system.
Definition: logging.h:188
stdx::this_process::pid_type process_id
Definition: logging.h:190
LogLevel level
Definition: logging.h:189
std::string message
Definition: logging.h:193
std::string domain
Definition: logging.h:192
std::chrono::time_point< std::chrono::system_clock > created
Definition: logging.h:191