MySQL 9.1.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 * option name used in config file (and later in configuration
58 * object) to specify log level.
59 */
60namespace options {
61constexpr char kFilename[] = "filename";
62constexpr char kDestination[] = "destination";
63constexpr char kLevel[] = "level";
64constexpr char kTimestampPrecision[] = "timestamp_precision";
65constexpr char kSinks[] = "sinks";
66} // namespace options
67
68constexpr char kConfigSectionLogger[] = "logger";
69
70constexpr char kNone[] = "";
71/**
72 * Special names reserved for "main" program logger. It will use one of the
73 * two handlers, depending on whether logging_folder is empty or not.
74 */
75constexpr char kMainLogger[] = "main";
76constexpr char kMainLogHandler[] = "main_log_handler";
77constexpr char kMainConsoleHandler[] = "main_console_handler";
78
79constexpr char kSqlLogger[] = "sql";
80/**
81 * Default log filename
82 */
83constexpr char kDefaultLogFilename[] = "mysqlrouter.log";
84/**
85 * Log level values.
86 *
87 * Log levels are ordered numerically from most important (lowest
88 * value) to least important (highest value).
89 */
90enum class LogLevel {
91 /** Fatal failure. Router usually exits after logging this. */
92 kFatal,
93
94 /**
95 * System message. These messages are always logged, such as state changes
96 * during startup and shutdown.
97 */
98 kSystem,
99
100 /**
101 * Error message. indicate that something is not working properly and
102 * actions need to be taken. However, the router continue
103 * operating but the particular thread issuing the error message
104 * might terminate.
105 */
106 kError,
107
108 /**
109 * Warning message. Indicate a potential problem that could require
110 * actions, but does not cause a problem for the continuous operation
111 * of the router.
112 */
113 kWarning,
114
115 /**
116 * Informational message. Information that can be useful to check
117 * the behaviour of the router during normal operation.
118 */
119 kInfo,
120
121 /**
122 * Note level contains additional information over the normal informational
123 * messages.
124 */
125 kNote,
126
127 /**
128 * Debug message. Message contain internal details that can be
129 * useful for debugging problematic situations, especially regarding
130 * the router itself.
131 */
132 kDebug,
133
134 kNotSet // Always higher than all other log messages
135};
136
137/**
138 * Default log level used by the router.
139 */
141
142/**
143 * Default log level written by the router to the config file on bootstrap.
144 */
146
147/**
148 * Log level name for the default log level used by the router
149 */
150const char *const kDefaultLogLevelName = "warning";
151
152/**
153 * Log level name used in raw logging mode
154 */
155const char *const kRawLogLevelName = "info";
156
157/**
158 * Log timestamp precision values.
159 */
161 // Second
162 kSec = 0,
163
164 // Millisecond
165 kMilliSec = 3,
166
167 // Microsecond
168 kMicroSec = 6,
169
170 // Nanosecond
171 kNanoSec = 9,
172
173 kNotSet // Always higher than all other log precisions
174};
175
176/**
177 * Log record containing information collected by the logging
178 * system.
179 *
180 * The log record is passed to the handlers together with message.
181 */
182struct Record {
185 std::chrono::time_point<std::chrono::system_clock> created;
186 std::string domain;
187 std::string message;
188};
189
190/**
191 * Log message for the domain.
192 *
193 * This will log an error, warning, informational, or debug message
194 * for the given domain. The domain have to be be registered before
195 * anything is being logged. The `Loader` uses the plugin name as the
196 * domain name, so normally you should provide the plugin name as the
197 * first argument to this function.
198 *
199 * @param name Domain name to use when logging message.
200 *
201 * @param fmt `printf`-style format string, with arguments following.
202 */
203/** @{ */
204#ifdef __cplusplus
205extern "C" {
206#endif
207
208/**
209 * Pre-processor symbol containing the name of the log domain. If not
210 * defined explicitly when compiling, it will be an empty string, which
211 * means that it logs to the top log domain.
212 */
213
214#ifndef MYSQL_ROUTER_LOG_DOMAIN
215#define MYSQL_ROUTER_LOG_DOMAIN ""
216#endif
217
218/*
219 * We need to declare these first, because __attribute__ can only be used in
220 * declarations.
221 */
222static inline void log_system(const char *fmt, ...)
223 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
224static inline void log_error(const char *fmt, ...)
225 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
226static inline void log_warning(const char *fmt, ...)
227 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
228static inline void log_info(const char *fmt, ...)
229 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
230static inline void log_note(const char *fmt, ...)
231 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
232static inline void log_debug(const char *fmt, ...)
233 ATTRIBUTE_GCC_FORMAT(printf, 1, 2);
234static inline void log_custom(const LogLevel log_level, const char *fmt, ...)
235 ATTRIBUTE_GCC_FORMAT(printf, 2, 3);
236
237/*
238 * Define inline functions that pick up the log domain defined for the module.
239 */
240
241static inline void log_system(const char *fmt, ...) {
242 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
243 const char *fmt, va_list ap);
244 va_list ap;
245 va_start(ap, fmt);
247 va_end(ap);
248}
249
250static inline void log_error(const char *fmt, ...) {
251 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
252 const char *fmt, va_list ap);
253 va_list ap;
254 va_start(ap, fmt);
256 va_end(ap);
257}
258
259static inline void log_warning(const char *fmt, ...) {
260 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
261 const char *fmt, va_list ap);
262 va_list ap;
263 va_start(ap, fmt);
265 va_end(ap);
266}
267
268static inline void log_info(const char *fmt, ...) {
269 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
270 const char *fmt, va_list ap);
271 va_list ap;
272 va_start(ap, fmt);
274 va_end(ap);
275}
276
277static inline void log_note(const char *fmt, ...) {
278 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
279 const char *fmt, va_list ap);
280 va_list ap;
281 va_start(ap, fmt);
283 va_end(ap);
284}
285
286static inline void log_debug(const char *fmt, ...) {
287 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
288 const char *fmt, va_list ap);
289 va_list ap;
290 va_start(ap, fmt);
292 va_end(ap);
293}
294
295static inline void log_custom(const LogLevel log_level, const char *fmt, ...) {
296 extern void HARNESS_EXPORT log_message(LogLevel level, const char *module,
297 const char *fmt, va_list ap);
298 va_list ap;
299 va_start(ap, fmt);
301 va_end(ap);
302}
303
304/** @} */
305
306#ifdef __cplusplus
307}
308#endif
309
310HARNESS_EXPORT bool log_level_is_handled(LogLevel level, const char *domain);
311
312static inline bool log_level_is_handled(LogLevel level) {
314}
315
316} // namespace logging
317
318} // namespace mysql_harness
319
320/**
321 * convenience macro to avoid common boilerplate
322 */
323#define IMPORT_LOG_FUNCTIONS() \
324 using mysql_harness::logging::log_system; \
325 using mysql_harness::logging::log_error; \
326 using mysql_harness::logging::log_warning; \
327 using mysql_harness::logging::log_info; \
328 using mysql_harness::logging::log_note; \
329 using mysql_harness::logging::log_debug; \
330 using mysql_harness::logging::log_custom;
331
332#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:215
static loglevel log_level(const Sql_condition *condition)
Definition: histogram.cc:1644
constexpr char kSinks[]
Definition: logging.h:65
constexpr char kDestination[]
Definition: logging.h:62
constexpr char kLevel[]
Definition: logging.h:63
constexpr char kTimestampPrecision[]
Definition: logging.h:64
constexpr char kFilename[]
Definition: logging.h:61
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
const char *const kDefaultLogLevelName
Log level name for the default log level used by the router.
Definition: logging.h:150
constexpr char kDefaultLogFilename[]
Default log filename.
Definition: logging.h:83
constexpr char kSqlLogger[]
Definition: logging.h:79
const LogLevel kDefaultLogLevel
Default log level used by the router.
Definition: logging.h:140
LogLevel
Log level values.
Definition: logging.h:90
@ 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:75
constexpr char kNone[]
Definition: logging.h:70
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:295
static void static void log_error(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:250
constexpr char kMainConsoleHandler[]
Definition: logging.h:77
static void static void static void static void static void log_note(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:277
const char *const kRawLogLevelName
Log level name used in raw logging mode.
Definition: logging.h:155
static void log_system(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:241
static void static void static void static void log_info(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:268
static void static void static void static void static void static void log_debug(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:286
const LogLevel kDefaultLogLevelBootstrap
Default log level written by the router to the config file on bootstrap.
Definition: logging.h:145
static void static void static void log_warning(const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
Definition: logging.h:259
LogTimestampPrecision
Log timestamp precision values.
Definition: logging.h:160
constexpr char kMainLogHandler[]
Definition: logging.h:76
constexpr char kConfigSectionLogger[]
Definition: logging.h:68
HARNESS_EXPORT bool log_level_is_handled(LogLevel level, const char *domain)
Definition: registry.cc:514
Definition: common.h:42
Definition: options.cc:57
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:555
Log record containing information collected by the logging system.
Definition: logging.h:182
stdx::this_process::pid_type process_id
Definition: logging.h:184
LogLevel level
Definition: logging.h:183
std::string message
Definition: logging.h:187
std::string domain
Definition: logging.h:186
std::chrono::time_point< std::chrono::system_clock > created
Definition: logging.h:185