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