MySQL 9.4.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
logger.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2017, 2025, 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#ifndef MYSQL_HARNESS_LOGGER_LOGGER_INCLUDED
27#define MYSQL_HARNESS_LOGGER_LOGGER_INCLUDED
28
29#include "harness_export.h"
31
32#include <atomic>
33#include <set>
34#include <string>
35
36namespace mysql_harness {
37
38namespace logging {
39
40class Registry;
41
42/**
43 * Logger class.
44 *
45 * The logger class handles the logging for one or more logging
46 * handlers. Each logger class instance keeps state for logging for one
47 * module or subsystem. You can add handlers to a logger which will
48 * then be used for all logging to that subsystem.
49 */
50class HARNESS_EXPORT Logger {
51 public:
52 explicit Logger(Registry &registry, LogLevel level = kDefaultLogLevel);
53
54 // such null object is useless, however we need the ability to create an
55 // object and populate it later inside of guarded scope (e.g: std::lock_guard)
56 Logger() : level_(LogLevel::kNotSet) {}
57
58 void attach_handler(std::string name);
59 void detach_handler(std::string name, bool handler_must_exist = true);
60 void handle(const Record &record);
61
62 void lazy_handle(LogLevel record_level,
63 std::function<Record()> record_creator) const;
64
65 /**
66 * check if the log-level will be handled.
67 *
68 * log-messages may be filtered on global and on handler level.
69 *
70 * in case it is not handled, there is no need call the prepare data for
71 * the log-function.
72 *
73 * @returns if log-level will be handled or not
74 * @retval true log-level (quite likely) will be handled
75 * @retval false log-level will be ignored
76 */
77 bool is_handled(LogLevel level) const;
78 const std::set<std::string> &get_handler_names() const { return handlers_; }
79
80 void set_level(LogLevel level) { level_ = level; }
81 LogLevel get_level() const { return level_; }
83 precision_ = precision;
84 }
85 LogTimestampPrecision get_timestamp_precision() const { return precision_; }
86
87 private:
90 std::set<std::string> handlers_;
91 const Registry *registry_; // owner backreference (we don't own Registry,
92 // Registry owns us)
93};
94
95class HARNESS_EXPORT DomainLogger {
96 public:
97 DomainLogger() = default;
98
99 DomainLogger(std::string domain) : domain_(std::move(domain)) {}
100
101 void debug(std::invocable auto producer) const {
102 log(LogLevel::kDebug, std::move(producer));
103 }
104 void debug(const std::string &msg) const { log(LogLevel::kDebug, msg); }
105
106 void info(std::invocable auto producer) const {
107 log(LogLevel::kInfo, std::move(producer));
108 }
109 void info(const std::string &msg) const { log(LogLevel::kInfo, msg); }
110
111 void warning(std::invocable auto producer) const {
112 log(LogLevel::kWarning, std::move(producer));
113 }
114 void warning(const std::string &msg) const { log(LogLevel::kWarning, msg); }
115
116 void system(std::invocable auto producer) const {
117 log(LogLevel::kSystem, std::move(producer));
118 }
119 void system(const std::string &msg) const { log(LogLevel::kSystem, msg); }
120
121 void note(std::invocable auto producer) const {
122 log(LogLevel::kNote, std::move(producer));
123 }
124 void note(const std::string &msg) const { log(LogLevel::kNote, msg); }
125
126 void error(std::invocable auto producer) const {
127 log(LogLevel::kError, std::move(producer));
128 }
129 void error(const std::string &msg) const { log(LogLevel::kError, msg); }
130
131 void log(LogLevel log_level, std::function<std::string()> producer) const;
132
133 void log(LogLevel log_level, std::string msg) const;
134
135 private:
136 bool init_logger() const;
137
138 mutable std::mutex logger_mtx_;
139 mutable std::optional<mysql_harness::logging::Logger> logger_;
140 mutable std::atomic<bool> logger_ready_{false};
141
142 std::string domain_{MYSQL_ROUTER_LOG_DOMAIN};
143};
144
145} // namespace logging
146
147} // namespace mysql_harness
148
149#endif /* MYSQL_HARNESS_LOGGER_LOGGER_INCLUDED */
void debug(const std::string &msg) const
Definition: logger.h:104
void warning(std::invocable auto producer) const
Definition: logger.h:111
DomainLogger(std::string domain)
Definition: logger.h:99
void info(const std::string &msg) const
Definition: logger.h:109
std::mutex logger_mtx_
Definition: logger.h:138
void info(std::invocable auto producer) const
Definition: logger.h:106
void note(const std::string &msg) const
Definition: logger.h:124
void system(std::invocable auto producer) const
Definition: logger.h:116
void note(std::invocable auto producer) const
Definition: logger.h:121
void system(const std::string &msg) const
Definition: logger.h:119
void debug(std::invocable auto producer) const
Definition: logger.h:101
void warning(const std::string &msg) const
Definition: logger.h:114
void error(const std::string &msg) const
Definition: logger.h:129
std::optional< mysql_harness::logging::Logger > logger_
Definition: logger.h:139
void error(std::invocable auto producer) const
Definition: logger.h:126
Logger class.
Definition: logger.h:50
const Registry * registry_
Definition: logger.h:91
std::set< std::string > handlers_
Definition: logger.h:90
LogLevel get_level() const
Definition: logger.h:81
LogLevel level_
Definition: logger.h:88
void set_level(LogLevel level)
Definition: logger.h:80
const std::set< std::string > & get_handler_names() const
Definition: logger.h:78
Logger()
Definition: logger.h:56
LogTimestampPrecision precision_
Definition: logger.h:89
LogTimestampPrecision get_timestamp_precision() const
Definition: logger.h:85
void set_timestamp_precision(LogTimestampPrecision precision)
Definition: logger.h:82
Definition: registry.h:48
Logging interface for using and extending the logging subsystem.
static int record
Definition: mysqltest.cc:195
static loglevel log_level(const Sql_condition *condition)
Definition: histogram.cc:1650
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.
LogTimestampPrecision
Log timestamp precision values.
Definition: logging.h:160
Definition: common.h:44
static int handle(int sql_errno, const char *sqlstate, const char *message, void *state)
Bridge function between the C++ API offered by this module and the C API of the parser service.
Definition: services.cc:64
Definition: gcs_xcom_synode.h:64
#define MYSQL_ROUTER_LOG_DOMAIN
Definition: main.cc:26
Log record containing information collected by the logging system.
Definition: logging.h:182