MySQL 8.0.31
Source Code Documentation
syslog_plugin.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2022, 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 also distributed 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 included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef MYSQL_HARNESS_SYSLOG_PLUGIN_INCLUDED
26#define MYSQL_HARNESS_SYSLOG_PLUGIN_INCLUDED
27
30#include "mysql/harness/plugin.h"
31
32#include <syslog.h>
33#include <cstring>
34
35constexpr const char *kSyslogPluginName = "syslog";
36extern "C" {
37extern mysql_harness::Plugin harness_plugin_syslog;
38}
39
42
43/**
44 * Unix-based systems specific logging handler(sink) that writes the logs to the
45 * syslog.
46 */
48 public:
49 static constexpr unsigned kMaxIdentSize = 100;
50
51 SyslogHandler(bool format_messages = true, LogLevel level = LogLevel::kNotSet)
52 : mysql_harness::logging::Handler(format_messages, level,
53 LogTimestampPrecision::kSec) {}
54 ~SyslogHandler() override { close(); }
55
56 void open(const std::string &ident) noexcept {
57 std::strncpy(ident_, ident.c_str(), kMaxIdentSize);
58 ident_[kMaxIdentSize - 1] = '\0';
59 openlog(ident_, LOG_CONS | LOG_NDELAY, LOG_DAEMON);
60 }
61
62 void close() const noexcept { closelog(); }
63
64 // does nothing for the syslog handler
65 void reopen(const std::string /*dst*/) override {}
66
67 private:
68 void do_log(const mysql_harness::logging::Record &record) noexcept override {
69 syslog(log_level_to_syslog(record.level), "%s", record.message.c_str());
70 }
71
72 static int log_level_to_syslog(const LogLevel level) noexcept {
73 switch (level) {
74 case LogLevel::kFatal:
75 return LOG_EMERG;
76 case LogLevel::kError:
77 return LOG_ERR;
78 case LogLevel::kWarning:
79 return LOG_WARNING;
80 case LogLevel::kInfo:
81 case LogLevel::kNote:
82 case LogLevel::kSystem:
83 // Let loglevels NOTE and SYSTEM map to LOG_INFO
84 return LOG_INFO;
85 default: // kDebug
86 assert(level == LogLevel::kDebug);
87 return LOG_DEBUG;
88 }
89 }
90
92};
93
94#endif // MYSQL_HARNESS_SYSLOG_PLUGIN_INCLUDED
Unix-based systems specific logging handler(sink) that writes the logs to the syslog.
Definition: syslog_plugin.h:47
SyslogHandler(bool format_messages=true, LogLevel level=LogLevel::kNotSet)
Definition: syslog_plugin.h:51
static constexpr unsigned kMaxIdentSize
Definition: syslog_plugin.h:49
void open(const std::string &ident) noexcept
Definition: syslog_plugin.h:56
void close() const noexcept
Definition: syslog_plugin.h:62
static int log_level_to_syslog(const LogLevel level) noexcept
Definition: syslog_plugin.h:72
~SyslogHandler() override
Definition: syslog_plugin.h:54
char ident_[kMaxIdentSize]
Definition: syslog_plugin.h:91
void reopen(const std::string) override
Request to reopen underlying log sink.
Definition: syslog_plugin.h:65
void do_log(const mysql_harness::logging::Record &record) noexcept override
Log message handler primitive.
Definition: syslog_plugin.h:68
Base class for log message handler.
Definition: handler.h:49
Logging interface for using and extending the logging subsystem.
LogLevel
Log level values.
Definition: logging.h:95
LogTimestampPrecision
Log timestamp precision values.
Definition: logging.h:160
Definition: common.h:41
Definition: binlog.h:117
Log record containing information collected by the logging system.
Definition: logging.h:182
Definition: mi_test3.cc:54
mysql_harness::Plugin harness_plugin_syslog
Definition: syslog_plugin.cc:32
constexpr const char * kSyslogPluginName
Definition: syslog_plugin.h:35