MySQL 8.0.37
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 * Log level name for the default log level used by the router
150 */
151const char *const kDefaultLogLevelName = "warning";
152
153/**
154 * Log level name used in raw logging mode
155 */
156const char *const kRawLogLevelName = "info";
157
158/**
159 * Log timestamp precision values.
160 */
162 // Second
163 kSec = 0,
164
165 // Millisecond
166 kMilliSec = 3,
167
168 // Microsecond
169 kMicroSec = 6,
170
171 // Nanosecond
172 kNanoSec = 9,
173
174 kNotSet // Always higher than all other log precisions
175};
176
177/**
178 * Log record containing information collected by the logging
179 * system.
180 *
181 * The log record is passed to the handlers together with message.
182 */
183struct Record {
186 std::chrono::time_point<std::chrono::system_clock> created;
187 std::string domain;
188 std::string message;
189};
190
191/**
192 * Log message for the domain.
193 *
194 * This will log an error, warning, informational, or debug message
195 * for the given domain. The domain have to be be registered before
196 * anything is being logged. The `Loader` uses the plugin name as the
197 * domain name, so normally you should provide the plugin name as the
198 * first argument to this function.
199 *
200 * @param name Domain name to use when logging message.
201 *
202 * @param fmt `printf`-style format string, with arguments following.
203 */
204/** @{ */
205#ifdef __cplusplus
206extern "C" {
207#endif
208
209/**
210 * Pre-processor symbol containing the name of the log domain. If not
211 * defined explicitly when compiling, it will be an empty string, which
212 * means that it logs to the top log domain.
213 */
214
215#ifndef MYSQL_ROUTER_LOG_DOMAIN
216#define MYSQL_ROUTER_LOG_DOMAIN ""
217#endif
218
219/*
220 * We need to declare these first, because __attribute__ can only be used in
221 * declarations.
222 */
223static inline void log_system(const char *fmt, ...)
224 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
225static inline void log_error(const char *fmt, ...)
226 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
227static inline void log_warning(const char *fmt, ...)
228 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
229static inline void log_info(const char *fmt, ...)
230 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
231static inline void log_note(const char *fmt, ...)
232 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
233static inline void log_debug(const char *fmt, ...)
234 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
235static inline void log_custom(const LogLevel log_level, const char *fmt, ...)
236 ATTRIBUTE_GCC_FORMAT(printf, 2, 3);
237
238/*
239 * Define inline functions that pick up the log domain defined for the module.
240 */
241
242static inline void log_system(const char *fmt, ...) {
243 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
244 const char *fmt, va_list ap);
245 va_list ap;
246 va_start(ap, fmt);
248 va_end(ap);
249}
250
251static inline void log_error(const char *fmt, ...) {
252 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
253 const char *fmt, va_list ap);
254 va_list ap;
255 va_start(ap, fmt);
257 va_end(ap);
258}
259
260static inline void log_warning(const char *fmt, ...) {
261 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
262 const char *fmt, va_list ap);
263 va_list ap;
264 va_start(ap, fmt);
266 va_end(ap);
267}
268
269static inline void log_info(const char *fmt, ...) {
270 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
271 const char *fmt, va_list ap);
272 va_list ap;
273 va_start(ap, fmt);
275 va_end(ap);
276}
277
278static inline void log_note(const char *fmt, ...) {
279 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
280 const char *fmt, va_list ap);
281 va_list ap;
282 va_start(ap, fmt);
284 va_end(ap);
285}
286
287static inline void log_debug(const char *fmt, ...) {
288 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
289 const char *fmt, va_list ap);
290 va_list ap;
291 va_start(ap, fmt);
293 va_end(ap);
294}
295
296static inline void log_custom(const LogLevel log_level, const char *fmt, ...) {
297 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
298 const char *fmt, va_list ap);
299 va_list ap;
300 va_start(ap, fmt);
301 log_message(log_level, MYSQL_ROUTER_LOG_DOMAIN, fmt, ap);
302 va_end(ap);
303}
304
305/** @} */
306
307#ifdef __cplusplus
308}
309#endif
310
311HARNESS_EXPORT bool log_level_is_handled(LogLevel level, const char *domain);
312
313static inline bool log_level_is_handled(LogLevel level) {
315}
316
317} // namespace logging
318
319} // namespace mysql_harness
320
321/**
322 * convenience macro to avoid common boilerplate
323 */
324#define IMPORT_LOG_FUNCTIONS() \
325 using mysql_harness::logging::log_system; \
326 using mysql_harness::logging::log_error; \
327 using mysql_harness::logging::log_warning; \
328 using mysql_harness::logging::log_info; \
329 using mysql_harness::logging::log_note; \
330 using mysql_harness::logging::log_debug; \
331 using mysql_harness::logging::log_custom;
332
333#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:216
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:151
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:296
static void static void log_error(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:251
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:278
const char *const kRawLogLevelName
Log level name used in raw logging mode.
Definition: logging.h:156
static void log_system(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:242
static void static void static void static void log_info(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:269
static void static void static void static void static void static void log_debug(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:287
static void static void static void log_warning(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:260
LogTimestampPrecision
Log timestamp precision values.
Definition: logging.h:161
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:495
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:536
Log record containing information collected by the logging system.
Definition: logging.h:183
stdx::this_process::pid_type process_id
Definition: logging.h:185
LogLevel level
Definition: logging.h:184
std::string message
Definition: logging.h:188
std::string domain
Definition: logging.h:187
std::chrono::time_point< std::chrono::system_clock > created
Definition: logging.h:186